Page 2 of 2

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 10:27 am
by howartp
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

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 10:31 am
by autolog
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

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 10:37 am
by howartp
That's useful @autolog as I didn't realise the difference in from * either.


Sent from my iPhone using Tapatalk Pro

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 10:49 am
by jay (support)
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

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 11:17 am
by jay (support)
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.

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 12:02 pm
by whmoorejr
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.

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 4:10 pm
by Colly
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 ...

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 5:06 pm
by whmoorejr
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.

Re: Short time updating variable?

PostPosted: Fri Jul 07, 2017 6:29 pm
by bkmar1192
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

Re: Short time updating variable?

PostPosted: Sat Jul 08, 2017 7:20 am
by whmoorejr
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.

Re: Short time updating variable?

PostPosted: Sat Jul 08, 2017 9:53 am
by bkmar1192
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