Need help: read current state of multiple device


(Chrisb) #1

So I’m working on an app to check the current state of multiple open/close devices. Obviously I can have the user input multiple sensors by adding the “multiple: true” to the preferences section. And if not mistaken the devices are known as sensor1, sensor2, sensor3, etc. (assuming I put sensor as the input name). But how do I know how many devices the user selected when installed?

What I’d like to do is check each sensor and then send a text if with the name of a sensor that is open.


(Im Brian) #2

YOUR_SENSORS_VARIABLE.findAll { it?.latestValue("contact") == "open" }

In the link above, you can see how I’d done that exact thing, then string them togther with ", " as the join glue - and check teh count to see how the verbiage should be changed.


(Todd Wackford) #3

It actually is an array of sensors and you an call the “size” method. Try “def myCount = sensor.size”. You can get to each one like sensor[0] for the first one and sensor[1] for second, and so on. So if you wnated to cycle through them all:

for (def cntr = 0; cntr < sensor.size; cntr++) {
//do something in code
sensor[cntr].on()
}

HTH
Twack


(Chrisb) #4

Wow @imbrian

That’s a pretty powerful line of code there… does exactly what I’m looking for. I have no idea why or how, but it works! :slight_smile: