[Groovy] Programmatic way of getting all devices with certain capabilities?

I currently do something like this in my custom smart app:

    paragraph   "All sensors capable of reporting power usage events"
    input             "ALL_POW_01", "capability.powerMeter",
    multiple:       true,
    title:               "Which power meters?"

    // A little later...

    subscribe(ALL_POW_01, "power", handlePowerUsageEvent)

     // ...and handlePowerUsageEvent ships off all device activity to a Node-based API I'm writing

This is all well and good, but it means I have to manually add new devices to my smart app every time I get them, which I don't always remember to do.

I've scoured the docs and forums, but can't find an answer:

Is there a programmatic way of doing something like the following?

def powerDevices = getAllDevicesWithCapability('power')

I see a vaguely similar Q&A on here that recommends composite devices as a solution, but I can't find any useful documentation that explains what composite devices actually are, and what I'm able to glean from context doesn't make them seem like a fit.

Short answer no. By not allowing this type of functionality, the hub is actually protected from a rogue app taking over devices that the owner has not authorized explicitly.


Thanks for the response and the explanation. It makes sense.

Adding devices manually isn't too onerous a task. It's also something I do a few times a year--not a few times a week.

Anyway, thanks again. Glad to know the reason I have to do it this way.

1 Like

Glad to help.

is there a way to check the device capability.
eg if (outputSensor("capability.notification") == true) {

ive added the ouput sensor already as it has presence capability. but if it also supports notification id like to subscribe to it

outputSensor.hasCapability("Notification") == true


Download the Hubitat app