TCP and Cancel All Pending Tasks


#1

1) Give a description of the problem
I have a piston set to every morning at 6:40am fade my bedroom lights on over 20 min with a restriction that it only initiates the fade it if the sun hasn’t risen yet. If I don’t set the TCP to never, the fade will stop if the sun rises at some point between 6:40am and 7am. So, I set the TCP to never. However, sometimes, we will get up at some point during the fade and turn the lights all the way up. Of course a few seconds later, the next fade increment kicks in and the light get set back to the next fade level.

2) What is the expected behavior?
So… I 'm looking to have the fade get cancelled if someone alters the lights outside of the piston. Naturally, I’d like to use a “Cancel all pending tasks” upon switch/level changes from my bedroom light switch.

3) What is happening/not happening?
It’s more of a question: IF I have the TCP set to “Never” and then I try to “Cancel all pending tasks” - what happens? will the cancel all pending tasks statement override the TCP of never? Is there a better way to do this where the fade won’t stop if the sunrises between 640am and 7am without having to set the TCP to never?

4) Post a Green Snapshot of the pistonimage

Thanks!


#2

You’re deep in the weeds now. :slight_smile:

I look forward to seeing the replies on this one as I think this will be a learning experience for me as well.


#3

If line 34 overrides the TCP, then your piston is going to stop at 2%. The way Fade works is to compute the number of steps over time and schedule a new task at the next time increment. Line 30 looks for level change, which will occur on the step computed by line 26 and then cancel all remaining steps.

I’m not sure you can use Fade. I think you need to read the current level into a variable and do a ‘SetLevel $levelnow +1’ with a ‘Wait 25 seconds’ (I rounded up) all inside a ‘While $levelnow < 50’ loop. This way, if you manually increase the level it should pick up from there and continue until 50. And if you manually set the level greater than 50 it’ll stay there, rather than fading back down to 50.


#4

Actually, I take back my 1st paragraph. I overlooked the ‘and’ lines 31 & 32. I’m not sure that the piston will read the level change and switch change as simultaneous in order to cancel all pending (because they’re both triggers), but I still think you have to do a While-Do loop instead.


#5

This has been gone around recently, and the short answer is there are no easy solutions. I couldn’t find any system variable or trigger that I could use to determine a piston woke up to execute a fade step vs. anything else.

Lots of information in this thread that should help you find a solution:


#6

I like the sound of this suggestion!


#7

Yeah, I’ve tried every permutation I could think of with TCP and TEP and it seems a fade is just invincible. I made this simple piston last night to test things out:

And, here’s the log for me turning the switch off as the fade is in progress:

+1ms	╔Received event [Family Room Lights].switch = off with a delay of 695ms
+291ms	║RunTime Analysis CS > 17ms > PS > 227ms > PE > 47ms > CE
+293ms	║Runtime (37199 bytes) successfully initialized in 227ms (v0.2.101.20171227) (292ms)
+294ms	║╔Execution stage started
+303ms	║║Cancelling statement #5's schedules...
+309ms	║║Event detected
+310ms	║║Executed virtual command log (1ms)
+312ms	║║Executed virtual command cancelTasks (1ms)
+315ms	║╚Execution stage complete. (21ms)
+317ms	║Setting up scheduled job for Thu, Jan 4 2018 @ 9:26:00 PM PST (in 83813.043s)
+329ms	╚Event processed successfully (328ms)

you can see in the log that it properly detects me hitting the switch, executes cancelTasks, then proceeds to schedule the next job.

Which brings up some questions… what actually can you use cancel all pending tasks for? Is this a bug @ady624 or is it functioning “as intended”?

As it works now, once a fade is started it’s unstoppable without taking drastic action (e.g., editing the piston and re-saving it). I even tried setting the TCP to cancel tasks on piston state change or condition changes and forcing the condition to change with something like:

50 PM

(I also tried to do a manual piston state change).

In the mean time, I’ll use a a for loop to make my own fade and see if I can get that working. It does seem like the better solution would be to make fade() cancellable (unless there other reasons it isn’t that, which could be the case).

Anyway, thanks for reading this far :slight_smile:


#8

I came up with the following… can’t test it as I’m not home now…

What I intend to happen is the lights will start to fade on at 640am, but any operation of the switch stops the piston. Oh, and if the lights were already on, it does nothing in the first place.


#9

Are you sure that $currentEventDevice will be the bulb? Isn’t $currentEvent the time on line 21, with no real device (let alone one that can accept set level)?

Also, I was writing a piston this morning to send me increasingly frequent notifications if I left a door open, my first practical experience with variables and a while-loop. The only way I could get it to work was to use global dynamic variables. Local ones didn’t work, they stayed at whatever the value was at the beginning of the while-loop. You might have to put the level into a global dynamic var on each pass through the while-loop and then increment it. Add a line to reset it at the end, after your while-loop completes.


#10

@jsducote @michicago @Mike1616

I figured out how to do this. I use a while loop to increase the brightness by 1% every time through the loop. The while loop conditions check the final brightness level hasn’t been reached, the switch is still on, and that the current level of the switch is still the same as the last time the loop set it.

So, the fade starts and ends if someone turns off the switch, changes the switch level in any way, or when the final brightness is reached. Did extensive testing on this last night and it worked in all scenarios.


#11

That’s cool. I think I’ll use this in my “wake up” routine that I have as well.