How do I correctly compare time values in a condition?


#1

1) Give a description of the problem
On odd occasions, if we are out later than usual our heating will turn on based on the schedule and there is no way for me to know so that I can switch it off or delay it turning on.

2) What is the expected behaviour?
I want to setup a piston which sends me a notification 1 hour before the scheduled “turn on” time for any of my heating zones and we are away.

3) What is happening/not happening?
I’m not sure how to do the time comparison for “60 minutes before the turn on time”. The device handler I am using for my heating system provides the ‘nextScheduledTime’ for a thermostat device which I have checked and that is the right attribute I want to check against. So what I want to say is “for any of my thermostat devices, if the ‘nextScheduledTime’ is 1 hour away and we are out, then send me a notification”.

You can see the logic I have coded in the piston screenshot below. I am comparing the ‘nextScheduledTime’ to the time + 60 minutes and if they are the same then send me a notification. Is this the right way to do the time comparison?

4) Post a Green Snapshot of the pistonimage


#2

Dates and times evaluate to ms since some time in the past.

So, look at this piston which logs some info.

Here are the results.

9/29/2017, 10:19:42 PM +637ms
+330ms	║Fri, Sep 29 2017 @ 10:19:42 PM EDT
+344ms	║Fri, Sep 29 2017 @ 10:19:41 PM EDT
+360ms	║Fri, Sep 29 2017 @ 10:18:42 PM EDT

Notice the first line is $now, the second line is 1 second less and the third line is 1 minute less.

1000ms is 1 second
60000ms is 1 minute

However, you can use the addMinutes or + 60000 (per minute) either will work…

You want something like…

if(nextScheduledTime == addMinutes($now, 60))

or

if(nextScheduledTime == ($now + 3600000)

But the way you have the piston won’t work. You need to loop through your devices and check to see if each “nextScheduledTime” is roughly an hour from now. I say roughly because you can’t check this every second, well you can but that will be a lot of work. I would check every 5 minutes, or every minute.

I can’t seem to find a device that has the “nextScheduleTime” property, but this should get your started.


#3

Thanks for replying @whoismoses. I fully understand the time stuff and can build that in. I’m a bit confused about the ‘for each’ loop though. What do I put in the ‘if’ statement for that? When I select to use the tStats variable in that ‘if’ condition the list of attributes I can use is different to the list of attributes I see when I select a specific thermostat device. And the list for the tStats variable doesn’t include the ‘nextScheduledTime’ attribute I need to use. So I’m a bit confused…


#4

I think you would do something like…

[$device : nextScheduledTime] is between addMinutes($now, 59)) and addMinutes($now, 61))

I picked 59 and 61 assuming you ran through the loop every 2 minutes.

Here is an example of a piston I have using a for each loop in a similar way, though I’m not comparing the time in this case.


#5

Interesting piston and a great learner. I see you named it security, so that gives an idea what your piston is doing in your home. Could you perhaps explain a little what type of devcices the 3 switches are at the top and what makes contact sensor 5 special that it has it’s own tasks?


#6

Those are virtual switches I created within smartthings. That part of the piston will only execute if they are all on. So for example, I have a virtual switch called “All Automation”. If I turn this switch off in the ST app, pretty much all of my automations will not execute. Basically I use them to control which automations run. I have another one called lighting automations. I have this virtual switch on all my automations that turn lights on / off. If the virtual switch is off, the pistons do nothing.

Only that I don’t want to get notifications for that particular contact sensor when I am on vacation because I leave a certain door open for the cats (inside the house).