While Loop Not Executing How I Expected


#1

1) Give a description of the problem
The piston is executing from the top line of code when it exits the While loop.

2) What is the expected behavior?
What I want this to do is:
When the TV is switched off, set the family room lights to 20% and turn on the master bedroom lights. [The TV switch was removed from this code for testing purposes]
Monitor the master bedroom lights, and when that switch is turn off, turn off the lights in the family room.

3) What is happening/not happening?
When the master bedroom lights are switched off, it executes the code from the top. What I want it to do is simply execute the code beneath the While Loop and complete the piston. How do I do this?

4) Post a Green Snapshot of the pistonimage

5) Attach any logs (From ST IDE and by turning logging level to Full)
11/10/2017, 11:18:55 AM +628ms +1ms ╔Received event [Master Bedroom Main Lights].switch = off with a delay of 81ms +247ms ║RunTime Analysis CS > 21ms > PS > 54ms > PE > 173ms > CE +262ms ║Runtime (39058 bytes) successfully initialized in 54ms (v0.2.0fd.20171105) (260ms) +263ms ║╔Execution stage started +272ms ║║Comparison (integer) 11 is_inside_of_range (integer) 9 .. (integer) 23 = true (2ms) +273ms ║║Condition #7 evaluated true (5ms) +277ms ║║Calculating (integer) 10 + (integer) 1 >> (integer) 11 +280ms ║║Comparison (integer) 11 is_less_than (integer) 11 = false (0ms) +281ms ║║Condition #22 evaluated false (7ms) +281ms ║║Condition group #1 evaluated false (state did not change) (13ms) +288ms ║║Comparison (enum) off is (string) on = false (1ms) +289ms ║║Cancelling condition #15's schedules... +289ms ║║Condition #15 evaluated false (5ms) +290ms ║║Cancelling condition #14's schedules... +291ms ║║Condition group #14 evaluated false (state changed) (7ms) +292ms ║║Cancelling statement #16's schedules... +340ms ║║Executed physical command [Family Room Accent Lights].off() (45ms) +341ms ║║Executed [Family Room Accent Lights].off (46ms) +366ms ║║Executed physical command [Family Room Main Lights].off() (23ms) +366ms ║║Executed [Family Room Main Lights].off (23ms) +368ms ║╚Execution stage complete. (105ms) +379ms ╚Event processed successfully (379ms) 11/10/2017, 11:18:53 AM +180ms +0ms ╔Received event [Home].time = 1510330734431 with a delay of -1252ms +164ms ║RunTime Analysis CS > 37ms > PS > 54ms > PE > 73ms > CE +181ms ║Runtime (39046 bytes) successfully initialized in 54ms (v0.2.0fd.20171105) (178ms) +182ms ║╔Execution stage started +233ms ║║Comparison (enum) on is (string) on = true (3ms) +236ms ║║Condition #15 evaluated true (16ms) +238ms ║║Condition group #14 evaluated true (state did not change) (19ms) +243ms ║║Cancelling statement #18's schedules... +249ms ║║Executed virtual command wait (0ms) +251ms ║║Requesting a wake up for Fri, Nov 10 2017 @ 11:18:58 AM EST (in 5.0s) +261ms ║╚Execution stage complete. (78ms) +264ms ║Setting up scheduled job for Fri, Nov 10 2017 @ 11:18:58 AM EST (in 4.989s) +286ms ╚Event processed successfully (286ms) 11/10/2017, 11:18:49 AM +226ms +1ms ╔Received event [Home].time = 1510330730603 with a delay of -1377ms +130ms ║RunTime Analysis CS > 37ms > PS > 51ms > PE > 42ms > CE +145ms ║Runtime (39046 bytes) successfully initialized in 51ms (v0.2.0fd.20171105) (143ms) +146ms ║╔Execution stage started +191ms ║║Comparison (enum) on is (string) on = true (2ms) +193ms ║║Condition #15 evaluated true (11ms) +195ms ║║Condition group #14 evaluated true (state did not change) (12ms) +198ms ║║Cancelling statement #18's schedules... +203ms ║║Executed virtual command wait (1ms) +204ms ║║Requesting a wake up for Fri, Nov 10 2017 @ 11:18:54 AM EST (in 5.0s) +210ms ║╚Execution stage complete. (64ms) +212ms ║Setting up scheduled job for Fri, Nov 10 2017 @ 11:18:54 AM EST (in 4.994s) +269ms ╚Event processed successfully (269ms) 11/10/2017, 11:18:45 AM +263ms +0ms ╔Received event [Home].time = 1510330726387 with a delay of -1125ms +285ms ║RunTime Analysis CS > 209ms > PS > 39ms > PE > 37ms > CE +297ms ║Runtime (39047 bytes) successfully initialized in 39ms (v0.2.0fd.20171105) (296ms) +298ms ║╔Execution stage started +328ms ║║Comparison (enum) on is (string) on = true (2ms) +330ms ║║Condition #15 evaluated true (11ms) +332ms ║║Condition group #14 evaluated true (state did not change) (12ms) +334ms ║║Cancelling statement #18's schedules... +338ms ║║Executed virtual command wait (0ms) +340ms ║║Requesting a wake up for Fri, Nov 10 2017 @ 11:18:50 AM EST (in 5.0s) +345ms ║╚Execution stage complete. (47ms) +347ms ║Setting up scheduled job for Fri, Nov 10 2017 @ 11:18:50 AM EST (in 4.994s) +424ms ╚Event processed successfully (424ms) 11/10/2017, 11:18:41 AM +212ms +1ms ╔Received event [Home].time = 1510330722383 with a delay of -1171ms +109ms ║RunTime Analysis CS > 19ms > PS > 47ms > PE > 42ms > CE +124ms ║Runtime (39046 bytes) successfully initialized in 47ms (v0.2.0fd.20171105) (122ms) +126ms ║╔Execution stage started +161ms ║║Comparison (enum) on is (string) on = true (2ms) +163ms ║║Condition #15 evaluated true (11ms) +165ms ║║Condition group #14 evaluated true (state did not change) (13ms) +168ms ║║Cancelling statement #18's schedules... +173ms ║║Executed virtual command wait (0ms) +174ms ║║Requesting a wake up for Fri, Nov 10 2017 @ 11:18:46 AM EST (in 5.0s) +181ms ║╚Execution stage complete. (56ms) +183ms ║Setting up scheduled job for Fri, Nov 10 2017 @ 11:18:46 AM EST (in 4.993s) +338ms ╚Event processed successfully (338ms) 11/10/2017, 11:18:37 AM +270ms +1ms ╔Received event [Master Bedroom Main Lights].switch = on with a delay of 59ms +71ms ║RunTime Analysis CS > 10ms > PS > 33ms > PE > 28ms > CE +80ms ║Runtime (39053 bytes) successfully initialized in 33ms (v0.2.0fd.20171105) (79ms) +81ms ║╔Execution stage started +88ms ║║Comparison (integer) 11 is_inside_of_range (integer) 9 .. (integer) 23 = true (1ms) +90ms ║║Condition #7 evaluated true (5ms) +94ms ║║Calculating (integer) 10 + (integer) 1 >> (integer) 11 +96ms ║║Comparison (integer) 11 is_less_than (integer) 11 = false (1ms) +97ms ║║Cancelling condition #22's schedules... +98ms ║║Condition #22 evaluated false (7ms) +98ms ║║Cancelling condition #1's schedules... +99ms ║║Condition group #1 evaluated false (state changed) (14ms) +104ms ║║Comparison (enum) on is (string) on = true (1ms) +105ms ║║Cancelling condition #15's schedules... +106ms ║║Condition #15 evaluated true (5ms) +107ms ║║Cancelling condition #14's schedules... +107ms ║║Condition group #14 evaluated true (state changed) (6ms) +109ms ║║Cancelling statement #18's schedules... +111ms ║║Executed virtual command wait (0ms) +112ms ║║Requesting a wake up for Fri, Nov 10 2017 @ 11:18:42 AM EST (in 5.0s) +116ms ║╚Execution stage complete. (34ms) +117ms ║Setting up scheduled job for Fri, Nov 10 2017 @ 11:18:42 AM EST (in 4.997s) +134ms ╚Event processed successfully (133ms) 11/10/2017, 11:18:36 AM +413ms +2ms ╔Received event [Master Bedroom Main Lights].switch = off with a delay of 62ms +114ms ║RunTime Analysis CS > 16ms > PS > 60ms > PE > 38ms > CE +125ms ║Runtime (39052 bytes) successfully initialized in 60ms (v0.2.0fd.20171105) (123ms) +126ms ║╔Execution stage started +136ms ║║Comparison (integer) 11 is_inside_of_range (integer) 9 .. (integer) 23 = true (2ms) +138ms ║║Condition #7 evaluated true (5ms) +142ms ║║Calculating (integer) 10 + (integer) 1 >> (integer) 11 +145ms ║║Comparison (integer) 10 is_less_than (integer) 11 = true (0ms) +146ms ║║Condition #22 evaluated true (8ms) +147ms ║║Condition group #1 evaluated true (state did not change) (15ms) +149ms ║║Cancelling statement #2's schedules... +224ms ║║Executed physical command [Family Room Accent Lights].setLevel([20]) (71ms) +225ms ║║Executed [Family Room Accent Lights].setLevel (72ms) +258ms ║║Executed physical command [Family Room Main Lights].setLevel([20]) (30ms) +259ms ║║Executed [Family Room Main Lights].setLevel (32ms) +261ms ║║Cancelling statement #4's schedules... +291ms ║║Executed physical command [Master Bedroom Main Lights].setLevel([80]) (24ms) +292ms ║║Executed [Master Bedroom Main Lights].setLevel (25ms) +296ms ║║Calculating (integer) 10 + (integer) 1 >> (integer) 11 +303ms ║║Executed virtual command [Master Bedroom Main Lights].setVariable (4ms) +311ms ║║Comparison (enum) off is (string) on = false (1ms) +312ms ║║Cancelling condition #15's schedules... +313ms ║║Condition #15 evaluated false (7ms) +314ms ║║Cancelling condition #14's schedules... +315ms ║║Condition group #14 evaluated false (state changed) (9ms) +317ms ║║Cancelling statement #16's schedules... +324ms ║║Skipped execution of physical command [Family Room Accent Lights].off([]) because it would make no change to the device. (5ms) +325ms ║║Executed [Family Room Accent Lights].off (6ms) +330ms ║║Skipped execution of physical command [Family Room Main Lights].off([]) because it would make no change to the device. (4ms) +331ms ║║Executed [Family Room Main Lights].off (4ms) +333ms ║╚Execution stage complete. (206ms) +342ms ╚Event processed successfully (342ms) 11/10/2017, 11:18:23 AM +210ms +1ms ╔Starting piston... (v0.2.0fd.20171105) +214ms ║╔Subscribing to devices... +247ms ║║Subscribing to Master Bedroom Main Lights.switch... +359ms ║║Subscribing to Family Room Accent Lights... +360ms ║║Subscribing to Family Room Main Lights... +361ms ║╚Finished subscribing (148ms) +384ms ║Comparison (integer) 11 is_inside_of_range (integer) 9 .. (integer) 23 = true (2ms) +391ms ║Calculating (integer) 10 + (integer) 1 >> (integer) 11 +393ms ║Comparison (integer) 10 is_less_than (integer) 11 = true (1ms) +415ms ║Comparison (enum) on is (string) on = true (1ms) +437ms ╚Piston successfully started (436ms)


#2

I have not looked at the piston in detail but the first thing I noticed is your WHILE only has the Wait 5 secs in it and nothign else. If you want White Bulb 2 and 3 to turn off, they need to be inside the WHILE, you can probably drag it in.


#3

Thank you for the quick response! At the top of the while statement it checks to see if Bulb 5 has been turned off; I just put a wait statement in there so it didn’t run constantly - only checking the status of the bulb every 5 seconds.

Once that bulb is turned off, I want bulbs 2 and 3 to turn off. Those commands come immediately after the while loop, so I thought they’d be executed once the bulb 5 switch is determined to be off.

Based on the log details, my understanding of a while loop is incorrect. I’ve also tried other loops but end up with the same result.


#4

Try it with something simple like this… the piston will subscribe to events from Bulb 5, so when it sees it turn off, it should turn off bulb 2 and 3 also without needing a loop. That is if I understood what you need correctly :smiley:

if
	White Bulb 5 switch is off
	with
		White Bulb 2 and 3
		do
			Turn off;
	end with;
end if;

#5

That makes sense - I didn’t think about it subscribing to events, so I thought I had to keep polling the switch until it changed.

The statement you wrote does what I want it to do, the only catch is I only want that executed if White Bulb 5 is on because the if statement above turned it on.

Is there a way to do that? Maybe I can create another variable like I did for “last day” so it only executes once per day…

Thanks so much for your help!


#6

I use the same lastDay variable like yours except my comparison is

if
   {$day} is different than {lastDay}

Also, in the above code I used if White Bulb 5 switch is off because I thought you wanted to turn off the other two bulbs when Bulb 5 is off/turns off.


#7

That’s how my original code is, too. I kept having to monkey with it because when I’d test it, the variables would get set and I couldn’t test it again. :slight_smile:

Your If statement suggestion, paired with a new Boolean “did this routine turn on Bulb 5” accomplishes what I’ve been struggling to figure out using repeat and while loops. I had it in my head that only the first If statement in a piston would subscribe to events… going to see what else I can come up with now that those gates are open!

Thanks again for your help!