Subscribe to all Sensor Events


I’m trying to subscribe to all events for switches and sensors. My code is as follows:

preferences {
    section("Devices to Track") {
        input "switches", "capability.switch", multiple: true, required: false
        input "sensors", "capability.sensor", multiple: true, required: false
def initialize() {
    subscribe(switches, "switch", deviceHandler)
    subscribe(sensors, "sensor", deviceHandler)
def deviceHandler(evt) {
    do things here

The switches trigger my event, the sensors do not. Any way to get this to work?

Capability “Sensor” is for tagging / filtering only (so it is great for your input statements).

You cannot subscribe to a “sensor event” though; just a “sensor device”.

  • Try accepting any event from the sensors.
  • or Try pre-querying all the sensors to determine what real Capabilities they have

“Try accepting any event from the sensors.”

That’s exactly what I want. How do I do that?

subscribe(sensors, "any", deviceHandler)

does not work. Is there a way to tell it to run deviceHandler on any event from a sensor?

Got it! Is this the best way to go or is there a better way?

sensors.each { sen ->
    sen.capabilities.each { cap ->
        subscribe(sen,, deviceHandler)

That looks close; but remember that a Capability can have multiple Attributes. So you may want (or need?) to subscribe to those individual Attributes.

I’m not sure if I want each attribute. If I subscribe to the capability, will I get the handler to run on any attribute? It matters not what the value is in my handler code, I just need it to run on any sensor state change.

Honestly… I don’t know off the top of my head.

I’ve said this a dozen times to novices:

SmartThings is a highly complex system. You just joined the Community 22 hours ago?!

You have to spend many days (or even weeks or months) to write “good” ST good. Start with really, really, really simple examples. Make sure they work. Add a change, and another, until it breaks. Then rollback and start again.

A “SmartThings for Dummies” class at a college would be at least 2 semesters (8 months!) long. Seriously.

Folks are so impatient and impulsive these days. I’ve been here 5 years by the way.

Thanks, I’ll change it to this for now:

sensors.each { sen ->
    sen.capabilities.each { cap ->
        cap.attributes.each { attr ->
            subscribe(sen,, deviceHandler)

Yeah, I just started getting into writing ST apps. Joined this community site yesterday, but have been working on this app for about a week now. Am I asking things above my level? This is a pretty simple app (I think, could definitely be wrong). It’s been a lot of fun working on ST apps so far, looking forward to doing more with it.


You obviously know Groovy … some folks ask for complete coding solutions and their questions are actually “how to write Groovy code” – the ST community isn’t really meant as a Groovy or Java tutorial: There’s 1000s of sites for that!

What takes time to experience, however, is the quirks, limitations, and bugs of the SmartThings environment. These aren’t documented - there are a few notes in the official Developer Docs, but lots of quirks are not mentioned. Like never trust the Simulator - it doesn’t work for anything but the most basic basic basic examples. Always push to your phone to test.

And the other thing that experience brings is finding all the great code examples in the Community Forum and in the public GitHub:

Lol, I just started with Groovy last week, when I started playing around with ST. I have been coding in general for like 20 years, so there is that.

That’s good to know lol. I’ll test a lot more on phone then :slight_smile:
I will definitely look through the Github examples. Thanks! One of the things that led me to buy an ST hub instead of another Z-Wave hub is that I read that the comminuty is very alive and helpful. Glad to see this is true!

1 Like

You said that we cannot subscribe to a “sensor event” though; just a “sensor device”.
Would that include “capability.switchLevel” ?
I have been attempting to flag an event - if level changes, but I’m just not getting it to work.
I have parsed down my code to the basic to just test, I have also checked it out in webCore, can’t get it to work there either.
input “devices”, “capability.switchLevel”, multiple: true works to select the devices with that capability, then the documentation goes south - so I have tried:
subscribe(devices, “switch”, levelEventHandler)
** subscribe(devices, “level”, levelEventHandler)**
** subscribe(devices, “setLevel”, levelEventHandler)**
** subscribe(devices, “switch.setLevel”, levelEventHandler)**

still no joy, I have got switches and buttons all subscribed and working , but not detecting level changes.
I have read all the documentation I can find, and gone through other peoples code to no avail.
Would you have any ideas ?


I don’t have an example handy, so falling back on the docs for now.

The first method signature shown on the section below is the applicable one, and you can find the Attribute Name in the Capabilities section of the Docs.

Thanks for the documentation links, I have already studied them and your other posts regarding smartapp subscriptions.
I added all the subscribe variants into the smartapp to make sure i wasn’t missing something.
I have seen all of the variant used in other peoples code (some of it quite old, so the underlying IDE might have changed)
My test code is very basic and small, just to test the subscribe, I just can’t get it to work.
Also the webCore test I setup, which check to see if the level has changed doesn’t work either.

Thanks - Update
I found my issue, it was the zigbee device driver I was using…
Changed it to the GE Link - it had the correct capabilities
capability “Switch Level”

1 Like


You can’t expect a device to have Attribute(s) for a Capability it doesn’t claim :wink:.

Strange that it allows me to set level and get current level but not check on change, but judging by the incompleteness of the documentation I’m not surprised.