The Sensor That Failed Silently: MQTT Last-Will Messages and Detecting Devices by Their Absence
A sensor stops publishing. Nothing in the dashboard changes. The last value sits there, looking like a reading, and the only thing that has actually happened is that no new message arrived. Silence is not an event unless something is watching for it.
MQTT has a mechanism for exactly this. It is called the Last Will and Testament, and it is the cheapest way to turn a device’s absence into a visible state. It is also narrower than most people assume, and the narrowness is the useful part.
What the will message actually is
When an MQTT client connects to a broker, the CONNECT packet can carry a Will Flag. The MQTT 5.0 specification lists among the protocol’s features “a mechanism to notify interested parties when an abnormal disconnection occurs,” and the Will Flag in the CONNECT packet is that mechanism. The client tells the broker: if this connection ends without a proper DISCONNECT, publish this message on this topic with this payload.
The broker holds that instruction for the life of the connection. If the client disconnects cleanly, the broker discards the will. If the connection drops — power loss, network failure, a crash — the broker publishes the will message on the client’s behalf. The device does not need to be alive to report that it is gone. That is the whole point.
Home Assistant’s MQTT integration supports Birth and Last Will and Testament messages. By default, Home Assistant itself publishes online and offline to homeassistant/status, which is how other MQTT clients can learn whether Home Assistant is up.
The device side: one topic, two messages
ESPHome is a concrete example because its defaults are documented. ESPHome uses the last will and testament and birth message feature of MQTT to achieve availability reporting for Home Assistant. By default it sends a retained online message to <TOPIC_PREFIX>/status and instructs the broker to send offline on that same topic if the connection drops.
Two details matter more than they look:
- The birth message is retained. A retained
onlinepayload is what lets a freshly restarted broker or a newly subscribed client learn the current state without waiting for the next transition. Without retention, a subscriber that connects after the device came online sees nothing until the device disconnects. - Birth and will must share a topic. ESPHome’s documentation is explicit: if the birth message and last will message have empty topics or topics that differ from each other, availability reporting is disabled. A device with mismatched topics will look permanently present no matter what happens to it.
The MQTTMessage schema in ESPHome lets you set topic, payload, qos, and retain for each message. The defaults are QoS 0 and retain: true. For a status topic, retention is the setting you want to keep.
The limitation that changes how you use it
Home Assistant’s own documentation states that the LWT message is sent both in case of a clean disconnect (for example, Home Assistant shutting down) and in case of an unclean disconnect (for example, Home Assistant crashing or losing its network connection).
Read that again, because it is the part that gets skipped. An offline will message does not mean the device failed. It means the client’s connection to the broker ended. A planned reboot and a crashed node produce the same payload on the same topic.
So the will topic answers one question: is this client connected to the broker right now? It does not answer: is this sensor producing plausible readings? A device can hold its MQTT connection open while its sensor element is stuck, saturated, or reporting a constant value, and the status topic will stay green the entire time.
That is not a reason to skip it. It is a reason to know which failure class you have covered.
What the will topic covers, and what it does not
| Failure | Will topic catches it? | Why |
|---|---|---|
| Device loses power | Yes | TCP connection drops; broker publishes the will |
| Device loses Wi-Fi | Yes | Same — the broker sees the connection end |
| Device firmware crashes | Yes | Connection ends without a clean DISCONNECT |
| Device reboots on purpose | Yes, but ambiguous | Clean disconnect also fires the will per Home Assistant’s docs |
| Sensor element fails, radio still up | No | The client is still connected; it just publishes bad or no data |
| Sensor publishes a stuck value | No | Messages are arriving; the content is wrong |
| Broker is down | Everything goes offline together | Legible as a system-wide signal, not a per-device one |
The last row is worth sitting with. If the broker goes down, every device’s status topic goes stale or offline at once. That is not a false alarm — it is a correct signal that the availability layer itself is unavailable. It degrades legibly.
Diagnostic path: confirm the will is actually configured
Before trusting a status topic, verify it exists and behaves. The Mosquitto package ships command-line tools, and Home Assistant’s documentation points to mosquitto_sub and mosquitto_pub for testing.
Step 1 — subscribe to the status topic and check for a retained birth message.
mosquitto_sub -h 127.0.0.1 -v -t 'your_prefix/+/status'
If the device is online and the birth message is retained, you should see the online payload immediately on subscribe. If you see nothing, either the device is offline, the topic is wrong, or the birth message is not retained. Those are three different problems and the next step separates them.
Step 2 — force a disconnect and watch for the will.
With the subscription still running, cut power to the device (or block its network). Within the broker’s keepalive window, the will message should arrive on the same topic with the offline payload. If it does not, the will was never registered — check that the will topic matches the birth topic exactly.
Step 3 — confirm the retained state survives a broker restart.
Restart the broker, then subscribe again. A retained online message should still be there for any device that is currently connected. If it is gone, retention is off and the status topic only tells you about transitions, not current state.
Repeatable procedure
- Pick one status topic per device. A consistent pattern like
<prefix>/<device>/statusmakes wildcard subscriptions possible and keeps the mental model simple. - Set birth and will to the same topic. In ESPHome, override
birth_messageandwill_messagetogether. If they differ, availability reporting is disabled — this is documented behavior, not a bug. - Keep both messages retained. The birth message especially. Retention is what makes the current state readable by a new subscriber.
- Subscribe in Home Assistant. The integration supports LWT, so entities can go unavailable when the client disconnects rather than showing a stale value indefinitely.
- Pair it with a separate staleness check on the data topic. The will topic covers connection loss. It does not cover a sensor that is connected but silent, stuck, or wrong. Those need a different check — a timestamp comparison, a value-range sanity check, or a periodic heartbeat on the data topic itself.
When a plain switch is the better answer
If the device in question is a single light on a mechanical timer, or a switch that a person physically operates, the availability layer is overhead. A will topic is worth adding when a device’s silence would otherwise be invisible and consequential — a temperature sensor in a room you do not visit, a leak detector, a sump pump monitor. For a lamp on a schedule, the failure is self-evident the moment someone walks into the room.
The tradeoff is real: every status topic is another thing to configure, another thing to verify, and another thing that can be misconfigured into looking permanently healthy. Add it where absence is the failure mode you cannot see.
FAQ
Does the will message fire when a device reboots on purpose?
Yes. Home Assistant’s documentation states the LWT is sent on both clean and unclean disconnects. A planned reboot and a crash look the same on the status topic. The useful signal is the transition and its timing, not the offline state alone.
What happens if the broker goes down?
Every device’s status topic goes stale or offline at once. That is a correct signal that the availability layer itself is unavailable, not a per-device fault. It degrades legibly.
Can I detect a sensor that is connected but not reporting?
Not with the will topic alone. The client is still connected, so the broker has no reason to publish the will. That failure class needs a separate check on the data topic — a timestamp comparison or a value-range sanity check.
Does Home Assistant require a specific MQTT version?
Yes. Home Assistant requires that your broker supports MQTT protocol version 5. Most brokers, including the official Mosquitto app, do.
What if I use Zigbee2MQTT instead of ESPHome?
Zigbee2MQTT has its own availability mechanisms, but I have not verified the current documentation for this article and will not describe them from memory. Check the Zigbee2MQTT availability guide directly before relying on any specific configuration.
The boundary
A last-will check is worth adding before adding more sensors. It converts an invisible failure class — the device that is simply gone — into a visible one for the cost of one topic and one subscription. It degrades gracefully: if the broker is down, everything is offline together, which is itself a legible signal.
But it tells you the client is connected. It does not tell you the sensor is reporting plausible values. Those are two different questions, and conflating them is how a dashboard ends up showing a comfortable number from a device that stopped being useful three days ago.
For a broader look at which small systems in a home are worth auditing this way, see How to Audit the Small Systems That Quietly Run Your Week.