This is a somewhat noobish question, but I’m new to Groovy, so any pointers would be greatly appreciated. I think I’m missing something obvious.
Say I have a bunch of sensors of the same type, in this case, contact sensors that are set to garage door mode. My user has made multiple selections in the preferences pane. Now, say 2 of the 3 garage doors are open.
Can someone please point me in the right direction on how to determine the user-defined names of the sensors in a certain state? I see you can use displayName for a single sensor, but what if you have multiple devices and are only interested in ones where the contact attribute has the “open” value?
There are a few ways to do this (in Groovy, there usually is), but this would work. This example uses switches, but you could modify it to fit your needs:
def getOnSwitches() {
// find all the switches that are on
def on = switches.findAll {
it.currentValue("switch") == "on"
}
log.debug "there are ${on.size()} switches on"
// In SmartThings, you can get device properties on entire
// collections just as you would a single device. The result
// will be a list
def onNames = on.displayName
// pretty print by joining the list returned with a comma
log.debug "These switches are on: ${onNames.join(', ')}"
}
And more information about working with multiple devices in a SmartApp here.
If you’re new to Groovy, check out our Groovy Basics Docs. It’s not complete Groovy documentation of course, but does contain a lot of tips, tricks, and information about working with Groovy.