Groovy question - getting the names of all devices in a certain state

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.

And, welcome to Groovy!

1 Like