Dewster35 wrote:Gotcha... so hours and minutes (along with weeks, months and years) don't exist... we just do some math to make it in a readable format for what we understand? I guess where I was being tripped up was that there was actually a day, hour, minute, second value that was being populated. This makes more sense even though it is a little more work for the user to get it into a friendly format.
The datetime object has year, month, day, minute, second, microsecond, etc. attributes. The timedelta object, which is returned when you add or subtract datetime objects, only has days, seconds, and microseconds.
Dewster35 wrote:So now this works for a single device that I'm tracking... if I have multiple devices that I'm tracking, what would the most efficient way to do this be? I currently have a schedule in indigo that basically runs this script a little over every minute.
So, you have a trigger for each device you care about that updates a device specific variable? For example, you could have a device called "Office Motion Sensor" which fires a trigger whenever it goes on that populates the date/time into a variable called "officeMotionSensorLastOn" and then this script populates a variable called "officeMotionSensorLastOnReadable" with the human version.
A relatively straight-forward modification to the script would perform the operation for each variable pair:
- Code: Select all
# this is a list of Python tuples (something like a list) - for each device specific
# "last on" source variable there's a corresponding device specific human readable destination
# var so you construct the list by putting the ID of the first source var, comma, the ID of the
# destination var inside parenthesis and separated by commas inside square brackets (which identifies
# it as a standard Python list).
varPairList = [(SOURCEVAR1ID, DESTVAR1ID), (SOURCEVAR2ID, DESTVAR2ID)]
# now that you have the list, you just cycle through the list grabbing the source id and destination
# id and use them in the appropriate spots in the previous script
for sourceVarId, destVarId in varPairList:
# import the datetime object from the datetime module (kinda silly but that's a Python oddity)
from datetime import datetime
# get the date/time string from the variable that was inserted using the Insert Timestamp into Variable
dateString = indigo.variables[sourceVarId].value
# convert the date/time string into an actual datetime Python object
lastOn = datetime.strptime(dateString, "%Y-%m-%d %H:%M:%S")
# subtract the current date/time from the converted datetime and return a timedelta object
delta = datetime.now() - lastOn
# timedelta objects only have days, seconds, and microseconds as attributes
# so you have to calculate hours and minutes then once that's done we
# set the string elapsedText to represent the human readable version
if delta.days > 0:
# if at least a day has elapsed then
# set the string to look like this: "2 days"
elapsedText = "%i days" % delta.days
elif delta.seconds / (60 * 60) > 0:
# if at least one hour (60 seconds * 60 minutes) has elapsed then
# set the string to look like this: "2 hours"
elapsedText = "%i hours" % (delta.seconds / (60 * 60))
elif delta.seconds / 60 > 0:
# if at least one minute has elapsed then
# set the string to look like this: "2 minutes"
elapsedText = "%i minutes" % (delta.seconds / 60)
else:
# Less than 60 seconds has passed since the last activation so we'll just
# set the string to look like this: "now"
elapsedText = "now"
indigo.variable.updateValue(destVarId, elapsedText)