DefaultValue for capability.motionSensor setting

I'm trying to simplify the number of steps for our admins when setting up our custom apps on our hubs. As a convention our admins add a living room motion sensor with a label of livingroom, all lower case. Ideally, I'd like to be able to default the configuration settings of our hub apps using the device labels. Something like below.

input "livingRoomMotion", "capability.motionSensor", title: "Living Room", defaultValue : "livingroom"

That doesn't work but demonstrates what I'm trying to achieve. I tried getting a list of devices on the hub but couldn't find the API call for that. I figured I might be able to iterate over a list of devices in the install() method of my app looking at the device label property and somehow default the properties.

Has anyone been able to achieve this?

I don't think there is a way to officially do this. User apps can only access devices that users have specifically selected, though I suppose this is being asked as a sort of way to achieve that goal. Similarly, there is definitely not an API (for user apps--at least not without going through another app that does have them authorized) to get a list of all hub devices. There is actually a Rooms API, but I'm not sure if that is documented yet or ready for public consumption, and even if you were able to find a device reference that way, I'm still not sure it would help you fill in the default value for a device input selector (and you'd still have to have the device in a Room).

Unofficially, I have heard talk about some people "faking" this with JavaScript (finding the desired item in the list and effectively "clicking" it--inserted, I assume, via a paragraph() on the app Groovy side). I can't help with that and wouldn't really recommend that option (given that it's a bit sketchy, even if your intentions are helpful, plus it's not a guaranteed "API" and may break/change at any time). But if it's something for your own use and you find it helpful, perhaps it's something you could explore.

It's not perfect but I came up with the below prototype solution. The hub admin user still has to select the devices. Not much room for error though in that they just tick the one select all checkbox on the allDevices app setting. Now the update method had access to all devices on the hub and iterates over them setting up the subscription based on the sensor display label.

/**

preferences {
section("Settings"){
input "allDevices", "capability.*", title: "Select Devices", submitOnChange: true, hideWhenEmpty: false, required: true, multiple: true, defaultValue : true
}
}

def installed() {
initialize()
}

def updated() {
unschedule()
unsubscribe()
initialize()

allDevices.each { 
    if (it.getLabel() != null) {
        def deviceLabel = it.getLabel().toLowerCase()
            if (deviceLabel == "livingroom" || 
                deviceLabel == "bedroom" || 
                deviceLabel == "batrhroom") {
                log.debug "Name : " + it.getLabel()
                subscribe(it, "motion.active", motionActiveHandler) 
            }
    }
}

}

def initialize() {

}

def motionActiveHandler(evt) {

}

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.