Piston not executing correctly by "out of sync" arguments and variables?


#1

Hi everyone. I have a piston (SWITCH) that is executed by a virtual switch, it sets a global boolean variable ALL_ON as TRUE and then executes another piston (MOTORS) sending some arguments.

This second piston (MOTORS) is not subscribed to events (it is only executed by the SWITCH piston) and has a restriction that only completes its actions if ALL_ON variable is TRUE. (This is because it has a for loop and I don’t want the loop to continue its course of action if I turn off the switch in the middle of the actions, which take around 1 minute in total).

I’ve been having a problem and enabled the logs to analyze it. It seems that sometimes when the MOTORS piston is started, the first comparison it makes [Comparison (boolean) false is (boolean) true = false (1ms)] is resulting false, and this line changes in the logs when the piston works, so I’m assuming that at the time this piston reads the global variable it hasn’t been yet updated. Does this make sense?
I’ve been told here before that global variables are “slower” than arguments or local variables, could this be the problem?
If so, would I need to add a WAIT in the SWITCH piston, between the ALL_ON variable being written and the MOTOR piston being executed so that I leave some spare time for the ALL_ON variable to be updated?

I want confirmation if this makes sense and if so, how much of a delay would you add? This piston works fine 90% of the times.

I went with this global variable because I tried other methods to make the piston “stop” in the middle of the action and they didn’t work. This method works fine most of the times but has this only problem, so I’d prefer to stick with this, but anyway all comments are appreciated :slight_smile:

Thanks,
Rodrigo.


#2

Post images of your pistons and logs.


#3

I hate to be the bearer of bad news, but with your current structure, it will not be 100%.

Let me break it down a bit.

Switch Piston

Set @global = TRUE
Execute Motors Piston

Motors Piston

IF @global is TRUE, then do stuff


The issue is because a global variable is not written until the very last line of code has executed. This means, that the “Switch” piston sends the “Execute piston” command before it tries to change the variable. If the “Motors” piston is quick to respond, it will see the old @global… If “Motors” is slow to respond, it may see the new @global.

Adding a WAIT to the “Switch” piston will not change that at all.


One work-around is to remove the “Execute piston” from the “Switch” piston, and change the trigger in Motors piston to execute when the global changes. IE:

Switch Piston

Set @global = TRUE

Motors Piston

IF @global CHANGES TO TRUE, then do stuff


Alternatively, you can create a new piston that sits between the two… IE:

Switch Piston

Set @global = TRUE
Execute Middle Piston

Middle Piston

WAIT 2-3 seconds
Execute Motors Piston

Motors Piston

IF @global is TRUE, then do stuff


#4

4/19/2020, 11:41:55 AM +286ms
+1ms ╔Received event [All Blinds].switch = off with a delay of 59ms
+90ms ║RunTime Analysis CS > 17ms > PS > 22ms > PE > 50ms > CE
+92ms ║Runtime (45074 bytes) successfully initialized in 22ms (v0.3.110.20191009) (90ms)
+93ms ║╔Execution stage started
+101ms ║║Condition #2 evaluated false (4ms)
+102ms ║║Condition group #1 evaluated false (state did not change) (6ms)
+109ms ║║Condition #35 evaluated false (5ms)
+110ms ║║Condition group #34 evaluated false (state did not change) (6ms)
+114ms ║║Comparison (enum) off changes = true (0ms)
+115ms ║║Condition #24 evaluated true (3ms)
+116ms ║║Condition group #23 evaluated true (state did not change) (4ms)
+122ms ║║Comparison (enum) off is (string) on = false (2ms)
+123ms ║║Cancelling condition #67’s schedules…
+124ms ║║Condition #67 evaluated false (6ms)
+125ms ║║Cancelling condition #66’s schedules…
+126ms ║║Condition group #66 evaluated false (state changed) (8ms)
+128ms ║║Cancelling statement #68’s schedules…
+131ms ║║Executed virtual command setVariable (1ms)
+142ms ║║Executed virtual command executePiston (7ms)
+145ms ║╚Execution stage complete. (53ms)
+146ms ╚Event processed successfully (146ms)
4/19/2020, 11:40:50 AM +821ms
+2ms ╔Received event [All Blinds].switch = on with a delay of 107ms
+116ms ║RunTime Analysis CS > 26ms > PS > 34ms > PE > 55ms > CE
+118ms ║Runtime (45079 bytes) successfully initialized in 34ms (v0.3.110.20191009) (116ms)
+119ms ║╔Execution stage started
+127ms ║║Condition #2 evaluated false (5ms)
+128ms ║║Condition group #1 evaluated false (state did not change) (6ms)
+134ms ║║Condition #35 evaluated false (4ms)
+135ms ║║Condition group #34 evaluated false (state did not change) (5ms)
+139ms ║║Comparison (enum) on changes = true (0ms)
+140ms ║║Condition #24 evaluated true (3ms)
+141ms ║║Condition group #23 evaluated true (state did not change) (4ms)
+146ms ║║Comparison (enum) on is (string) on = true (1ms)
+147ms ║║Cancelling condition #67’s schedules…
+148ms ║║Condition #67 evaluated true (5ms)
+149ms ║║Cancelling condition #66’s schedules…
+150ms ║║Condition group #66 evaluated true (state changed) (7ms)
+152ms ║║Cancelling statement #25’s schedules…
+157ms ║║Executed virtual command setVariable (3ms)
+166ms ║║Executed virtual command setVariable (3ms)
+169ms ║║Executed virtual command setVariable (1ms)
+182ms ║║Executed virtual command executePiston (10ms)
+185ms ║╚Execution stage complete. (66ms)
+185ms ╚Event processed successfully (185ms)
4/19/2020, 11:33:34 AM +836ms
+2ms ╔Received event [All Blinds].switch = off with a delay of 100ms
+112ms ║RunTime Analysis CS > 28ms > PS > 26ms > PE > 57ms > CE
+114ms ║Runtime (45076 bytes) successfully initialized in 26ms (v0.3.110.20191009) (112ms)
+115ms ║╔Execution stage started
+124ms ║║Condition #2 evaluated false (5ms)
+125ms ║║Condition group #1 evaluated false (state did not change) (6ms)
+132ms ║║Condition #35 evaluated false (5ms)
+133ms ║║Condition group #34 evaluated false (state did not change) (6ms)
+138ms ║║Comparison (enum) off changes = true (0ms)
+139ms ║║Condition #24 evaluated true (3ms)
+140ms ║║Condition group #23 evaluated true (state did not change) (4ms)
+145ms ║║Comparison (enum) off is (string) on = false (1ms)
+146ms ║║Cancelling condition #67’s schedules…
+147ms ║║Condition #67 evaluated false (5ms)
+148ms ║║Cancelling condition #66’s schedules…
+149ms ║║Condition group #66 evaluated false (state changed) (8ms)
+151ms ║║Cancelling statement #68’s schedules…
+155ms ║║Executed virtual command setVariable (1ms)
+168ms ║║Executed virtual command executePiston (10ms)
+171ms ║╚Execution stage complete. (55ms)
+172ms ╚Event processed successfully (171ms)
4/19/2020, 11:33:34 AM +284ms
+0ms ╔Received event [All Blinds].switch = on with a delay of 68ms
+91ms ║RunTime Analysis CS > 20ms > PS > 18ms > PE > 53ms > CE
+93ms ║Runtime (45079 bytes) successfully initialized in 18ms (v0.3.110.20191009) (92ms)
+94ms ║╔Execution stage started
+104ms ║║Cancelling condition #2’s schedules…
+105ms ║║Condition #2 evaluated false (6ms)
+106ms ║║Cancelling condition #1’s schedules…
+106ms ║║Condition group #1 evaluated false (state changed) (8ms)
+113ms ║║Condition #35 evaluated false (4ms)
+114ms ║║Condition group #34 evaluated false (state did not change) (6ms)
+118ms ║║Comparison (enum) on changes = true (0ms)
+119ms ║║Cancelling condition #24’s schedules…
+120ms ║║Condition #24 evaluated true (4ms)
+121ms ║║Cancelling condition #23’s schedules…
+122ms ║║Condition group #23 evaluated true (state changed) (6ms)
+128ms ║║Comparison (enum) on is (string) on = true (2ms)
+129ms ║║Cancelling condition #67’s schedules…
+130ms ║║Condition #67 evaluated true (6ms)
+131ms ║║Cancelling condition #66’s schedules…
+132ms ║║Condition group #66 evaluated true (state changed) (8ms)
+134ms ║║Cancelling statement #25’s schedules…
+138ms ║║Executed virtual command setVariable (2ms)
+145ms ║║Executed virtual command setVariable (2ms)
+149ms ║║Executed virtual command setVariable (1ms)
+160ms ║║Executed virtual command executePiston (7ms)
+163ms ║╚Execution stage complete. (68ms)
+164ms ╚Event processed successfully (163ms)

4/19/2020, 11:41:55 AM +518ms
+1ms ╔Received event [Home].execute = :b9b5f43186ee0de9fcf1ed935f04c30d: with a delay of 95ms
+104ms ║RunTime Analysis CS > 22ms > PS > 25ms > PE > 57ms > CE
+107ms ║Runtime (44391 bytes) successfully initialized in 25ms (v0.3.110.20191009) (105ms)
+108ms ║╔Execution stage started
+117ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+124ms ║║Comparison (boolean) false is (boolean) true = false (2ms)
+126ms ║║Cancelling condition #63’s schedules…
+126ms ║║Condition #63 evaluated false (5ms)
+128ms ║║Cancelling condition #64’s schedules…
+128ms ║║Condition group #64 evaluated false (state changed) (8ms)
+135ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+142ms ║║Comparison (boolean) false is (boolean) true = false (2ms)
+143ms ║║Condition #63 evaluated false (4ms)
+144ms ║║Condition group #64 evaluated false (state did not change) (6ms)
+151ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+158ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+160ms ║║Condition #63 evaluated false (4ms)
+161ms ║║Condition group #64 evaluated false (state did not change) (6ms)
+164ms ║║Cancelling statement #16’s schedules…
+170ms ║║Skipped execution of physical command [All Blinds].off([]) because it would make no change to the device. (2ms)
+171ms ║║Executed [All Blinds].off (4ms)
+176ms ║║Skipped execution of physical command [Close All Blinds].off([]) because it would make no change to the device. (2ms)
+177ms ║║Executed [Close All Blinds].off (4ms)
+181ms ║║Skipped execution of physical command [Night Blinds].off([]) because it would make no change to the device. (2ms)
+182ms ║║Executed [Night Blinds].off (3ms)
+184ms ║╚Execution stage complete. (77ms)
+185ms ╚Event processed successfully (185ms)
4/19/2020, 11:41:54 AM +280ms
+1ms ╔Received event [Home].time = 1587307315297 with a delay of -1017ms
+133ms ║RunTime Analysis CS > 23ms > PS > 39ms > PE > 71ms > CE
+137ms ║Runtime (44369 bytes) successfully initialized in 39ms (v0.3.110.20191009) (136ms)
+138ms ║╔Execution stage started
+156ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+202ms ║║Cancelling statement #16’s schedules…
+963ms ║║Executed physical command [All Blinds].off() (756ms)
+964ms ║║Executed [All Blinds].off (758ms)
+971ms ║║Skipped execution of physical command [Close All Blinds].off([]) because it would make no change to the device. (3ms)
+971ms ║║Executed [Close All Blinds].off (4ms)
+975ms ║║Skipped execution of physical command [Night Blinds].off([]) because it would make no change to the device. (2ms)
+976ms ║║Executed [Night Blinds].off (3ms)
+977ms ║╚Execution stage complete. (839ms)
+979ms ╚Event processed successfully (978ms)
4/19/2020, 11:41:33 AM +197ms
+0ms ╔Received event [Home].time = 1587307294715 with a delay of -1518ms
+108ms ║RunTime Analysis CS > 21ms > PS > 37ms > PE > 50ms > CE
+111ms ║Runtime (44372 bytes) successfully initialized in 37ms (v0.3.110.20191009) (110ms)
+112ms ║╔Execution stage started
+123ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+156ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+163ms ║║Comparison (boolean) true is (boolean) true = true (1ms)
+165ms ║║Condition #63 evaluated true (5ms)
+165ms ║║Condition group #64 evaluated true (state did not change) (6ms)
+167ms ║║Cancelling statement #19’s schedules…
+176ms ║║Executed virtual command setVariable (3ms)
+180ms ║║Executed virtual command wait (1ms)
+181ms ║║Waiting for 100ms
+294ms ║║Comparison (enum) off is (string) off = true (2ms)
+295ms ║║Cancelling condition #18’s schedules…
+296ms ║║Condition #18 evaluated true (12ms)
+297ms ║║Cancelling condition #12’s schedules…
+298ms ║║Condition group #12 evaluated true (state changed) (15ms)
+300ms ║║Cancelling statement #14’s schedules…
+306ms ║║Executed virtual command setVariable (3ms)
+309ms ║║Cancelling statement #33’s schedules…
+319ms ║║Calculating (integer) 99 - (integer) 1 >> (integer) 98
+324ms ║║Calculating (decimal) 21.0 / (decimal) 100.0 >> (decimal) 0.21
+327ms ║║Calculating (decimal) 0.21 * (decimal) 98.0 >> (decimal) 20.58
+329ms ║║Calculating (decimal) 20.58 * (decimal) 1000.0 >> (decimal) 20580.0
+332ms ║║Calculating (decimal) 20580.0 + (decimal) 350.0 >> (decimal) 20930.0
+336ms ║║Executed virtual command setVariable (3ms)
+343ms ║║Comparison (integer) 99 is_less_than_or_equal_to (integer) 1 = false (2ms)
+344ms ║║Condition #29 evaluated false (5ms)
+345ms ║║Condition group #28 evaluated false (state did not change) (7ms)
+347ms ║║Cancelling statement #9’s schedules…
+1163ms ║║Executed physical command [3 Bedroom].setLevel([99]) (811ms)
+1164ms ║║Executed [3 Bedroom].setLevel (812ms)
+1168ms ║║Executed virtual command [3 Bedroom].wait (1ms)
+1169ms ║║Requesting a wake up for Sun, Apr 19 2020 @ 11:41:55 AM ART (in 20.93s)
+1174ms ║╚Execution stage complete. (1063ms)
+1175ms ║Setting up scheduled job for Sun, Apr 19 2020 @ 11:41:55 AM ART (in 20.925s)
+1182ms ╚Event processed successfully (1182ms)
4/19/2020, 11:41:11 AM +302ms
+0ms ╔Received event [Home].time = 1587307272447 with a delay of -1146ms
+184ms ║RunTime Analysis CS > 91ms > PS > 42ms > PE > 52ms > CE
+187ms ║Runtime (44369 bytes) successfully initialized in 42ms (v0.3.110.20191009) (186ms)
+188ms ║╔Execution stage started
+202ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+275ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+283ms ║║Comparison (boolean) true is (boolean) true = true (2ms)
+284ms ║║Condition #63 evaluated true (5ms)
+285ms ║║Condition group #64 evaluated true (state did not change) (6ms)
+288ms ║║Cancelling statement #19’s schedules…
+301ms ║║Executed virtual command setVariable (3ms)
+305ms ║║Executed virtual command wait (0ms)
+312ms ║║Waiting for 100ms
+425ms ║║Comparison (enum) on is (string) off = false (2ms)
+427ms ║║Cancelling condition #18’s schedules…
+428ms ║║Condition #18 evaluated false (13ms)
+430ms ║║Cancelling condition #12’s schedules…
+431ms ║║Condition group #12 evaluated false (state changed) (15ms)
+434ms ║║Cancelling statement #6’s schedules…
+444ms ║║Executed virtual command setVariable (3ms)
+447ms ║║Cancelling statement #33’s schedules…
+460ms ║║Calculating (integer) 99 - (integer) 1 >> (integer) 98
+467ms ║║Calculating (decimal) 21.0 / (decimal) 100.0 >> (decimal) 0.21
+471ms ║║Calculating (decimal) 0.21 * (decimal) 98.0 >> (decimal) 20.58
+474ms ║║Calculating (decimal) 20.58 * (decimal) 1000.0 >> (decimal) 20580.0
+478ms ║║Calculating (decimal) 20580.0 + (decimal) 350.0 >> (decimal) 20930.0
+483ms ║║Executed virtual command setVariable (3ms)
+492ms ║║Comparison (integer) 99 is_less_than_or_equal_to (integer) 1 = false (2ms)
+494ms ║║Condition #29 evaluated false (7ms)
+495ms ║║Condition group #28 evaluated false (state did not change) (9ms)
+498ms ║║Cancelling statement #9’s schedules…
+2474ms ║║Executed physical command [2 Bathroom].setLevel([99]) (1970ms)
+2476ms ║║Executed [2 Bathroom].setLevel (1972ms)
+2481ms ║║Executed virtual command [2 Bathroom].wait (1ms)
+2482ms ║║Requesting a wake up for Sun, Apr 19 2020 @ 11:41:34 AM ART (in 20.93s)
+2490ms ║╚Execution stage complete. (2302ms)
+2492ms ║Setting up scheduled job for Sun, Apr 19 2020 @ 11:41:34 AM ART (in 20.922s)
+2500ms ╚Event processed successfully (2500ms)
4/19/2020, 11:40:51 AM +86ms
+1ms ╔Received event [Home].execute = :b9b5f43186ee0de9fcf1ed935f04c30d: with a delay of 91ms
+109ms ║RunTime Analysis CS > 23ms > PS > 28ms > PE > 58ms > CE
+112ms ║Runtime (44391 bytes) successfully initialized in 28ms (v0.3.110.20191009) (110ms)
+112ms ║╔Execution stage started
+122ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+129ms ║║Comparison (boolean) true is (boolean) true = true (2ms)
+130ms ║║Cancelling condition #63’s schedules…
+131ms ║║Condition #63 evaluated true (5ms)
+132ms ║║Cancelling condition #64’s schedules…
+133ms ║║Condition group #64 evaluated true (state changed) (8ms)
+135ms ║║Cancelling statement #19’s schedules…
+144ms ║║Executed virtual command setVariable (3ms)
+147ms ║║Executed virtual command wait (0ms)
+148ms ║║Waiting for 100ms
+259ms ║║Comparison (enum) off is (string) off = true (2ms)
+260ms ║║Cancelling condition #18’s schedules…
+261ms ║║Condition #18 evaluated true (10ms)
+262ms ║║Cancelling condition #12’s schedules…
+263ms ║║Condition group #12 evaluated true (state changed) (12ms)
+266ms ║║Cancelling statement #14’s schedules…
+271ms ║║Executed virtual command setVariable (3ms)
+274ms ║║Cancelling statement #33’s schedules…
+283ms ║║Calculating (integer) 99 - (integer) 1 >> (integer) 98
+288ms ║║Calculating (decimal) 21.0 / (decimal) 100.0 >> (decimal) 0.21
+291ms ║║Calculating (decimal) 0.21 * (decimal) 98.0 >> (decimal) 20.58
+294ms ║║Calculating (decimal) 20.58 * (decimal) 1000.0 >> (decimal) 20580.0
+297ms ║║Calculating (decimal) 20580.0 + (decimal) 350.0 >> (decimal) 20930.0
+301ms ║║Executed virtual command setVariable (2ms)
+307ms ║║Comparison (integer) 99 is_less_than_or_equal_to (integer) 1 = false (1ms)
+309ms ║║Cancelling condition #29’s schedules…
+310ms ║║Condition #29 evaluated false (7ms)
+311ms ║║Cancelling condition #28’s schedules…
+312ms ║║Condition group #28 evaluated false (state changed) (9ms)
+314ms ║║Cancelling statement #9’s schedules…
+425ms ║║Executed physical command [1 Living].setLevel([99]) (105ms)
+426ms ║║Executed [1 Living].setLevel (107ms)
+429ms ║║Executed virtual command [1 Living].wait (1ms)
+430ms ║║Requesting a wake up for Sun, Apr 19 2020 @ 11:41:12 AM ART (in 20.93s)
+435ms ║╚Execution stage complete. (323ms)
+437ms ║Setting up scheduled job for Sun, Apr 19 2020 @ 11:41:12 AM ART (in 20.925s)
+444ms ╚Event processed successfully (443ms)
4/19/2020, 11:33:35 AM +120ms
+0ms ╔Received event [Home].execute = :b9b5f43186ee0de9fcf1ed935f04c30d: with a delay of 123ms
+131ms ║RunTime Analysis CS > 41ms > PS > 33ms > PE > 57ms > CE
+133ms ║Runtime (44393 bytes) successfully initialized in 33ms (v0.3.110.20191009) (132ms)
+134ms ║╔Execution stage started
+143ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+150ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+151ms ║║Condition #63 evaluated false (5ms)
+152ms ║║Condition group #64 evaluated false (state did not change) (6ms)
+159ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+165ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+166ms ║║Condition #63 evaluated false (4ms)
+167ms ║║Condition group #64 evaluated false (state did not change) (5ms)
+174ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+180ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+181ms ║║Condition #63 evaluated false (4ms)
+182ms ║║Condition group #64 evaluated false (state did not change) (5ms)
+185ms ║║Cancelling statement #16’s schedules…
+191ms ║║Skipped execution of physical command [All Blinds].off([]) because it would make no change to the device. (2ms)
+192ms ║║Executed [All Blinds].off (3ms)
+197ms ║║Skipped execution of physical command [Close All Blinds].off([]) because it would make no change to the device. (3ms)
+198ms ║║Executed [Close All Blinds].off (4ms)
+203ms ║║Skipped execution of physical command [Night Blinds].off([]) because it would make no change to the device. (1ms)
+203ms ║║Executed [Night Blinds].off (3ms)
+205ms ║╚Execution stage complete. (72ms)
+206ms ╚Event processed successfully (206ms)
4/19/2020, 11:33:34 AM +498ms
+0ms ╔Received event [Home].execute = :b9b5f43186ee0de9fcf1ed935f04c30d: with a delay of 59ms
+87ms ║RunTime Analysis CS > 18ms > PS > 18ms > PE > 52ms > CE
+90ms ║Runtime (44391 bytes) successfully initialized in 18ms (v0.3.110.20191009) (89ms)
+91ms ║╔Execution stage started
+100ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+107ms ║║Comparison (boolean) false is (boolean) true = false (2ms)
+108ms ║║Condition #63 evaluated false (4ms)
+109ms ║║Condition group #64 evaluated false (state did not change) (6ms)
+116ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+122ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+124ms ║║Condition #63 evaluated false (5ms)
+125ms ║║Condition group #64 evaluated false (state did not change) (6ms)
+132ms ║║Calculating (integer) 3 - (integer) 1 >> (integer) 2
+138ms ║║Comparison (boolean) false is (boolean) true = false (1ms)
+139ms ║║Condition #63 evaluated false (4ms)
+140ms ║║Condition group #64 evaluated false (state did not change) (5ms)
+143ms ║║Cancelling statement #16’s schedules…
+249ms ║║Executed physical command [All Blinds].off() (103ms)
+250ms ║║Executed [All Blinds].off (105ms)
+255ms ║║Skipped execution of physical command [Close All Blinds].off([]) because it would make no change to the device. (3ms)
+256ms ║║Executed [Close All Blinds].off (3ms)
+260ms ║║Skipped execution of physical command [Night Blinds].off([]) because it would make no change to the device. (3ms)
+261ms ║║Executed [Night Blinds].off (4ms)
+262ms ║╚Execution stage complete. (172ms)
+263ms ╚Event processed successfully (263ms)


#5

I think you told me this before, but then how is this piston working 90% of the times? I’m sure that when it works, the variable is false before starting the manual action. If it is how you say, wouldn’t adding a WAIT before the start of the MOTORS piston help?

I tried using pistons based on triggers and it failed much more than now. One of my problems was that virtual switches failed to turn off after ending the process so when another piston would try to start this action by turning the switch on, the action wouldn’t happen. I tried many different options and this is the one that works best for now, so I’d prefer to adjust the part that is failing here and not go back to the old method that was more buggy, if it is possible.

R.


#6

This means that the “Motors” piston is usually slow to respond in your house.
On the occasions where “Motors” is quick and snappy, the @global is not set yet.

In other words, your success means that your house is running ssllooww…
Your failures means that your house is running like a champ!

Bleh!


Adding a WAIT still leaves us with the same logic:
“a global variable is not written until the very last line of code has executed”

Now, if you can find a way to set the global, but NOT execute the other piston… When the “Switch” piston runs again, the @global will already be set, so then it can execute the other piston…

But honestly, my two suggestions above are much more reliable, without gambling or creating loops.


The issue here has nothing to do with "using pistons based on triggers"…
That issue was your code did not finish properly.


#7

I just created this piston and I see the global variable changing way before the last line is executed. Am I understanding this correctly?


#8

See:


#9

Could you please care to explain this quote?

Becuase the way I understand it (and I might be wrong) I don’t see it being true. I have used global variables a lot, I’ve been told they were slow and I experienced that, so I switched my variables to arguments and now it all runs much better, but I still see that global variables are written before the “very last line of code has executed”, so I’m not understanding the meaning of “the very last line of code has executed”.

I just did a test and it seems that the variable has been updated before the “very last line of code has executed”, so I’m not getting what you mean, sorry. I’m really trying to understand this part.

Thanks,
R.


#10

Essentially, when a piston first begins, it takes a “snapshot” of the current @globals. Then it follows along the code, line by line, but any updates to the @globals are not processed until the piston has completed it’s tasks.

Now, there may be loops or some other trick to force the piston to start all over at the top… Thereby grabbing a fresh “snapshot” of the @globals… but I was not recommending these methods.

Feel free to experiment.


#11

Okay, thanks :slight_smile:
I will try converting this variable into an argument, I think it will work better.

R.


#12

Global variables are read into a cache when an instance of a piston starts up, the cached versions are used by the piston, then any changes made to the globals are written from the cache when the piston is having a tidy up before it exits. You hit problems if your executed piston gets to caching the globals before the calling piston gets around to writing then. It is essentially a race condition.

If you call a ‘wait’ of at least five seconds after setting the global variable the piston will implement it by scheduling a wakeup and exiting. That exit will cause the global variable to be written. So you can execute a piston after a wait of that length and you should be OK.

You could alternatively add a wait of greater than five seconds to the start of your executed piston as when it wakes up it would cache the global variable again. This would defeat the race condition most of the time but there is always the possiblity of freak delay in the calling piston. So this is is more of a fudge.


#13

I was going to suggest this option. Simpler and no reliance on the whims of global variables ;).


#14

If “ALL_ON as TRUE/FALSE” is not needed in other pistons, then I agree.

If it is needed elsewhere, I would push on thru understanding the way @globals act/react, or shift to a Simulated Switch to represent True/False. (the best of both worlds… instantly visible from other pistons)


#15

I was originally searching for a method to stop the FOR loop from another piston. I wanted to avoid using a global boolean variable or a virtual switch to execute the MOTORS piston (because I had problems with that) and I got the recommendation to use arguments, which I didn’t know how to use at the time, so I was experimenting.
I needed the SWITCH piston to start the MOTORS piston but then if the switch was manually flipped off, I want the MOTORS to stop. This worked fine when I used a global variable or switch to start the MOTORS piston but sometimes the other variables wouldn’t load in time, this is why I wanted to try arguments.

The first thing I tried after changing the method to execute with arguments, was setting a global variable, and then when the switch is off it would set that variable to false, but that didn’t work as expected because of what you guys told me, that the piston reads the variable once when it is executed, so if it changes in the middle the piston won’t notice.
So then I tried re-executing the piston after setting the variable to false, it worked, and I admit I was kind of lazy and I could have moved that variable to arguments at that point (but it still worked much better than all my previous tests).
Clearly using an argument and re-executing the piston is much better than using a global variable, but the global variable worked fine for a long time (I use this piston at least 5 times a day and it failed maybe once a week).

I still am not sure if re-executing the piston with a “false” argument that prevents it from following through is the best way to achieve this. Can anyone recommend a better way to do this?

The main objectives here are:

There are three switches. A dimmer switch that sets the % of the blinds (they open sequentially, in order 1, 2, 3), a “evening” switch that closes two of the three blinds (in order 3, 2) and a “close all” switch that closes all the blinds (in order 3, 2, 1). Currently I have a SWITCH piston that depending on which switch is turned on, sets arguments for the order and blinds position and executes the MOTORS piston.

The MOTORS piston starts its actions (which take around a minute to complete), when the actions complete it turns off all of these switches (only one would be ON actually, but I set its last action to turn off whichever switch is on).

If a switch is manually turned off while the action is running, I want the action to stop. In reality, the current blind that is moving will keep moving until it reaches its set % but I don’t care about this, I just want the rest of the action to be canceled.

Currently, every time I turn on a switch the SWITCH piston is called, it sets the variables and executes the MOTORS piston sending those variables as arguments, and when the MOTORS piston ends it turns off the active switch, so the SWITCH piston is called again and the MOTORS piston is called one last time. I can see this is not ideal, but it has been working for me. Can anyone recommend a different approach to this? Is it really bad, do I need to improve this for some reason? :slight_smile:

I really don’t want to change the core of the MOTORS piston (the FOR loop and the arguments that are being sent).

Thanks everyone!
Rodrigo.