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

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.

And, welcome to Groovy!

1 Like

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.

Also, check out @pstuart’s Live Code Fridays edition on Groovy: Live Code Fridays 11/6/2015 8pm EST

All of the Live Code Fridays are good and worth checking out.


That is great. Thanks a ton Jim, I would not have thought of that on my own. It’s exactly what I need to put the final bit of polish on this app.

1 Like

Thanks for the plug @Jim I learned something in the above code. Groovy has so many ways to accomplish the same thing.

1 Like