Time difference seconds

Posted on
Sat Mar 28, 2020 1:15 pm
c64 offline
User avatar
Posts: 53
Joined: Oct 28, 2012
Location: Germany

Time difference seconds

I'm a bit lost! I'm trying to figure out if a button was released within 1 second. I have an Indigo Trigger timestamping the ButtonPress_on, with this python script measuring the ButtonPress_off:

Code: Select all
from datetime import time

ButtonPress_on = indigo.variables[27640136] # "var_SpotifyButtonPress"

timeDelta = datetime.now() - ButtonPress_on.value
if timeDelta.seconds > 1:
   indigo.server.log("long press")
else:
   indigo.server.log("short press")

Posted on
Sat Mar 28, 2020 1:43 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Time difference seconds

1. what format is the date string in the variable?
2. the variable is a date STRING, not a datetime object
3. import datetime from datetime
then use datetime.now()
but thats just the start.

you should read on the datetime arithmetics .. and that is actually complicated

in principle:
import time
import datetime
1. get date time string from your variable
timeString = indigo.variables[27640136] .value
2. convert that string to "time since epoch" (the "%Y has to match the datetime string format in your variable)
timeWhenButtonPressed = time.mktime(time.strptime(timeString, "%Y-%m-%d-%H:%M:%S"))
deltaTime = time.time()- timeWhenButtonPressed

if deltaTime >1:
xx
else:
yy

Posted on
Sun Mar 29, 2020 2:39 am
c64 offline
User avatar
Posts: 53
Joined: Oct 28, 2012
Location: Germany

Re: Time difference seconds

Thank you so much!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests

cron