Resolve Device Object by Device ID or Device Name?

A smartapp doesn’t automatically have access to all devices in the system. It can only reference devices that the user has selected in an input, or that have been created as child devices by the smartapp itself.

You can get an array of devices either from an input you have defined in the smartapp’s preferences, or using getChildDevices() to get any child devices you have created. Then you can use find() or findAll() on the array…

// Get the devices from an input:
def devices = settings.inputDevices
// Or from child devices:
//def devices = getChildDevices()

// Find Devices with a specific ID:
devices.findAll( { it.id == "1a61e11a-c4ca-4a76-912f-7c45ffe6b306"} ).each {
	log.debug "Found device: ID: ${it.id}, Label: ${it.label}, Name: ${it.name}"
}

// Find Devices with a specific name:
devices.findAll( { it.name == "blah"} ).each {
	log.debug "Found device: ID: ${it.id}, Label: ${it.label}, Name: ${it.name}"
}
3 Likes