Resolve Device Object by Device ID or Device Name?

I’m trying to get a Device Object by either a Device ID (e.g. 1a61e11a-c4ca-4a76-912f-7c45ffe6b306) or Device Name (e.g. Fibaro Motion Sensor ZW5).

I’ve tried multiple ways to get the Device Object, mostly by comparing objects and variables by looping
settings.each { .. }
To see if device property (Name or ID) is equal to the given Name or ID and if true, save the Device Object.

Sadly I haven’t been able to make it happen just yet and would appreciate any suggestions!

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

Thanks a lot!
However

def devices = settings.inputDevices

devices now contains null
But when I loop through

settings.each {
log.debug “key: $it.key value: $it.value”
}

It does contain valid data

The devices are stored as input:

preferences {
    section("Allow Endpoint to Control These Things...") {
        input "switches", "capability.switch", title: "Which Switches?", multiple: true, required: false
        input "dimmers", "capability.switchLevel", title: "Which Dimmers?", multiple: true, required: false
        input "thermostats", "capability.thermostat", title: "Which Thermostats?", multiple: true, required: false
        input "motions", "capability.motionSensor", title: "Which Motions?", multiple: true, required: false
        input "accelerations", "capability.accelerationSensor", title: "Which Accelerations?", multiple: true, required: false
        input "contacts", "capability.contactSensor", title: "Which Contacts?", multiple: true, required: false
        input "illuminants", "capability.illuminanceMeasurement", title: "Which Illuminance Sensors?", multiple: true, required: false
        input "temperatures", "capability.temperatureMeasurement", title: "Which Temperatures?", multiple: true, required: false
        input "humidities", "capability.relativeHumidityMeasurement", title: "Which Humidities?", multiple: true, required: false
        input "presences", "capability.presenceSensor", title: "Which Presence?", multiple: true, required: false
        input "locks", "capability.lock", title: "Which Locks?", multiple: true, required: false
        input "batteries", "capability.battery", title: "Which Batteries?", multiple: true, required: false
        input "powers", "capability.powerMeter", title: "Power Meters", required:false, multiple: true
        input "energys", "capability.energyMeter", title: "Energy Meters", required:false, multiple: true
        input "dioxides", "capability.carbonDioxideMeasurement", title: "Co2 Measurement", required: false, multiple: true
        input "signals", "capability.signalStrength", title: "Signal Strength", required: false, multiple: true
        input "leaks", "capability.waterSensor", title: "Water Detection", required: false, multiple: true
        input "sounds", "capability.soundPressureLevel", title: "Sound Pressure", required: false, multiple: true
        input "colors", "capability.colorControl", title: "Color", required: false, multiple: true
        input "colorTemperatures", "capability.colorTemperature", title: "Color Temperature", required: false, multiple: true
    }
}

However,

def devices = getChildDevices()

Also returns null

You’ll need to build devices from the inputs you have. In your case:

def devices = settings.switches + settings.dimmers + settings.thermostats // + etc.

Alternatively, if all your inputs are device inputs, you could do:

def devices = []
settings.each() {
    devices += it.value    
}

If you had other types of input though this would obviously break, so you’d want to add some checking and probably a good naming scheme for your inputs.

2 Likes