Device event subscriptions: Can we subscribe any event?

Hi all,
I have a question about the subscribe( ) API. The documentation states that we can subscribe the event values. Basically, I’m trying to understand the way that we can subscribe the device attributes. Particularly, it is stated that

The handler method must accept an Event parameter.

Refer to the Event API documentation for more information about the Event object.

You can find the possible Events to subscribe to by referring to the Attributes column for a capability in the Capabilities Reference. The general form we use is “.”. If the attribute does not have any possible values (for example, “battery”), you would just use the attribute name.

In the example above, the switch capability has the attribute “switch”, with possible values “on” and “off”. Putting these together, we use “switch.on”

My question is, there are devices that have attributes different than ENUM like switch example has only values on and off. For instance, battery: NUMBER and heatingSetpoint: NUMBER is one of the many examples. Can I subscribe these events?, for instance, subscribe(my_battery, “battery.50”, battery_50_handler)
or maybe
subscribe(my_battery, “battery.currentState(“battery”)”, battery_50_handler).

You can subscribe to battery events, the eventhandler will be passed evt, do a dev = evt.getDevice(), with the device get dev.latestValue(“x”) for the attributes you need…

1 Like

Thanks for the information. Basically, you say, we can only subscribe to the events that have the following format, attribute: ENUM. For all others, you can subscribe to its attribute only (like the battery) and get the state information using the latestValue, currentValue, currentBattery or there are some other calls such as currenState(“battery”), batteryState.

This means I cannot subscribe a specific value of a battery or a predicate such as battery.value>50.

Is my understanding correct?

Your understanding is correct

1 Like

Okay, thanks @dudz40. However, I’m getting confused by delving into the documentation. Let’s consider the smoke sensor. The documentation states that it has attributes smoke: ENUM and carbonMonoxide: ENUM.
How can I subscribe to events of this device?
subscribe (“theSmoke”, “smoke.value”, smokeHandler) and also subscribe (“theSmoke”, “carbonMonoxide.value”, smokeHandler). Based on my understanding, both smoke and carbonMonoxide are ENUM types so that they have values that allow me to subscribe them. Correct?

Attributes
smoke: ENUM
The state of the smoke detection device

clear
No smoke detected
detected
Smoke detected
tested
Smoke detector test button was activated

carbonMonoxide: ENUM - Optional
Optionally, the state of the carbon monoxide device

clear
No carbon monoxide detected
detected
Carbon monoxide detected
tested
Carbon monoxide device test button was activated

you can subscribe to a specific state of an attribute e.g. switch.off, motion.active, carbonMonoxide.detected, these are discrete states, when an attribute has a range e.g. temperature, batterylevel etc. then you cannot test for a specific value (this is my observation). A bit like the difference in states that is described in the tile docuementation.

1 Like

Thanks @dudz40, that makes sense, now I start understanding the logic. Can you guide me through the documentation you mention? I would like to understand the logic better.

The device handler section of the documentation is a good start for this. It will explain event state attribute.

2 Likes