Short time updating variable?

Posted on
Fri Jul 07, 2017 10:27 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Short time updating variable?

Change datetime.datetime to just datetime

Once it's been imported above, you don't need to type it twice. (It's confusing because the module and the whateveritscalled are the same in this particular instance)


Sent from my iPhone using Tapatalk Pro

Posted on
Fri Jul 07, 2017 10:31 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Short time updating variable?

You either have to just:
Code: Select all
import datetime
in which case your code:
Code: Select all
source_datetime = datetime.datetime.strptime(source_var.value, "%Y-%m-%d %I:%M%p") # use this format specifier to insert timestamp into var
or you have to do as you have done which is:
Code: Select all
from datetime import datetime
in which case your code should read:
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


Datetime can be a bit confusing as it also contains datetime. :?

UPDATE: I see @howartp just :D beat me to it - but posting anyway as I had already typed it out

Posted on
Fri Jul 07, 2017 10:37 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Short time updating variable?

That's useful @autolog as I didn't realise the difference in from * either.


Sent from my iPhone using Tapatalk Pro

Posted on
Fri Jul 07, 2017 10:49 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

As I explained above, you can't use lastChanged because that represents ANY change to the device (going on, going off, being edited, etc.). If you're looking for the last time motion was detected, then you have to trigger and store that date separately.

Also, the script still won't work because you're trying to refer to a datetime object (my_device.lastChanged) as if it were an Indigo Variable. If you do really want to use lastChanged, then the script gets easier since you don't have to convert a string date/time into a datetime object at all:

Code: Select all
# Get the source device
my_device = indigo.devices[937130354] # "Bill's Office Light"
# Set the destination variable ID
dest_var_id = 1796103489 # "Motion_last"
# Subtract the device's lastChanged from the  current time from the server
delta = indigo.server.getTime() - my_device.lastChanged
# Insert the number of elapsed minutes (seconds / 60)
indigo.variable.updateValue(dest_var_id, str(delta.seconds/60)) # insert elapsed minutes into dest var

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jul 07, 2017 11:17 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Short time updating variable?

And, yeah, if there's one screw-up in the built-in Python classes IMO, it's the datetime module's naming conventions.

datetime is the module name. That module contains several classes, among them datetime. So, when you:

Code: Select all
import datetime


you're importing the entire datetime module. If you want to reference any of the classes/functions defined in it, then you have to use the dot notation:

Code: Select all
datetime.datetime # a datetime class


If you're only interested in the datetime class itself, just import the class rather than the entire module:

Code: Select all
from datetime import datetime


This is my preference - because datetime.datetime is a lot to type (and it looks really stupid LOL). Other modules name classes with a leading uppercase character (or PascalCase) which helps make it clearer (and, as it turns out, it's in the PEP8 style guide). For instance, the collections module has the awesome OrderedDict class (which is a standard Python dictionary except that when you iterate you get the key/values in the order they were added to the dict - so it iterates more like a list). So, it's clear when you look at a usage of OrderedDict that it's a class.

I'd like to see: datetime.DateTime, datetime.Date, datetime.Time, datetime.TimeDelta, etc. But, of course, such a fundamental change isn't backwards compatible so it would cause lots of breakage.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

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

Re: Short time updating variable?

jay (support) wrote:
As I explained above, you can't use lastChanged because that represents ANY change to the device


Right... it's an evolving thought.... triggers, scripts and schedules for things where I want specific information regularly... but maybe just a script that is launched via control page button or something to give me the duration of a current device (regardless of the state).

Example: garage door is open or closed... click a button and it will tell me how long it has been that way.

Or if I see that one of the lights was left on... click to see how long it has been on for.

Bill
My Plugin: My People

Posted on
Fri Jul 07, 2017 4:10 pm
Colly offline
Posts: 535
Joined: Jan 16, 2016
Location: Ireland

Re: Short time updating variable?

whmoorejr wrote:
Or if I see that one of the lights was left on... click to see how long it has been on for.

I like the sound of this 8) - my other half has a habit of leaving lights on, doors open etc ...

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

Re: Short time updating variable?

Colly wrote:
whmoorejr wrote:
Or if I see that one of the lights was left on... click to see how long it has been on for.

I like the sound of this 8) - my other half has a habit of leaving lights on, doors open etc ...


For me it's mostly kid issues. I have one with Autism that will get up at all hours of the night... even with a notification... if his bedroom light turns on after 10pm --> turn on master bedroom light.... I still might sleep through the light a bit longer. This answers the question, how long have you been awake?

We have to track his sleeping and eating patterns for his doctor.

Bill
My Plugin: My People

Posted on
Fri Jul 07, 2017 6:29 pm
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Re: Short time updating variable?

I know I am late to this conversation but a couple of other ideas to consider.

1) Use the Pester/Timer plugin. Create a timer for each device that gets reset and started when motion is detected. Might be the opposite of what you were looking for - so 0 would indicate no motion and anything great than zero would be motion.

2) I use my Dynamic Control plugin to shows an area of my house that has triggered motion. It then fades out as time elapses. Bright green = recent motion, the dimmer it is the longer it has been since motion detected. Makes for a very nice visual of where motion has been detected. Let me know if you are interested and I can go into more specifics.


Sent from my iPhone using Tapatalk

Posted on
Sat Jul 08, 2017 7:20 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Short time updating variable?

bkmar1192 wrote:
I know I am late...


Your timing is good. I have a couple main home control pages. Next, I want to do a heat map to represent activity. I was just trying to figure out how to do that with custom variable images or something.

Bill
My Plugin: My People

Posted on
Sat Jul 08, 2017 9:53 am
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Re: Short time updating variable?

I just posted an update to the manual that explains how to create the motion heat map. http://forums.indigodomo.com/viewtopic.php?f=241&t=18634

Who is online

Users browsing this forum: No registered users and 1 guest