- Most devices don’t report if they’re offline.
- Some don’t report an acknowledgement upon receipt of a command.
- For those that don’t acknowledge, some can’t be polled for their status.
Given this broad range of (in)abilities, it’s a challenge to devise a ‘generic way of offline detection’.
The simplest strategy is “If I don’t hear from you for over X minutes (or hours), I will assume the worst and consider you to be offline.”
In MQTT Sensor, the expire_after option implements this concept.
(integer)(Optional) Defines the number of seconds after the value expires if it’s not updated.
Default value: 0
So if you set expire_after
to 300 seconds (5 minutes) and no message is received during this period, the sensor is considered to be offline and, instead of a numeric value, the UI shows a hyphen (-). This is suitable for sensors that periodically report their status (temperature, humidity, etc).
This concept would not be applicable to light switches or any other device that does not periodically report its status. You’d have to use another technique such as using the device’s acknowledgements (to issued commands) as an indicator of its health. So if you command the light to turn on and it replies with an acknowledgement, you consider it to be online. If it fails to reply, you consider it to be offline. Many lighting protocols work this way but not all so this technique cannot be considered to be generic.
Another technique would be polling. Periodically request status from all devices and those who fail to reply are deemed to be offline. Yet again, not all devices support polling so this technique is not a panacea either. In addition, polling comes with its own headaches. For devices using wireless mesh networks (zwave, zigbee) if not used judiciously, it can add to network congestion and impact its performance (i.e. responsiveness).
The MQTT protocol has the concept of Last Will and Testament (LWT). In the event the device goes offline, its LWT is shared with other devices (… pretty much like how it works with deceased people).
The question is “How do we know the device is offline?” In MQTT, devices formally connect and communicate with an MQTT Broker (a ‘middleman’) who manages and monitors the connections. If the Broker detects the device is no longer communicating, it publishes the device’s LWT. Any other device subscribed to that LWT (like Home Assistant can via the availability_topic) is now informed of the device’s demise. However, this is all specific to MQTT and isn’t applicable to other protocols.