Subtracting Time

Posted on
Fri May 24, 2019 11:11 pm
jltnol offline
Posts: 410
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 time
from datetime import datetime
from datetime import timedelta

OffTime = 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.. :(

As always... THANKS in advance!


Code: Select all
from datetime import time
from datetime import datetime
from datetime import timedelta

on = indigo.variables[1506159459] #acOntime
off = indigo.variables[254766893] #acOffTime
total = indigo.variables[985437501] #cummAcTotal


OffTime = datetime.time(datetime.now())

indigo.variable.updateValue(254766893, value=unicode(OffTime)) #puts off time in variable
t1 = time delta(datetime.time)(off)-(datetime.time)(on)+(total) #this is obviously the problem line

indigo.variable.updateValue(985437501, value=datetime.time(t1))

Posted on
Sat May 25, 2019 2:32 am
howartp offline
Posts: 3667
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

Posted on
Sat May 25, 2019 4:34 am
kw123 offline
User avatar
Posts: 6583
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

Posted on
Sat May 25, 2019 5:43 am
DaveL17 offline
User avatar
Posts: 5017
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 strings
now        = dt.datetime.now()             # this is already a datetime object
on_time    = "2019-05-25 01:23:45.123456"  # this is a string
off_time   = "2019-05-25 01:32:45.974913"  # this is a string
total_time = "0:01:23.456789"              # this is a string

on_time_d  = dt.datetime.strptime(on_time, "%Y-%m-%d %H:%M:%S.%f")   # converts the string to datetime object
off_time_d = dt.datetime.strptime(off_time, "%Y-%m-%d %H:%M:%S.%f")  # converts the string to datetime object

t       = dt.datetime.strptime(total_time, "%H:%M:%S.%f")  # converts the string to datetime object
total_d = dt.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond)  # converts the datetime object to a timedelta object

diff = off_time_d - on_time_d  # Subtracting a datetime from a datetime yields a timedelta object

new_delta = total_d + diff  # Add the two timedelta objects together

indigo.server.log(unicode(total_time))  # old accum time
indigo.server.log(unicode(diff))        # latest delta
indigo.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]

Posted on
Sat May 25, 2019 7:54 am
jltnol offline
Posts: 410
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[1506159459] #acOntime
for this one line? Again, I think I can figure out the rest.. :)

Posted on
Sat May 25, 2019 9:30 am
jay (support) offline
Site Admin
User avatar
Posts: 15693
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[1506159459] #acOntime
for this one line? Again, I think I can figure out the rest.. :)


Close:

Code: Select all
on_time = indigo.variables[1506159459].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)
Twitter | Facebook | LinkedIn

Posted on
Sat May 25, 2019 9:53 am
jltnol offline
Posts: 410
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