Trying to set up an interrupt


#1

Hi All, I have a small issue. I am trying to set up a morning alarm piston using just lights. I have got most of the piston put together but I am stumped on what part. I have an external remote that I want to use as a kind of snooze button for the piston. What I want is for the alarm to start at a certain time but if a button is pushed for it to be paused and then resume after a time expires. I have attached what I have so far.


#2

I think the easiest way to do that in your piston would be to add something like:

IF Button X is pushed
THEN Set StartAlarmTime = addMinutes(StartAlarmTime,10)

You might want to add one more IF statement to see if it’s a weekday or weekend, then add to the appropriate variable.

Or as an alternative, you could set up a SnoozeAlarmTime that is blank by default, then set to StartAlarmTime+10 minutes on snooze. That way your original alarm time isn’t overwritten and in need of updating every morning you hit snooze.

If that’s not clear how to do that, let me know and I’ll try to throw something together.


#3

I would test the timing to see if this is even going to work right. What I mean is when does the button press get detected by the piston and will that still be detected when you get to that point. Knowing that fades cannot be cancelled…and if you adjust the fade time up beyond the 2 minutes you show now (which you may have just put there for testing) you could also run into a snag with that as well.


#4

Thanks y’all for your help. The idea is to have the button get pressed during the fade time. For example lights begin to fade. I wake up before the fade is complete and then press the snooze button. The hope is the button push cancels the rest of the fade sits on a time for x minutes and then skips to the next if statement where it sets the lights to a specific color and level.

You are correct this is just for testing usually that time is around 10-15 minutes. I am still working it out.

Do you think it is possible to add a wait timer after the fade command and then reset the piston state by disrupting the execution?

I think I tried something like this but the question is can I interrupt the flow of the piston?


#5

Probably not if you’re using a fade. I don’t know of anybody that has found a way to cancel a fade once the fuse is lit:


#6

Thanks for your quick response that is very helpful. Now back to the drawing board :frowning:


#7

Agreed. That’s absolutely the way the one in WC works.

However, if you really want to make this happen you could still build your own fade. A fade is essentially a loop that would set the level of the light and then wait and repeat. If you build it yourself you could add your “snooze” detection into the middle of the fade (I’d still make the snooze trigger into a boolean that gets set elsewhere) and then reset everything.


#8

You could get around that by writing your own fade command:

Define another variable or two:

FadeLevel (integer)
MaxFadeLevel (integer)

Rather than using “Fade from 1% to 60% in two minutes”, try something like this:


#9

Are you suggesting a second piston somewhere that waits for a button press or just a seperate if statement in the main piston?


#10

This might be considerably easier. After reading through the last discussion link you posted I was trying to create a second piston to handle the snooze interrupt.


#11

Not a second piston, but just a separate IF block somewhere in your main piston. Two main reasons. First you’re not going to be worried about timing issues. Meaning did the button press happen at the right time in order to get noticed by the piston that is waiting (or changing the level of the light). Worst case this way you go thru one more loop, worst case trying to use the device as a trigger you completely miss it.

Secondly, I’ve personally had difficulty getting a piston to continue running code that is after a Loop when a device is used as a condition. Now that could have just been the way I was creating the piston. But it almost seemed like once the device flipped state it would want to cancel anything else that the piston was supposed to do next. In your case you would want it to keep doing stuff…you’d want it to reset the alarm time and turn the light back off. So you need ti to continue.

That’s why I think setting a boolean works better than testing the device itself in the loop.


#12

Thanks for the help. I think I will end up going that way but for now I am trying something different.
I got stuck on the idea of using the fade function but I am using a separate piston to manage the break. I have it mostly working but when I get the lights back on after the snooze it will not run the rest of the routine. I get this in the logs

8603fcc5-05b5-4a26-bacd-3c43be2609cf 4:49:20 PM: debug ║║ Cancelling statement #98’s schedules…
8603fcc5-05b5-4a26-bacd-3c43be2609cf 4:49:20 PM: debug â•‘â•‘ Comparison (string) First going off before snooze is (string) Coming Back from Snooze = false (2ms)
8603fcc5-05b5-4a26-bacd-3c43be2609cf 4:49:20 PM: debug â•‘â•‘ Comparison (string) First going off before snooze is (boolean) false = false (2ms)
8603fcc5-05b5-4a26-bacd-3c43be2609cf 4:49:20 PM: debug â•‘â•‘ Evaluating switch with values [[i:90:null:0, v:[t:string, v:First going off before snooze, d:0, vt:string]]]

I am curious if y’all have any idea what is happening. I have attached the pistons.


#13

What’s supposed to be in @SnoozeReady? Is it a string or a boolean?

Is that how you meant to construct your Switch Case?

Usually they’re done:

switch (variable)
case (value)


#14

That Variable is set to boolean.

Thank you for clearing that up I was not sure exactly how that was supposed to be set up. I have now set it up so the switch is the variable and the first case is true and the second case if false. I have also tried to clean up a couple other parts.


#15

Gotcha. Usually you don’t see a Switch Case with just 2 options as a simple IF will do the job. But a Switch Case is great otherwise so that you don’t get into a billion ELSE IF combinations.


#16

@michicago I have used your example here to setup a light to go on at a certain time in the morning and fade up from 1 to 80% in 10 minutes but to stop if the light is turned off sometime between that time. However, it seems to work for interrupting the fade but it isn’t fading up. In other words it is stuck at 1%. Can you see what I am doing wrong? Also, will this keep the light on once it reaches 80% until I set a time to go off?


#17

It sounds like the WHILE loop is failing to execute. When I get home this evening I’ll figure out what’s happening and will post a solution.

Would you mind posting a log for one execution in the meantime? I wonder if a condition change is canceling the while loop.


#18

Here is the current piston and log that I am working with. I have been testing it with a specific time and then I want to tell Alexa to turn off the light and the light will stay off.

+514ms â•‘Comparison (enum) off stays_unchanged = true (4ms)
+542ms â•‘Setting up scheduled job for Sun, Jan 21 2018 @ 1:07:00 PM EST (in 86174.633s)
+631ms â•šPiston successfully started (630ms)
1/20/2018, 1:06:57 PM +419ms
+1ms ╔Starting piston… (v0.2.102.20180116)
+440ms ║╔Subscribing to devices…
+477ms ║║Subscribing to Tom’s Light.switch…
+590ms â•‘â•šFinished subscribing (166ms)
+621ms â•‘Comparison (time) 47218037 happens_daily_at (time) 47220000 = false (0ms)
+626ms ║Cancelling statement #12’s schedules…
+628ms â•‘Requesting time schedule wake up at Sun, Jan 21 2018 @ 1:07:00 PM EST
+649ms â•‘Comparison (integer) 0 is_less_than (integer) 60 = true (4ms)
+664ms â•‘Comparison (enum) off stays_unchanged = true (4ms)
+691ms â•‘Setting up scheduled job for Sun, Jan 21 2018 @ 1:07:00 PM EST (in 86401.89s)
+703ms â•šPiston successfully started (703ms)
1/20/2018, 1:06:30 PM +748ms
+1ms ╔Starting piston… (v0.2.102.20180116)
+413ms ║╔Subscribing to devices…
+427ms ║║Subscribing to Tom’s Light.switch…
+547ms â•‘â•šFinished subscribing (150ms)
+575ms â•‘Comparison (time) 47191321 happens_daily_at (time) 46920000 = false (0ms)
+577ms ║Cancelling statement #12’s schedules…
+580ms â•‘Requesting time schedule wake up at Sun, Jan 21 2018 @ 1:02:00 PM EST
+591ms â•‘Comparison (integer) 0 is_less_than (integer) 60 = true (1ms)
+603ms â•‘Comparison (enum) off stays_unchanged = true (3ms)
+604ms ║Cancelling condition #11’s schedules…
+605ms ║Cancelling condition #5’s schedules…
+617ms ║Cancelling condition #17’s schedules…
+618ms ║Cancelling condition #13’s schedules…
+628ms â•‘Setting up scheduled job for Sun, Jan 21 2018 @ 1:02:00 PM EST (in 86128.625s)
+640ms â•šPiston successfully started (640ms)
1/20/2018, 1:04:17 PM +789ms
+2ms ╔Received event [Tom’s Light].switch = off with a delay of 131ms
+140ms â•‘RunTime Analysis CS > 17ms > PS > 49ms > PE > 74ms > CE
+143ms â•‘Runtime (40372 bytes) successfully initialized in 49ms (v0.2.102.20180116) (140ms)
+144ms â•‘â•”Execution stage started
+153ms â•‘â•‘Comparison (time) 47057940 happens_daily_at (time) 46920000 = false (0ms)
+155ms â•‘â•‘Condition #12 evaluated false (4ms)
+156ms ║║Cancelling statement #12’s schedules…
+160ms â•‘â•‘Requesting time schedule wake up at Sun, Jan 21 2018 @ 1:02:00 PM EST
+162ms â•‘â•‘Condition group #1 evaluated false (state did not change) (12ms)
+170ms â•‘â•‘Comparison (enum) off changes_to (string) off = true (0ms)
+171ms ║║Cancelling condition #17’s schedules…
+172ms â•‘â•‘Condition #17 evaluated true (6ms)
+173ms ║║Cancelling condition #13’s schedules…
+174ms â•‘â•‘Condition group #13 evaluated true (state changed) (9ms)
+177ms ║║Cancelling statement #14’s schedules…
+181ms ║║Skipped execution of physical command [Tom’s Light].off([]) because it would make no change to the device. (1ms)
+182ms ║║Executed [Tom’s Light].off (2ms)
+189ms ║║Executed virtual command [Tom’s Light].setVariable (4ms)
+193ms â•‘â•šExecution stage complete. (48ms)
+194ms â•‘Setting up scheduled job for Sun, Jan 21 2018 @ 1:02:00 PM EST (in 86262.018s)
+206ms â•šEvent processed successfully (205ms)


#19

I don’t think any of the events in the log were from a time trigger, so the piston never executed the while loop. Looks like they were either piston startups (after a change/save) or a light switch trigger.


#20

So how do I get the loop to trigger?