Task Not Cancelled on Condition Change


#1

I have setup the below piston to automatically lock my front door 90 seconds after the door is unlock and contact sensor is closed. My understanding is that the default Task Cancellation Policy behavior is to cancel any pending on condition state changes. But in my testing, a pending wait to lock the door task was scheduled when the door is initially unlocked and the door sensor is still closed but wasn’t cancelled, as I expected, when the door sensor became open causing the scheduled task to run when the door was still opened. Am I understanding this correctly?

As a side note, I added a retry notification because my lock sometimes goes in a funky state where it would not respond to lock request until I restart my Smartthings hub.

Piston Rule:

Piston Logs:

12/19/2017, 4:45:24 PM +560ms
+1ms ╔Received event [Front Door].lock = locked with a delay of 947ms
+141ms ║RunTime Analysis CS > 20ms > PS > 32ms > PE > 89ms > CE
+143ms ║Runtime (38867 bytes) successfully initialized in 32ms (v0.2.100.20171211) (141ms)
+144ms ║╔Execution stage started
+150ms ║║Comparison (enum) locked is (string) unlocked = false (1ms)
+151ms ║║Cancelling condition #8’s schedules…
+152ms ║║Condition #8 evaluated false (5ms)
+152ms ║║Condition group #1 evaluated false (state did not change) (5ms)
+154ms ║╚Execution stage complete. (10ms)
+155ms ╚Event processed successfully (155ms)
12/19/2017, 4:45:17 PM +306ms
+1ms ╔Received event [Home].time = 1513719918188 with a delay of -883ms
+173ms ║RunTime Analysis CS > 21ms > PS > 19ms > PE > 134ms > CE
+177ms ║Runtime (38881 bytes) successfully initialized in 19ms (v0.2.100.20171211) (174ms)
+179ms ║╔Execution stage started
+322ms ║║Executed physical command [Front Door].lock() (123ms)
+323ms ║║Executed [Front Door].lock (125ms)
+334ms ║║Calculating (integer) 0 + (integer) 1 >> (integer) 1
+338ms ║║Executed virtual command [Front Door].setVariable (2ms)
+349ms ║║Comparison (integer) 1 is_greater_than_or_equal_to (integer) 3 = false (2ms)
+350ms ║║Condition #15 evaluated false (8ms)
+352ms ║║Condition group #12 evaluated false (state did not change) (9ms)
+355ms ║╚Execution stage complete. (177ms)
+357ms ╚Event processed successfully (356ms)
12/19/2017, 4:43:48 PM +473ms
+1ms ╔Received event [Front Door Sensor].contact = open with a delay of 519ms
+143ms ║RunTime Analysis CS > 23ms > PS > 25ms > PE > 94ms > CE
+145ms ║Runtime (38875 bytes) successfully initialized in 25ms (v0.2.100.20171211) (143ms)
+146ms ║╔Execution stage started
+157ms ║║Comparison (enum) unlocked is (string) unlocked = true (1ms)
+158ms ║║Cancelling condition #8’s schedules…
+159ms ║║Condition #8 evaluated true (10ms)
+162ms ║║Comparison (enum) open is (string) closed = false (1ms)
+163ms ║║Cancelling condition #10’s schedules…
+163ms ║║Condition #10 evaluated false (4ms)
+164ms ║║Condition group #1 evaluated false (state did not change) (15ms)
+166ms ║╚Execution stage complete. (20ms)
+166ms ╚Event processed successfully (166ms)
12/19/2017, 4:43:48 PM +18ms
+2ms ╔Received event [Front Door].lock = unlocked with a delay of 930ms
+142ms ║RunTime Analysis CS > 21ms > PS > 24ms > PE > 96ms > CE
+144ms ║Runtime (38869 bytes) successfully initialized in 24ms (v0.2.100.20171211) (141ms)
+145ms ║╔Execution stage started
+151ms ║║Comparison (enum) unlocked is (string) unlocked = true (1ms)
+152ms ║║Cancelling condition #8’s schedules…
+153ms ║║Condition #8 evaluated true (5ms)
+160ms ║║Comparison (enum) closed is (string) closed = true (1ms)
+161ms ║║Condition #10 evaluated true (8ms)
+162ms ║║Cancelling condition #1’s schedules…
+163ms ║║Condition group #1 evaluated true (state changed) (14ms)
+164ms ║║Cancelling statement #5’s schedules…
+169ms ║║Executed virtual command [Front Door].wait (0ms)
+169ms ║║Requesting a wake up for Tue, Dec 19 2017 @ 4:45:18 PM EST (in 90.0s)
+173ms ║╚Execution stage complete. (28ms)
+174ms ║Setting up scheduled job for Tue, Dec 19 2017 @ 4:45:18 PM EST (in 89.997s)
+179ms ╚Event processed successfully (179ms)


#3

I believe a ‘while’ won’t cancel it’s actions like an ‘IF’ does.

Try putting the closed condition inside the while.

While Door is unlocked
do
IF Door is closed
Wait 90 seconds
Lock
set variable


#4

Thanks I have updated my piston based on your feedback. I am just wondering about the efficiency since the while statement will be constantly looping when the door is open.


#5

It’s only once every 90 seconds, but you could just repeat the closed condition:

While Door is unlocked
AND
Door is closed
do
IF Door is closed
Wait 90 seconds
Lock
set variable


#6

Having the door is closed condition in the while loop, as well, would definitely prevent the while statement from unnecessarily looping when the door is open. I was just trying to get a more streamlined piston. I guess if I didn’t have the retries notification logic it would just be a simple if statement.


#7

So after rethinking how to get a cleaner version of my piston, I was able to implement what I initially intended with just a simple if statement along with a delayed failed notification which gets cancelled if the door actually locks.