Use one button condition for full piston?


#1

I assume this is possible with groups but I couldn’t quite work it out.

Instead of having the if statement check to see if a switch has turned on, I want that switch to turn trigger all the check conditions and if statements.

For example.

If switch is turned on…

and condition A is true
and condition B is true
then:

else
If condition A is false
and condition B is true
then:

else
If condition A is false
and condition b is false
then:

How would I structure the piston so the button switching on is only checked once at the start to trigger the piston and then all the if conditions are a part of that?


#2

I would use nested IFs to create four distinct possibilities… Something like

IF DeviceX's switch changes to on  <-- Trigger
Then
    IF ConditionA is true          <-- Condition
    Then
        IF ConditionB is true      <-- Condition
        Then
            Log "A & B are both true"
        ELSE
            Log "A is true, B is false"  (bonus not mentioned in your OP)
        END IF
    ELSE
        IF ConditionB is true      <-- Condition
        Then
            Log "A is false, B is true"
        ELSE
            Log "A & B are both false"
        END IF
    END IF
ELSE
    This ELSE intentionally left blank. (it'll execute when DeviceX turns off)
END IF

This method works well as long as you keep only one condition in each IF. The IFs go three levels deep, so just go slow when creating this, and use my indents above as a reference.


Pro Tip:

If any of your commands turns off DeviceX, then the entire piston will re-trigger again, and run thru the code top to bottom. (this is why the last ELSE is left empty)

The logic behind this is:
You are subscribed to DeviceX’s switch. This means whenever it turns on (or off), the entire piston will run thru the code, top to bottom, and execute any commands not blocked by conditions.


#4

Ahh nested perfect - it seems obvious when you say! Will work on doing it this way.


#5

I’m thick headed today. Just not getting why you would do this.


#6

Just to follow up this is what I ended up with and works perfectly - this then triggers a bunch of web requests in another piston. I’m using a virtual dimmer so I don’t need tens of virtual buttons for the same command/s.


#7

I was taking an educated guess that DeviceX may have been a SimSwitch, so my comments was just to make sure that @TheCartel was aware of what happens when that switch turns off.

Thankfully, he is not turning off that device in this piston, so the final ELSE will only be executed later when DeviceX is eventually turned off. (no matter how it is done)


To clarify a bit, sometimes I put common code in that final ELSE block if I want something to happen when the switch is turned off… but normally, I leave that final ELSE block empty.


#8

Then why add it at all? I’m sure I’m missing something obvious.


#10

You are right. It is 100% empty…

I only wrote, “This ELSE intentionally left blank” to make it obvious.
(I usually write notes like that in the comments)


#11

Lookin’ good!!

If you are happy with it, maybe solution a post as the solution to help others.


#12

I am! Ticked for you. It was pretty obvious when you pointed it out - i’ve no idea why my brain started doing it where each IF statement was checking the full parameters before passing it on - in theory it would still work but I wasn’t sure if because each section was subscribing to the button press it would actually break on the second if as the button as no longer changed to “on” hence just wanting that checking once at the start of the piston. I thought groups might do it but this of course works perfectly and is probably a cleaner way to get 5 outcomes and could be expanded on pretty easier for some more.

I might add though i’m using a momentary push button to trigger it all so in theory it turns back off again straight away but it hasn’t caused any issues in my testing - all works perfectly.


#13

In that case, you definitely want to keep that very last ELSE totally empty then.
(as you have done)