How to implement switch statement


#1

1) Give a description of the problem
What is the best way to implement a “switch statement?” Say I have two presence sensors and want to use a switch statement to determine who is home.
Like:
{whoIsHome}
{
no sensor present = 0
sensor 1 is present = 1
sensor 2 is present = 2
sensor 1 and sensor 2 present = 3

and maybe if it makes sense. I can see needing this maybe if i add more presence sensors

sensor 1 present but not sensor 2 = 4
sensor 2 present but not sensor 1 = 5
}

2) What is the expected behavior?
how do i implement this and then how do I take action? I’m not sure how to populate it and then how to do something with it like something as simple as sending an SMS.

send SMS: "{whoIsHome} is home"
I’d expect the message to report "sensor 1 is home"
I would not want “1 is home”

3) What is happening/not happening?
Just need help learning how to do this. I’ve seen other examples that have dynamic variables and then those variables are added to the switch variable. I’m not sure if this is the way I want to try to make this work and i’m also not fully comprehending it.


#2

IF
Any of presence sensor 1 or presence sensor 2 changes to present
THEN
switch($currentEventDevice)
case <<SELECT PRESENCE SENSOR 1 as physical device>>
your actions go here for presence sensor 1
case <<SELECT PRESENCE SENSOR 2 as physical device>>
your actions go here for presence sensor 2
default
leave blank
End IF

Here is the easiest way I can think to get started. It is similar to how I have my arrival/departure pistons setup.

As for your message, depending how your sensors are named (ie mine are the names of myself or girlfriend) you can just use $currentEventDevice is home.


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

Is it actually possible to do this? The UI always wants me to choose an attribute (e.g., switch or $status) for the device when I choose “Physical Device” as the case type. If I don’t choose an attribute, the case condition ends up empty. The only way I’ve found to do this is to assign physical devices to variables and then use the variables as the case conditions.


#4

Sorry, just use a value and type in the name of the sensor


#5

Hmmm…that seems a bit fragile (i.e., changing a device’s name might break things). It’s something, though.