Using a switch block

execution

#1

I am attempting to use a switch block instead of cascading if/else statements, but it’s not working the way I’d expect a switch block to work.

I’m expecting it to work like this:
switch (keypad button 1)
case button is pushed
turn on lamp
case button is held
turn off lamp
default
do something else
end switch

But when I start to fill in the values, the only way I’m able to tell it when button one is pressed is to include an if within my case which defeats the purpose of the case itself.

can someone help me determine the proper way to use this switch block?


Cycle automations/scenes with list/sequence instead of nested ifs
#2

It seems like the intent here is to easily handle pushed and held events for any key on the keypad. The two if’s shown above aren’t a big deal but if you’re handling two states for each of 9 buttons that would get chaotic. You’re on the right track with switch but it is used a bit differently than shown.

Let’s try an on event block that can subscribe to all important events triggered by your keypad(s). Inside the on event block you can use a switch to react to the event based on the $currentEvent* system variables. To begin, I would just add a debug log in the on event block for each of these variables so that you can see which corresponds to pressed, held, button #1, etc. I don’t have experience with a keypad but I would try logging $currentEventAttribute, $currentEventUnit, and $currentEventValue.

Once you see what is available for triggering you would use it like this, where the cases are the values you expect to see for the switched variable.

switch ($currentEventAttribute)
    case "pushed":
        with RGB Bulb 1 Turn on
    case "held":
        with RGB Bulb 1 Turn off
end switch

If the on events doesn’t work out, you can mitigate the code repetition in multiple if’s by storing your keypad in a variable and using that variable in the if’s.


7 Buttons - one piston. Am I doing this right?
Struggling with Comparison (integer) 1 is (integer) 1 = false
#3

The couple pistons where I am using a Switch I am using value as the case and then typing in the value I am looking for. For example, I have a piston where the switch is location mode and then for case I used value and typed in ‘Home’, ‘Away’, etc. These values match exactly my location modes. I’ve not been able to figure out how to use anything but value as the case. I’m sure you can use an expression to get to your keypad buttons. Someone will have the answer shortly I’m sure.