Calculate time between events


#1

1) Give a description of the problem
I’m trying to add a condition that will detect if a door is opened, then closed again within a short time frame (say, 20 seconds). I have the contact sensor in an On Event group, and i’m able to get the time stamp for each time the contact changes.

I can calculate the delta and display it with formatDuration and it looks like it’s storing the values correctly, however i can’t figure out how to do a comparison between dates in an expression. I want: if((currentChangeTime - previousChangeTime) > 20 Seconds).

Can anyone offer any suggestions as to what might work or the right way to go about this?


#2

First, I believe these variables should be of type datetime, not just time. Otherwise if they happened to occur just before and after midnight…

Then I think you want something like this:

if
    currentChangeTime is after addSeconds(previousChangeTime, 20)
then
    ...

#3

if((currentChangeTime - previousChangeTime) > 20000 milliseconds).


#4

I tried both of those and was getting unexpected results. The evaluation would come back with some exponent for a number and i could never pinpoint exactly where 20 seconds was. Doing 20 or 20000 never got it right.

I did find a solution, though somewhat inelegant:

replace(formatDuration(previousDoorChange - doorChange, true, "s"), "seconds", "") is less than 20


#5

Try changing your variables to type long


#6

You didn’t provide a picture of the Piston, so I have to guess at a few things. E.g., I’m guessing you’re only saving time, and not date & time (i.e., time variable vs datetime variable.) In any case, if I understood your ultimate goal, I came up with this simple example. (FYI, I’m using a virtual switch instead of a contact sensor, but it should work for that, too.)

The date & time of the current event is available in the system variable $currentEventDate. (I believe this is the same as the Virtual device “Date & Time.”) I’m comparing that to the previously saved event date & time + 20 seconds.

A comment about the variable previousChangeTimeValid. When the Piston is first created, the variable previousChangeTime will not have been initialized. (It will show as an invalid date & time.) If that was used in the condition, it’s my experience that webCoRE will use the value of $now for it. Therefore, the test would (effectively) be is $now before $now + 20, which would be true. To avoid that “false positive”, I added the variable previousChangeTimeValid. At first it will also be uninitialized, but my experience is that webCoRE will effectively treat that as false. Therefore, given the way I have it written, the complete condition group will not evaluate to true until at least the second time the Piston runs (when there will be a valid previous timestamp.)

I don’t know that I would attack the overall problem this way, but since I don’t have the complete picture of what you’re trying to do, I’m only providing feedback on your direct questions, and this is a concrete, working example of what my previous reply was trying to say.

Hope this helps.


#7

Sorry, i didn’t post a picture because the piston is quite large. I will try this implementation and see if it works better - thanks for the example!


#8

Understood. BTW, I said $currentEventDate is the same as the Virtual device Date & Time. D’oh! I should have said Date & Time is the same as $now. Oops.


#9

Your example worked - i must have had something out of order when i tried it that way earlier, or missed setting a variable in the right place.

Thanks for your help!