Add hours to a timestamped variable

Posted on
Fri Jun 10, 2022 4:32 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Add hours to a timestamped variable

I can't find an easy way to add time (hours or minutes) to a time stamped variable. Once accomplished I would put that modified time into a new variable. I'm sure there is an easy way to accomplish this but I do not have any Python experience and couldn't find a plugin to do this. Any help would be appreciated.

Thanks,

Bill

Posted on
Fri Jun 10, 2022 4:35 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Add hours to a timestamped variable

It’s going to depend on the way the original time stamp was generated when placed in the variable. Can you post an example?


Sent from my iPhone using Tapatalk

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Jun 10, 2022 8:13 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

My string in the variable has truncated the seconds and leading year and looks something like this:

06-10 14:20

But I can make it the standard format in the timestamp variable if that makes it easier. I just wanted to add 4-6 hours onto an existing variable string to show up in a control panel. Like you need to do this in 4 hours at such and such time.

Thanks for looking at this so quickly.

Bill

Posted on
Fri Jun 10, 2022 9:35 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Add hours to a timestamped variable

How are you actually populating that variable? Script?


Sent from my iPhone using Tapatalk

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Jun 11, 2022 9:48 am
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

I'm using the Modify Variable
Attachments
Screen Shot 2022-06-11 at 8.45.29 AM.png
Screen Shot 2022-06-11 at 8.45.29 AM.png (84.14 KiB) Viewed 5585 times

Posted on
Sat Jun 11, 2022 9:58 am
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Add hours to a timestamped variable

This is pretty much all explained (except for the Indigo part) on this page: https://stackoverflow.com/questions/436 ... -timestamp

The Indigo part (reading and writing variables) is here: https://wiki.indigodomo.com/doku.php?id ... e_examples

The steps you need are:
  1. Read the Original variable into a Python string
  2. Use strptime to parse that string into a datetime object
  3. Use timedelta to add time to the datetime object
  4. Use strftime to generate a new string from the datetime object
  5. Save the new string to the new Indigo variable.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Jun 11, 2022 2:44 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

Well I was able to split the hours out but have become completely stumped beyond that. Can't get the 4 hours added to the hour string. I'm an infant when it comes to Python scripting. Not sure if there is an easier way to accomplish what I trying to do.

My wife is having surgery next week and needs to take pain meds every 4 hours. I created a control page that counts down the time and then starts over when you hit the start button again. Also logs the action. I thought it would be helpful if I could display the next time to take the next set of pills when she hits the start button. I also could use a variable to change the "4" to like a 6 for 6 hours).

I lot to ask, but I would be very grateful for any ideas to accomplish this.

Thanks,

Bill

Posted on
Sat Jun 11, 2022 2:53 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Add hours to a timestamped variable

billorav wrote:
Well I was able to split the hours out but have become completely stumped beyond that. Can't get the 4 hours added to the hour string. I'm an infant when it comes to Python scripting. Not sure if there is an easier way to accomplish what I trying to do.

My wife is having surgery next week and needs to take pain meds every 4 hours. I created a control page that counts down the time and then starts over when you hit the start button again. Also logs the action. I thought it would be helpful if I could display the next time to take the next set of pills when she hits the start button. I also could use a variable to change the "4" to like a 6 for 6 hours).

I lot to ask, but I would be very grateful for any ideas to accomplish this.

Thanks,

Bill


Post what you have so far and we'll help you along.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Jun 11, 2022 5:10 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

I started with this:

Variable: vpilltaketime timestamp 06-10 14:20
Format in insert Timestamp into Variable: %m-%d %H:%M

Then stared with this Python Script:

firstpill = indigo.variables["vpilltaketime"].value
indigo.variable.updateValue("vpillnexttime", firstpill[6:8])


Didn't get much further since my script wouldn't compile when used the strptime (more likely incorrectly.

Posted on
Sat Jun 11, 2022 6:17 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Add hours to a timestamped variable

Use the "Code" tags, not Bold.

Code: Select all
import indigo
from datetime import datetime, timedelta

DATEFORMAT = '%m-%d %H:%M'

# step 1
firstpill = indigo.variables["vpilltaketime"].value

# step 2
timestamp = datetime.strptime(firstpill, DATEFORMAT)

# step 3
timestamp = timestamp + timedelta(hours=4)

# step 4
nextpill = timestamp.strftime(DATEFORMAT)

# step 5
indigo.variable.updateValue("vpillnexttime", nextpill)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Jun 11, 2022 8:17 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

Your script works fantastic the first time!

I wish I could exchange the favor.

I do use Ecobee 2 extensively with 4 stats. If I see anything to debug as time goes by I'll let you know.

If I had more time, I would play around with Python.

Thanks again!

Bill

Posted on
Sat Jun 11, 2022 10:31 pm
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

I hate to bother you one more time, but I have tried to change the "4" hour in step 3 to a variable but I get the unicode error. Tried to update the value of the variable to an integer with no luck. I'm sure its simple but you are dealing with a python dummy here.

# step 3
timestamp = timestamp + timedelta(hours=4)

I would like to change the 4 to a variable and the add to the timestamp

My wife will thank you also after a painful shoulder surgery is coming up next week and we need to track the pain meds. Here is a start of my control page:

Thanks,

Bill
Attachments
IMG_0004.jpeg
IMG_0004.jpeg (109.33 KiB) Viewed 5509 times

Posted on
Sun Jun 12, 2022 2:30 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Add hours to a timestamped variable

FlyingDiver wrote:
billorav wrote:
Well I was able to split the hours out but have become completely stumped beyond that. Can't get the 4 hours added to the hour string. I'm an infant when it comes to Python scripting. Not sure if there is an easier way to accomplish what I trying to do.

My wife is having surgery next week and needs to take pain meds every 4 hours. I created a control page that counts down the time and then starts over when you hit the start button again. Also logs the action. I thought it would be helpful if I could display the next time to take the next set of pills when she hits the start button. I also could use a variable to change the "4" to like a 6 for 6 hours).

I lot to ask, but I would be very grateful for any ideas to accomplish this.

Thanks,

Bill


Post what you have so far and we'll help you along.
Have you played with the Timers & Pesters plugin, or Timed Devices plugin?

Both I’m sure will help you achieve this in native timed formats rather than trying to parse and increment strings.


Sent from my iPhone using Tapatalk Pro

Posted on
Sun Jun 12, 2022 5:54 am
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Add hours to a timestamped variable

Code: Select all
import indigo
from datetime import datetime, timedelta

DATEFORMAT = '%m-%d %H:%M'

# step 1
firstpill = indigo.variables["vpilltaketime"].value

# step 2
timestamp = datetime.strptime(firstpill, DATEFORMAT)

# step 2.5 - get the time interval
vpilldelay = indigo.variables["vpilldelay"].value

# step 3
timestamp = timestamp + timedelta(hours=int(vpilldelay))

# step 4
nextpill = timestamp.strftime(DATEFORMAT)

# step 5
indigo.variable.updateValue("vpillnexttime", nextpill)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jun 12, 2022 10:02 am
billorav offline
Posts: 24
Joined: Jun 24, 2015

Re: Add hours to a timestamped variable

Thank you. We're all set now.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests