Find the Device Array matching Index Position


#1

1) Give a description of the problem

I am trying to find a way to write a portion of a piston more eloquently by reducing the number of “Ifs” used to populate a Variable. (See SwitchPositiontoUse variable and lines 45 to 70 in the Green Snapshot.)

2) What is the expected behaviour?

Based on which door is opened, a message plays that it is open.
An Alexa virtual switch triggers an Alex Routine that plays a message.

3) What is happening/not happening?

I have setup a Device List Variable with all of my Door Sensor Names (see ListOfDoorSensors variable) and have setup a Device List Variable with all of the Announcement Switches (see ListOfBroadcastMessageSwitches variable). The position of each corresponding device matches its relationship.

When the Door opens, I store that specific device in WhichDoorOpened variable.

Instead of setting the SwitchPositiontoUse variable using Ifs…How can I use the WhichDoorOpened variable to lookup in the ListOfDoorSensors variable’s position then convert it to number that would be put into the SwitchPositiontoUse, Which would allow the expression in BroadcastSwitchToUse work as a lookup.

**4) Post a Green Snapshot of the piston![image|45x37]

Closing this item as it was solved;

Blockquote I believe what you want is:
BroadcastSwitchToUse=arrayItem(indexOf(ListOfDoorSensors,WhichDoorOpened),ListOfBroadcastMessageSwitches)


#2

I believe what you want is:

BroadcastSwitchToUse=arrayItem(indexOf(ListOfDoorSensors,WhichDoorOpened),ListOfBroadcastMessageSwitches)


#3

Thank you @guxdude . It didn’t even dawn on me to use arrayitem and indexOf together.