### Subtracting Time

Posted:

**Fri May 24, 2019 11:11 pm**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

I was hoping that this line would get rid of the fractions of a second, but it doesn't, at least not as scripted

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!

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))