Piston executes from top to bottom?

every
execution
timer

#6

Here’s a question:

In the following piston, will the outlet turn on if the switch is off? Meaning, will the piston move on to run the second if statement, if the first is false?

IF switch is on
THEN set bulb to 100%
end if

IF door is open
THEN turn on outlet
end if


#7

Switch is OFF, Door is open

8/31/2017, 4:31:21 PM +176ms
+354ms	║Turning on outlet

Switch is ON, Door is open

8/31/2017, 4:31:08 PM +330ms
+233ms	║Setting to 100
+268ms	║Turning on outlet

Both devices were subscribed to, events from either one causes the IF statements to get evaluated.


#8

So you can put as many IF statements as you like, and have them all simultaneously evaluated, as long as each one has an exit (end if). Is that correct?


#9

“Simultaneous” requires one of the following two:

a) they have no waiting tasks (wait, fade, etc)
or
b) if they have a waiting task, they are set to run async (meaning next statements won’t wait)

But yeah, you can add as many statements (be them IF, FOR, WHILE, TIMER, ACTION, etc) in as many depth levels as you want. Open world. You can have IF after IF, IF inside IF, any combination you can think of.

if
   condition1
then
   do something;
   wait 2 minutes;
   do something else;
end if;

if
   condition2
then
   do something more;
end if;

In the example above, if condition1 is true, something will be done and then 2 minute wait starts. After two minutes, something else is done, then if condition2 is true at that time, something more will be done. Note that the second if waited on the first if to finish.

If condition1 is false, but condition2 is true, something more is done immediately.

Setting the first if as async will change the behavior:

If the first condition is true, something will be done, then a wait for 2 minutes is initiated. At the same time, if condition2 is true, something more will be done. Two minutes later, something else will be done as well. Note that the second if got executed without waiting for the first if to finish. That is what async does, they run in “parallel”.


#10

This clears a lot up. So the waiting tasks are the only things that will really hold up a piston. Without any waiting task, all conditions run parallel by default. Waiting tasks seem to be a unique thing here, sort of changing the rules for everything after it.


#11

SmartApps are single-threaded so nothing in them really runs in “parallel”. They are still executed in a sequence, top-to-bottom, but so rapidly they seem “parallel”. Anything waiting complicates the execution sequence, you are right.


#12

If I understand this correctly, if there are two IF statements either can fire the piston although both would be evaluated from the top down. Example:

if
    condition1 (based on presence)
then 
    set variable mode = 1;
else 
    set variable mode = 0;
end if;

if
    condition2 (based on time of day)
then
    set variable mode = mode + 2;
end if;

So if condition2 fires because of the time of day, does condition1 get evaluated also? I’m trying to set mode to 0 to 3 based on a combination of conditions.


#13

yes it is. if you dont want that, there is a way to avoid it where the piston still executes from top to bottom but not all of the conditions get evaluated.


#14

Perfect. Thanks for the prompt answer.


#15

So anytime any IF statement in a piston evaluates to true, by default, the entire piston runs top to bottom? I think that explains why I’m having trouble with a couple of my pistons. Thanks!


#16

But if i want to use a “and”.
Meaing that i only want the second if to evaluate if the first one is true.
But i want the third to start no matter what.


#17

I have a piston for my backyard patio lighting that evaluates for several triggers, a few of which are “every day at $sunset”, “every day at $midnight” and “every day at $sunrise”. How can I force the piston to run from top to bottom given these timer events?

Thanks


#18

you cant with the every statement. use if time happens ... instead.


#19

I looked around for this information for a long time but didn’t find it. I am in the infancy of developing pistons and hope to use many of the examples. this tool is extremely powerful.

OK, so what i am reading on another reply is that a piston will be checked for execution based on what is subscribed to in the piston. It can also set the subscription to ‘never’ for my definition of ‘inactive’ on my initial post.

Thanks folks for this great information.


#20

I’m not sure what other post you are referring to…but if you set a piston to never subscribe to events (as in all of them) then it will just sit there doing noting. If that’s what you want, then you should just Pause the piston instead of unsubscribing from all the events.

It is valid to tell a piston to not subscribe to certain Events if you want to use them as conditions rather than as Triggers (there’s another thread which details the use and difference between those).


#21

think you meant active and inactive as in this post:

if yes you are right. if you set the piston to never subscribe it will only be executed when called by another piston or some other way. remember not to pause the piston though. paused pistons dont get executed even when executed by being called.


#22

Apologies for bumping this, but it topical. How can I prevent an entire piston from being evaluated when some condition becomes true?


#23

the entire piston is always evaluated but off course this evaluation honors all conditions in the piston. so if sections of the piston should not be evaluated put those blocks of code under conditions which will cause them to be skipped based on those conditions.


#24

That’s what I was afraid of. I guess I’ll break the stuff out into another piston. Thanks.


#25

or make those blocks conditional.