Device.find(...) always returns a null

I have a routine I took from SmartThings that I can not seem to get to work in Hubitat. Here is the stripped down version

preferences {
     section ("Select the switch") {
		input "switches", "capability.switch", title: "Real Switch", required: true, multiple: true
      }
	}
def installed(){
     initialize()
}
def updated(){
    initialize()
 }

def initialize() {
	subscribe(switches, "switch", theSwitch)
}
def theSwitch(evt) {
    // Both log.info display the same id. In the case of multtipleswitches switches.id returns the array and the evt.deviceId is contained within this array
    log.info evt.deviceId
    log.info switches.id
    // Never appear to find the switch
    def device = switches.find { it.id  == evt.deviceId  }
    // device is always null
    log.info  "Device = $device"
}

There are no error but the find always returns a null. What have I got wrong??

evt.deviceId is a Long, while dev.id is a String. evt.device.id is also a String

Try this instead:

def device = switches.find { it.id == evt.device.id }

or

def device = switches.find { it.id.toLong() == evt.deviceId }

2 Likes

The second example worked. (The first is the same as I had above)

No, you were comparing to evt.deviceID, not evt.device.id

2 Likes

:blush: Wow, my "old mans" eyes missed that difference.