Short time updating variable?

Posted on
Thu Jul 06, 2017 12:04 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Short time updating variable?

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?

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 1:19 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 06, 2017 2:34 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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.

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 2:52 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

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

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 06, 2017 3:35 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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?

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 4:17 pm
Colly offline
Posts: 535
Joined: Jan 16, 2016
Location: Ireland

Re: Short time updating variable?

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.
Attachments
Screen Shot 2017-07-06 at 22.59.44.png
Screen Shot 2017-07-06 at 22.59.44.png (15.55 KiB) Viewed 6874 times
Screen Shot 2017-07-06 at 23.03.33.png
Screen Shot 2017-07-06 at 23.03.33.png (41.86 KiB) Viewed 6874 times

Posted on
Thu Jul 06, 2017 5:01 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 06, 2017 5:29 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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' "

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 5:33 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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.

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 5:47 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Short time updating variable?

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

Posted on
Thu Jul 06, 2017 6:02 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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.

Bill
My Plugin: My People

Posted on
Thu Jul 06, 2017 6:24 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

Fixed the script I posted above to import datetime.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 06, 2017 6:26 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

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

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 06, 2017 6:51 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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!

Bill
My Plugin: My People

Posted on
Fri Jul 07, 2017 10:07 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

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

Bill
My Plugin: My People

Who is online

Users browsing this forum: No registered users and 1 guest