Page 1 of 2

Short time updating variable?

PostPosted: Thu Jul 06, 2017 12:04 pm
by whmoorejr
Trying to make other sensors look like my DSC sensors on my control pages with Last Changed Short State times. Z-Wave motion detectors don't have that option... so I'm thinking a timestamp into a variable that triggers when motion is detected. I want to go one step further and have a schedule that updates all my Z-Wave motion detector times.... take current time minus timestamp time and put into a new variable as a short time (2m , 1h , 5d , etc.)

So, not sure where to start.... what is a timestamp format that will work mathematically with a scheduled python script to result in a 3 digit short time?

Sidenote.... any plugins that do this? There are lots of things / devices / where it would be cool to see when it was last used without scrolling through pages of log files.... when did the kids turn off their bedroom light.... how long has the bedroom fan been on, etc?

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 1:19 pm
by jay (support)
All devices have a lastChanged property that reflects the last time the device changed (in any way). This doesn't solve your problem though because you only care about the last time the motion sensor detected motion. You can do a trigger for each that updates a variable without an issue (you can use the Insert Timestamp into Variable action that used the standard Python date/time format specifiers to format the value any way you want).

However, trying to "fix" them later is a problem since the device doesn't store a "last time it detected motion" timestamp.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 2:34 pm
by whmoorejr
Was thinking something like this in a schedule? (it doesn't work.... still messing about with it)

Code: Select all
x = float(indigo.variables[1232853163].value)

theDate = indigo.server.getTime().date()
z = (theDate - x)
indigo.variable.updateValue(1796103489, str(z))


Edit: For the first variable (the timestamp) I changed the format to %j%H%M%S to give me a solid number to use for making a math formula.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 2:52 pm
by jay (support)
I'm not sure what you're trying to do here - can you explain a bit more? Python does date/time math nicely, I'm just not sure what you're trying to do...

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 3:35 pm
by whmoorejr
Device: Motion device detects something

Trigger: Motion event inserts timestamp into variable "Motion DTG" 1232853163

Schedule Every minute (or some other interval) a schedule runs a script....

Script: Current Time - Timestamp = Duration

Insert Duration into variable "Motion last" 1796103489

Control Page:
Motion Icon for motion device with variable text below "Motion Last".... preferably in an easy to understand format.... 3m = 3 minutes, 2d = 2 days, etc.... so at a glance I can see it has been 3 minutes or 2 days since that detector last saw motion.

Basically I want my non-DSC plugin devices to look the same. Putting a whole timestamp on a control page eats up a lot of Realestate.... makes it easy when you look up at an iPad at 2am to see which kid is up and look for the room with a 0m next to it.

Make sense?

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 4:17 pm
by Colly
I did something like what you're trying to recently which reminds me I didn't update my original post. I decided not to go the Python route as I'm really not comfortable with it yet - hope to get there someday! I use Colorado4Wheeler's Device Extensions Plugin - http://forums.indigodomo.com/viewtopic.php?f=197&t=16233 for my setup. When my Fibaro motion sensor triggers it updates the last changed property of the Device Extensions device, I then display the device state in my control page. See images below of both the device setup and a snapshot of my control page. For my gate sensor I also created a variable to display the actual time of last trigger. Hope this helps.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 5:01 pm
by jay (support)
I think Colly's idea is a good one, except using the variable as the source rather than the device state (as outlined above).

The script isn't terribly complex however:

Code: Select all
from datetime import datetime
# Get the source variable
source_var = indigo.variables[1232853163] # "Motion_DTG"
# Set the destination variable ID
dest_var_id = 1796103489 # "Motion_last"

# Assuming that your source variable string is of the format 2017-07-06 05:45PM,
# convert the source variable string into a Python datetime object
source_datetime = datetime.strptime(source_var.value, "%Y-%m-%d %I:%M%p") # use this format specifier to insert timestamp into var
# Subtract the source_datetime from the  current time from the server
delta = indigo.server.getTime() - source_datetime
# Insert the number of elapsed minutes (seconds / 60)
indigo.variable.updateValue(dest_var_id, str(delta.seconds/60)) # insert elapsed minutes into dest var


Not tested, but pretty close.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 5:29 pm
by whmoorejr
jay (support) wrote:
Code: Select all
source_datetime = datetime.strptime(source_var.value, "%Y-%m-%d %I:%M%p") # use this format specifier to insert timestamp into var
.


That's where I'm getting hung up. Tried your code and got "global name 'date time' is not defined"

Thought maybe it needed to be introduced first (like certain things in Applescript), so I added "import datetime" on the line previous... then got:

" 'module' object has no attribute 'strptime' "

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 5:33 pm
by whmoorejr
Colly wrote:
I did something like what you're trying to recently which reminds me I didn't update my original post. I decided not to go the Python route as I'm really not comfortable with it yet - hope to get there someday! I use Colorado4Wheeler's Device Extensions Plugin - http://forums.indigodomo.com/viewtopic.php?f=197&t=16233 for my setup. When my Fibaro motion sensor triggers it updates the last changed property of the Device Extensions device, I then display the device state in my control page. See images below of both the device setup and a snapshot of my control page. For my gate sensor I also created a variable to display the actual time of last trigger. Hope this helps.


Fun and thank you! I looked over that plugin earlier and dismissed it (Thought it was only for thermostats and sprinklers).

Thinking about adding stuff to some kid lights so at a glance I can see how long their bedroom light has been on.... stuff like that.

And thank you for the screen shot... I would have been plunking around with it for a while to get to that point.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 5:47 pm
by kw123
this should work:
Code: Select all
import datetime
# Get the source variable
source_var = indigo.variables[1232853163] # "Motion_DTG"
# Set the destination variable ID
dest_var_id = 1796103489 # "Motion_last"

# Assuming that your source variable string is of the format 2017-07-06 05:45PM,
# convert the source variable string into a Python datetime object
source_datetime = datetime.datetime.strptime(source_var.value, "%Y-%m-%d %I:%M%p") # use this format specifier to insert timestamp into var
# Subtract the source_datetime from the  current time from the server
delta = indigo.server.getTime() - source_datetime
# Insert the number of elapsed minutes (seconds / 60)
indigo.variable.updateValue(dest_var_id, str(delta.seconds/60)) # insert elapsed minutes into dest var

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 6:02 pm
by whmoorejr
kw123 wrote:
this should work:


Like a charm.... thank you.

Follow-up question.... to what point would running this script overburden indigo.... run every minute for 10 or 20 different devices..... to update 10 to 20 variables...

If firing off 10 or 20 python scripts pr. minute has little to no effect.... cool. Otherwise, I may look at an extra button on a control page that runs the scrips on command so the information is still there when I want it to be, but I'm not running 10 to 20 python scrips pr/minute for the 90% time that I'm not looking at the result.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 6:24 pm
by jay (support)
Fixed the script I posted above to import datetime.

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 6:26 pm
by jay (support)
whmoorejr wrote:
Follow-up question.... to what point would running this script overburden indigo.... run every minute for 10 or 20 different devices..... to update 10 to 20 variables...

If firing off 10 or 20 python scripts pr. minute has little to no effect.... cool. Otherwise, I may look at an extra button on a control page that runs the scrips on command so the information is still there when I want it to be, but I'm not running 10 to 20 python scrips pr/minute for the 90% time that I'm not looking at the result.


That shouldn't be a problem, but you don't need 20 scripts - just one script that updates 20 variables (just duplicate the logic in the script for each set of variables).

Re: Short time updating variable?

PostPosted: Thu Jul 06, 2017 6:51 pm
by whmoorejr
jay (support) wrote:
Just one script that updates 20 variables (just duplicate the logic in the script for each set of variables).


:face palm: That's what happens when I stare at the same problem for too long... total tunnel vision and I miss the simple stuff.

Thank you Jay!

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 10:07 am
by whmoorejr
Can I bypass the trigger that creates the source variable (timestamp) and just use the device's last changed state?

Tried this... but it's getting hung up.... I get 'datetime.datetime' has no attribute 'datetime'
I thought because the source format is %Y-%m-%d %I;%M:%S and the other format in the script is ..... %I:%M%p ?

When I change the stuff in the quotes to "%Y-%m-%d %I:%M:%S"... still hangs on that line with the same error.

(I'm using my office light right now instead of a motion sensor so I can control when it is on/off)

Code: Select all
from datetime import datetime
# Get the source variable
my_device = indigo.devices[937130354] # "Bill's Office Light"
# Set the destination variable ID
source_var = my_device.lastChanged

dest_var_id = 1796103489 # "Motion_last"

# Assuming that your source variable string is of the format 2017-07-06 05:45PM,
# convert the source variable string into a Python datetime object
source_datetime = datetime.datetime.strptime(source_var.value, "%Y-%m-%d %I:%M%p") # use this format specifier to insert timestamp into var
# Subtract the source_datetime from the  current time from the server
delta = indigo.server.getTime() - source_datetime
# Insert the number of elapsed minutes (seconds / 60)
indigo.variable.updateValue(dest_var_id, str(delta.seconds/60)) # insert elapsed minutes into dest var