Multiple if's in a single piston. What does "runs top to bottom" mean?


#1

I’m trying to understand how pistons fire, and on another thread, I was told that pistons run top to bottom when a trigger fires.

WebcoRE looks a lot like procedural programming, but the top level “If” blocks seem to behave like event handlers though.

In the examples, there are a number of pistons that have multiple top-level Ifs. https://wiki.webcore.co/Samples See the Advanced Motion Piston for example.

When one of those triggers fires off, does it run the whole piston code and check all the ifs, etc.? Does the piston code just run in a continuous loop checking the conditions?

Attached is my garage monitor piston. I’m checking for a few different things. I’m trying to figure out whether I need to split it into 3 separate pistons. But before I do I’m trying to patch up some holes in my understand.

Thanks for your patience!


#2

Your trigger in this piston is line 55, where the orange lightning bolt is in the margin. Generally, I like to put my triggers at the TOP of the piston. So your piston will only execute when the temperature drops below 50. At that time, your piston will run from top to bottom. Then your piston will only run again when the temperature drops below 50 again, which will require that it rise above 50 first. I can go into greater detail if you need. But it sounds like you are studying/reading other posts. I am a fan of small, task specific pistons. For me, that makes them easier to code and debug. Others here like large, complex pistons. You certainly could split this into different pistons.


#3

If a piston has a single trigger, or multiple triggers (lightning bolts in the left margin), whenever any one of them changes (in either direction), then the entire piston is run top to bottom, and any commands not blocked by conditions, will execute.


For example, consider this piston with two triggers:

IF Sensor's motion changes to active
    Then do something
END IF

IF Door's contact changes to open
    Then do something else
END IF

With Lightbulb
    Do Toggle
END WITH

The two triggers here are “Sensor’s motion” and “Door’s contact”.

This means the piston will run top to bottom on any of these trigger events:

  • Sensor’s motion changes to active
  • Sensor’s motion changes to inactive (notice this is not in the code above)
  • Door’s contact changes to open
  • Door’s contact changes to closed (notice this is not in the code above)

… The key element is, all four events will “Toggle” the Lightbulb.

Or to say this another way:

Sensor's motion changes to active   = Do something & Toggle Bulb
Sensor's motion changes to inactive = Toggle Bulb
Door's contact changes to open      = Do something else & Toggle Bulb
Door's contact changes to closed    = Toggle Bulb

When a triggers starts a piston, it runs top to bottom once, and then stops. It will go thru the same process on each and every trigger event that comes in.

For clarity, here in webC0RE we can make code that loops, but I would not recommend that until you have a firm grasp on the core concepts of webCoRE. (one bad programming loop can literally cripple your entire SmartHome)


Help on creating a timer to run a check every one minute to see if motion activates
#4

@WCmore, excellent description ! I hope your side job is as a computer programming teacher ! Also, am I correct with the above that the piston will only run when temperature drops below 50 and not again when the temperature rises above 50?


#5

I install & program SmartHomes and SmartBusinesses for a living, but I usually incorporate a lot of teaching during that process…


This trigger:

pic

Will only execute that block at the precise moment when the temp goes from 50 (or above) to 49 (or below). Once it drops below 50, it will not execute that block again until the temp rises to 50 (or above) and then drops again down below 50.

One extra addition to this is that code outside of this block will execute on each and every temperature change. No matter whether it is rising or dropping.


#6

So each temperature change (up or down), even by just 1 degree, will trigger the piston to run top to bottom?


#7

Jumping in here… The answer is yes. Since the trigger in this piston is the temperature reading from that sensor, every time the temprature reading changes (up or down) the piston will execute from top to bottom - ‘looking’ for conditions that are met. If it finds any, it’ll execute all the code blocks where the conditions are met. If it finds that none are met, it stops.

So to be clear, the piston will run top to bottom with any temperature change. Only the code blocks of the piston where its conditions are met (if any) will actually complete actions.


#8

So this piston, and any that use a trigger with temperature changes, could potentially run dozens/hundreds times a day. Good to know.


#9

Well put, both of you…

Temperature changes, along with motion sensors and power changes, are some of the “chattiest” of triggers.


One important addition to this:

If there are any commands NOT inside an IF block, they will execute each and every time.
(even when the IF block(s) are false, as seen with my “Lightbulb Toggle” above)


#10

Don’t let the nice guy on this forum to fool you. Behind the scenes he is an ass :joy::joy::joy::joy::joy::joy:
(no worries we are good friends, he can handle a loving joke)

He did most of my house and taught me everything I know and post here…


#11

LOTS of his knowledge has flowed in my direction too! And I appreciate all of the help that I get from every member here. We have a great, helpful community.


#12

Yes, unless your device records decimals, then it could actually trigger ten times when changing a single degree!


#13

Honestly, it’s very refreshing to see an online community this kind, nice and helpful without a single troll…
I wrote a single post in ST forum last year and boy oh boy all the haters came out of the wood work.


#14

Sorry @korey99, we kinda got off track from your subject. You have been given some very helpful information here. I hope you glean some benefit from it all. :beers: for everyone.


#15

That’s true. Any commands not limited by conditions or are not inside an IF block will execute every single time the trigger(s) change.

I can say for a fact that for location sensors, this is certainly the case. We recently discussed my use of our iPhones locations to determine the proper thermostat settings. As I found out during testing and discussions here, the location sensors change and update almost constantly - even when the phone is in my pocket sitting here at my desk. I’ve now set a 5-minute loop and changed the location sensors from triggers to be used as conditions. Much, much ‘quieter’ now.


#16

How right you are… I did not want to open that can of worms, LOL


#17

Thank you all very much. I appreciate the explanation, and I think everything is clear now. WCmore’s description solidifies the understanding for me. Appreciate the sharing and patience.