Page 1 of 1

Trigger off of deltaTime?

PostPosted: Mon Mar 26, 2018 11:53 pm
by gisplatin
I"m trying to trigger off various durations running on my irrigation system.

The duration is expressed as deltaTime, generated from a start and a finish Timestamp. using this code, which I was very happy to find here :D

Code: Select all
from datetime import datetime

startTimeVariable = indigo.variables[1632591664].value  # REPLACE '123' WITH YOUR START TIME VARIABLE ID
endTimeVariable = indigo.variables[765744602].value  # REPLACE '456' WITH YOUR END TIME VARIABLE ID

try:
   startTime = datetime.strptime(startTimeVariable, "%b %d %H:%M:%S")
except StandardError, e:
    indigo.server.log(u"Error Converting Start Time ('%s'). Error='%s'" % (startTimeVariable, e), isError=True)   
    return
try:
   endTime = datetime.strptime(endTimeVariable, "%b %d %H:%M:%S")
except StandardError, e:
    indigo.server.log(u"Error Converting End Time ('%s'). Error='%s'" % (endTimeVariable, e), isError=True)   
    return

if endTime > startTime:
    deltaTime = endTime - startTime 
    indigo.variable.updateValue(5022321, str(deltaTime))  # REPLACE '789' WITH YOUR DELTA TIME VARIABLE ID
else:
    indigo.variable.updateValue(5022321, value="Start Time not before End Time")  # REPLACE '789' WITH YOUR DELTA TIME VARIABLE ID

This bit of python is working beautifully.. It delivers duration in H:MM:SS.

What I haven't figured out is how to trigger off of this data (greater than, less than, etc). Conditions in triggers are not responsive, no matter how I formulate the entry. After many hours trying, I've been unable to manipulate this datetime value into a more digestible, trigger-ready format.

Would anyone have a suggestion?

Re: Trigger off of deltaTime?

PostPosted: Tue Mar 27, 2018 3:00 am
by racarter
The problem is that your value is a time delta object, which can't be compared directly with a different type, such as an integer.

One way to do what you want would be to convert the time difference into an integer representing minutes or seconds in your Python script, then use that value in your trigger.
Code: Select all
def time_to_num(time_str):
    hh, mm , ss = map(int, time_str.split(':'))
    return ss + 60*(mm + 60*hh)


If you were to do the comparison in Python you could set a comparison value such as:
Code: Select all
 halfAnHour = datetime.timedelta(minutes = 30)

and compare against that.

Re: Trigger off of deltaTime?

PostPosted: Tue Mar 27, 2018 3:41 am
by DaveL17
Options! In addition to @racarter:

Code: Select all
# Python 2.7
duration = delta.total_seconds()

# Python <= 2.7
duration = (delta.days * 24 * 60 * 60) + delta.seconds + float('.' + str(b.microseconds))  # lazy conversion, but probably can drop the microseconds anyway

Untested, but should be close.

Re: Trigger off of deltaTime?

PostPosted: Tue Mar 27, 2018 8:37 am
by Colorado4Wheeler
If you want a non-Python method you could try Device Extensions, it has an elapsed time conversion that you could base off of the lastChanged date/time, it reports as minutes passed rather than HH:MM:SS. I also wrote similar functionality in the Room-O-Matic plugins to track HH:MM:SS of remaining time on irrigation zones.

What's the end goal of what you are trying to achieve?