## Subtracting Time

Page 1 of 1
jltnol offline
Posts: 390
Joined: Oct 15, 2013

### Subtracting Time

In an effort to keep track of how long the AC runs every day, I've been working on a script. When the unit comes on, a trigger entered the time into a variable. When the unit goes off, that time is entered into a second variable. And while I though I had this figured out, I quickly realized that just doing the math doesn't work, as when it comes on at 0758, and goes off at 0805, it hasn't been running for 47 minus, as the basic math would indicate.

I did a bunch of research online but sadly, can't quite figure out how to do time math. I've read up on datetime and timedelta, but I can't get the basics down. It has occurred to me that I don't really need to put the Off time into a variable. When the unit goes off, I can just subtract the On time from the current time, and that should give me the result I'm looking for. But I've also come to understand that storing the On time in a variable, means I have to recall it for the math, and it needs to appear as time data, not just a series of numbers for the calculation.

This script does put the time into the Off Variable, but it also add 6 decimal places, which, of course, I don't need. I have a similar script for the On Trigger
Code: Select all
`from datetime import timefrom datetime import datetimefrom datetime import timedeltaOffTime = datetime.time(datetime.now())indigo.variable.updateValue(254766893, value=unicode(OffTime)) #puts off time in variable`

I was hoping that this line would get rid of the fractions of a second, but it doesn't, at least not as scripted
Code: Select all
`OffTime = datetime.time(datetime.now(%H:%M:%S))`

But aside from the decimal issue, I need to recall the data from the ON variable, have it appear as time, subtract the Off time from the On time, and then store it in a variable. I'm also going to need to make this a cumulative number, but I think I can probably figure out how to do that if I can get just the basic run time calculated. With any examples of how to do the basic time math, I can probably figure out how to do the rest of this project, but for the moment... I'm stuck.. Code: Select all
`from datetime import timefrom datetime import datetimefrom datetime import timedeltaon = indigo.variables #acOntimeoff = indigo.variables #acOffTimetotal = indigo.variables #cummAcTotalOffTime = datetime.time(datetime.now())indigo.variable.updateValue(254766893, value=unicode(OffTime)) #puts off time in variablet1 = time delta(datetime.time)(off)-(datetime.time)(on)+(total) #this is obviously the problem lineindigo.variable.updateValue(985437501, value=datetime.time(t1))`

howartp offline
Posts: 3592
Joined: Jan 09, 2014
Location: West Yorkshire, UK

### Re: Subtracting Time

Just as an aside, have a look at the Timed Devices plugin.

I *think* that has a device type that does exactly what you want; tracks how long a given device has been in a given state, be it AC duration, gate left open, ....

Sent from my iPhone using Tapatalk Pro

kw123 offline Posts: 6449
Joined: May 12, 2013
Location: Dallas, TX

### Re: Subtracting Time

You can plot usage with indigoplotd

Select device state and measurement: count w reset once a day

That gives you the number of minutes in a day plot.

Sent from my iPhone using Tapatalk

DaveL17 offline Posts: 4845
Joined: Aug 20, 2013
Location: Chicago, IL, USA

### Re: Subtracting Time

If you want to learn how this stuff works, here is an example of how to do time math (if I didn't screw it up). You'll need to modify it to your liking.

Code: Select all
`import datetime as dt# variables are stored in Indigo as stringsnow        = dt.datetime.now()             # this is already a datetime objecton_time    = "2019-05-25 01:23:45.123456"  # this is a stringoff_time   = "2019-05-25 01:32:45.974913"  # this is a stringtotal_time = "0:01:23.456789"              # this is a stringon_time_d  = dt.datetime.strptime(on_time, "%Y-%m-%d %H:%M:%S.%f")   # converts the string to datetime objectoff_time_d = dt.datetime.strptime(off_time, "%Y-%m-%d %H:%M:%S.%f")  # converts the string to datetime objectt       = dt.datetime.strptime(total_time, "%H:%M:%S.%f")  # converts the string to datetime objecttotal_d = dt.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond)  # converts the datetime object to a timedelta objectdiff = off_time_d - on_time_d  # Subtracting a datetime from a datetime yields a timedelta objectnew_delta = total_d + diff  # Add the two timedelta objects togetherindigo.server.log(unicode(total_time))  # old accum timeindigo.server.log(unicode(diff))        # latest deltaindigo.server.log(unicode(new_delta))   # new accum time`

Outputs:
Code: Select all
`   Script                          0:01:23.456789   Script                          0:09:00.851457   Script                          0:10:24.308246`

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

jltnol offline
Posts: 390
Joined: Oct 15, 2013

### Re: Subtracting Time

Hey Folks this has been most helpful. I think I had tried the Timed Devices years ago, but have downloaded it and will take another look. It seems like it will do what I need/want.

And Dave, as for your script, I had ran across something very similar online in my search, and can almost understand how it works... I was able to get the OnTime trigger to put the data into the OnTime variable, and the data looks exactly as the sting data in your example. Getting the OffTime trigger to do the same is done as well.

But don't understand how to get the data from the variable back into the script. Where you have
Code: Select all
`on_time    = "2019-05-25 01:23:45.123456"  # this is a string`
obviously, I need to get that data from the OnTime variable., and that's what I don't get. I'm 99% sure the rest of the script I can bang on and figure out.

Is this what I need to do?
Code: Select all
`on_time = indigo.variables #acOntime`
for this one line? Again, I think I can figure out the rest.. jay (support) offline Posts: 15467
Joined: Mar 19, 2008
Location: Austin, Texas

### Re: Subtracting Time

jltnol wrote:
Is this what I need to do?
Code: Select all
`on_time = indigo.variables #acOntime`
for this one line? Again, I think I can figure out the rest.. Close:

Code: Select all
`on_time = indigo.variables.value #acOntime`

Your version gets an Indigo Variable object itself. The latter get the value of the Indigo Variable Object. So assuming your variable contains exactly the string as outlined in @Dave's post, it should work.

Jay (Indigo Support)

jltnol offline
Posts: 390
Joined: Oct 15, 2013

### Re: Subtracting Time

Thanks!

I'll work on this and see what happens...

As ALWAYS.

Thank you.

Page 1 of 1

### Who is online

Users browsing this forum: No registered users and 0 guests