Time stamps and variables

Posted on
Tue Mar 02, 2021 5:12 pm
jmiked offline
Posts: 71
Joined: Sep 08, 2003
Location: Austin, Texas

Time stamps and variables

I'm looking for away to compare the current time to a time stamp stored as a variable. The intent is to perform an action if the difference between the two is, say, greater than 8 hours. I've not found a way to do this yet. Any ideas?

Thanks.

Mike D.

Posted on
Wed Mar 03, 2021 5:58 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

How frequently do you want to check the difference? How often does the timestamp variable change and what changes it?

Personally--depending on the nature of the data--I would use a schedule that runs every X units of time (like every five minutes) that runs a Trigger with the following Python script as a condition:

Code: Select all
import datetime as dt

now      = indigo.server.getTime()
var      = indigo.variables[389753528].value
time_var = dt.datetime.strptime(var, "%Y-%m-%d %H:%M:%S.%f")
diff     = now - time_var

if diff > dt.timedelta(hours=8):
    return True
and then fire a trigger when the result is true to do whatever you need to do. The script is untested but should be pretty close.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Mar 05, 2021 8:58 pm
jmiked0 offline
Posts: 1
Joined: Mar 05, 2021

Re: Time stamps and variables

The time variable (ActivityLast) could be updated anywhere between a few minutes and 8 hours or so. I'd like to check it every 30 minutes or so.

Essentially, the variable gets set by a motion detector being triggered. If it does not get triggered for 8 hours (or whatever I decide) and the AtHome flag is set, it invokes an action to send a message to my in-town relatives to check up on me after giving me a chance to respond to an alert. I'm well past retirement and live alone, and this is in addition to my Apple watch which is good for emergencies like falls, but not so much if I am unresponsive.

I do have a system set up now that monitors a variable set by the motion detector, but it only checks at specific times of the day. I'd like to have something with a bit intelligence.

Mike D.

Posted on
Sat Mar 06, 2021 6:57 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

Sounds like the above script should to the trick then.

Schedule: run every 30 minutes.
Condition: the Python script above.
Action: Notify your in-town relatives.

The script above presumes a standard format for the timestamp variable, so if your's is different we'd have to tweak the script a bit. Of course, you'd also have to change the variable id to the one that matches your timestamp variable.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Mar 09, 2021 11:09 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Time stamps and variables

Nice script Dave, thanks!

Question; we have changed the script into the European date format and without the milliseconds, that works.
But we display also this date and time into a control page.
When we delete the year denotation, the script won't work anymore (no red errors but nothing happens).
With this Indigo Variable input: 2021-09-03 16:54 the script works perfect.
With this Variable 09-03 16:54 and this script content %d-%m %H:%M nothing happens anymore.

What have we done wrong?
And is it also possible to use only the time? (without a date).

Thanks for your support!

John
Last edited by McJohn on Wed Mar 10, 2021 1:44 am, edited 1 time in total.

Posted on
Tue Mar 09, 2021 11:16 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

Hello John - sorry to hear that you're having trouble adjusting the code. Please check your script for things like errant spaces and proper indentation (if needed) as this code works for me:

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import datetime as dt

var = "09-03 16:54"

now = dt.datetime.now()
var_dt = dt.datetime.strptime(var, "%d-%m %H:%M")

print(now - var_dt)


Result: 44194 days, 18:21:36.113743

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Mar 09, 2021 11:46 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Time stamps and variables

Hi Dave

Thank you so much for the fast reply.
I think your new script is another script than the first one?

We want to use this script:

Code: Select all
import datetime as dt

now      = indigo.server.getTime()
var      = indigo.variables[389753528].value
time_var = dt.datetime.strptime(var, "%Y-%m-%d %H:%M:%S.%f")
diff     = now - time_var

if diff > dt.timedelta(hours=1):
    return True


But then with this Indigo Variable input: 09-03 16:54 (thus without the year, seconds and milliseconds)
And another one for only the time: 16:54

Is that possible?

Posted on
Tue Mar 09, 2021 2:35 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

Yes, it was different. I was just meant to test your inputs. These *should* work. Obviously, you'll need to change the variable ID to match your variable(s).

09-03 16:54
Code: Select all
import datetime as dt

now      = indigo.server.getTime()
var      = indigo.variables[389753528].value
time_var = dt.datetime.strptime(var, "%m-%d %H:%M")
diff     = now - time_var

if diff > dt.timedelta(hours=1):
    return True


16:54
Code: Select all
import datetime as dt

now      = indigo.server.getTime()
var      = indigo.variables[389753528].value
time_var = dt.datetime.strptime(var, "%H:%M")
diff     = now - time_var

if diff > dt.timedelta(hours=1):
    return True

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Mar 10, 2021 3:51 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Time stamps and variables

Thanks for your time and input Dave.
There is something strange going on.
We tried your first script from your last post (with only the date and time) also earlier and now again and as said in our post before, there is no result (no error, nothing happens).
When we set the timedelta hours for example to 2000, there is no message; "Script did not return a valid Boolean".

When we edit the script and include the year denotation (include %Y) it works perfect.
When we delete only the %Y in the code (and of course the Variable input is also without the year), the script won't work anymore.

This problem is also for the time script ("%H:%M")

Or the Python script could only calculate with the year denotation?
or maybe the European date and time settings of the Mac (Wednesday, 10 March 2021), gives the problem?

John

Posted on
Wed Mar 10, 2021 6:14 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

Turns out, in my haste, I gave you incomplete (also known as bad) advice. For lack of a better way to describe it, Python is imputing the missing bits of time and using its best guess which, as it turns out, is a really bad guess. Consider this (the script takes Indigo out of the equation):

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import datetime as dt

var = "09-03 16:54"

now      = dt.datetime.now()
time_var = dt.datetime.strptime(var, "%m-%d %H:%M")
diff     = now - time_var

print(time_var)
print(diff)
1900-09-03 16:54:00
44017 days, 13:01:32.759175 (close to the result above which I failed to highlight).

Not very helpful.

Without knowing the complete value to work from, both datetime and very useful dateutil libraries struggle. You could "manually" adjust the input value with Python by adjusting the variable string before working with it, but that is fraught with peril. For example, if you assert the current year, this will break comparisons that span years.

I would suggest that the easiest and most robust way to solve the problem is to see if you can change the "shortened" variable values to full timestamps. If that's not possible, we can work on creating some custom scripts that will work for your circumstance.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Mar 10, 2021 8:12 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Time stamps and variables

Thanks for the explanation, I'm glad we're not crazy :roll:

No problem, we are going to use the full timestamp within an extra Indigo Variable. (They are inexhaustible :D )
(The strange thing is without the seconds and milliseconds it's working well)

Thanks for all your support.

Case closed.

John

Posted on
Wed Mar 10, 2021 9:38 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time stamps and variables

Glad to be some, if not completely marginal, service. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests