Switch Case Example - 4 button remote with pushed and held


#1

I haven’t seen a ton of Switch examples on here while I was looking around on how to accomplish this particular task. So I figured that now that I have it done and working, that I would turn around and share it.

I have a 4 button remote where each button supports pushed and held. It’s the Aeon Minimote. I wanted to create a piston that could handle all 8 possible events that the remote can perform. I wanted it to be a simple, easy to understand design, and something that would be easy to work on. This is what I came up with, and it works great!

I created a switch statement that generates a string by combining the button id (index) as well as the action type (value) of either “pushed” or “held”.


#2

Thanks for your contribution… This looks like it could be quite useful…


With 8 triggers in play, an advanced version of testing might incorporate WAITs, and seeing if a different button interrupts or causes conflicts. (even though normally, I do not use WAITs in a multi-trigger piston)


#3

I already performed this test with logging messages under each case and everything worked as expected.

However, for the time being I don’t need any other cases filled for my uses, so I removed them.

As far as the race condition, I wasn’t too worried about testing that as I wasn’t planning on pressing a bunch of buttons on the remote rapidly, but I can look into that a bit.


#4

Excellent! Thanks for clarifying!


#5

Is it typical or standard for device handlers to pass a string as the index ($currentEventDeviceIndex) to button events?

I ask because the device I’m using, Zooz Zen27, uses integers, and this example failed for me with something like “could not convert str to int.” I can’t find much documentation on buttons let alone what’s normal for their device handlers … I’d like to let Zooz know if it’s atypical so folks don’t run into this again.


#6

Quick answer:

Every device and handler is different.

I typically run a few tests on a new device to see what actually passes, and then program accordingly.


#7

You could also try using an expression with an interpolated string instead of what I’m doing.

In my example, I’m making the assumption that both $currentEventDeviceIndex as well as $currentEventValue are strings. If they were integers, then it’s possible they would have thrown the same error you are seeing.

Based on your error…it sounds like it is treating the + sign as an addition operator instead of a concatenation operator, and it’s trying to convert ":" to an integer in order to perform the math.

You could try either explicitly converting $currentEventDeviceIndex to a string…or, you could use string interpolation (I am assuming webCoRE handles string interpolation the same way as C#…where it automatically converts every item to a string implicitly).

Example:
Instead of trying to use this:
$currentEventDeviceIndex+":"+$currentEventValue

You could try this instead:
"{$currentEventDeviceIndex}:{$currentEventValue}"

webCoRE should try to automatically convert anything in the curly braces to a string.


#8

Longer answer:

This part could simply mean with (or without) quotes at the source.
(IE: Strings always have quotes around them, but numbers typically do not)
Some would argue that it is a good habit to put quotes around numbers…
and in some respect, they are right.

But so are the ones who do not use quotes… So go figure, LOL.

Both perspectives have validity.

Be that as it may, thankfully, webCoRE can convert Strings to Integers (or visa versa)
So, as long as your handler does not change their internal format,
then webCoRE will consistently see it as X.
(and the piston can convert etc from there)

TL;DR:
If they add/remove quotes now, then many Zooz users will have to rewrite their pistons.


I typically only encounter this when working on an unfamiliar device.
If I fail with a simple math problem, then it means it’s likely coming in as a string.

For display purposes, I might leave it as a string.

If I need to do calculations on it, I might convert to an integer using:
integer(string)

… or a decimal using:
decimal(string)


More conversion examples can be found on the Function page.


#9

Is the point of this to use the strings you get of this as actions in other pistons?


#10

It’s just reducing the overhead you would normally need to set up a piston for a keypad or a remote.

So you can use my suggestion instead of doing this:

if
    Keypad 1's button gets pushed
then
    do something
else if
    Keypad 1's button gets held
then
    do something
else if
    Keypad 2's button gets pushed
then
    do something
else if
    Keypad 2's button gets held
then
    do something

#11

I see. Thanks :slight_smile: