How to Set Up MQTT Last Will for Device Offline Detection
A fleet dashboard needs to show each remote device as clearly online or offline, and MQTT's last will is the mechanism built for exactly that - but only if you pair it with a birth message and set the flags correctly. A will alone, without a matching birth and the right retain and delay settings, gives a status that flickers or sticks. This page walks the full birth-and-will pattern so device availability is trustworthy.
Set Up MQTT Last Will for Offline Detection in one line: To set up MQTT last will for offline detection, register a retained will message that publishes an offline status to a per-device status topic when the broker loses the client, and have the device publish a matching retained online birth message right after it connects. The broker fires the will on ungraceful disconnect; the birth clears it on reconnect.
Register the Will at Connect Time
The last will is configured in the CONNECT packet, not published later - the client hands the broker a topic, a payload, a QoS, and a retain flag, and the broker holds it. If the client disconnects ungracefully - a lost link, a crash, a keepalive timeout - the broker publishes that will on the client's behalf. So the will is your dead-man's switch: the device tells the broker in advance what to announce if it vanishes.
Point the will at a per-device status topic, with an offline payload, so each device has its own availability signal. Set retain on the will so a dashboard connecting later immediately learns the device is offline rather than waiting for the next event. The mechanism in full is in the MQTT last will and testament.
Publish a Matching Retained Birth Message
A will that announces offline is only half the pattern; you need something to announce online. Immediately after the client connects, have the device publish a retained online message to the same status topic the will targets. This birth message overwrites the retained offline value, so the status topic always holds the device's true current state - online after a successful connect, offline after the broker fires the will.
Both messages must target the identical status topic and both must be retained, or a dashboard connecting between events sees nothing or a stale value. The birth-and-will pair is a single coherent scheme: the device says online when it arrives and pre-arranges to be marked offline when it leaves. This is the same discipline Sparkplug formalizes - see the Sparkplug NBIRTH and NDEATH message.
Set the Will Delay to Avoid False Offlines
A brief network blip can make the broker fire the will even though the device is fine and about to reconnect, flickering the dashboard to offline and back. The will delay interval solves this: it tells the broker to wait a set period after the disconnect before publishing the will, giving the device a chance to reconnect first and cancel the pending will. Reconnect within the delay and no false offline is ever published.
Set the delay to cover your expected transient reconnect time but not so long that a genuinely dead device shows online for ages. It is a balance between false positives and detection speed. The knob and its behavior are covered in the MQTT will delay interval; tune it to the link's real reconnect behavior.
Tie Detection Speed to the Keepalive
The broker only fires the will after it decides the client is gone, and for an ungraceful disconnect that decision is driven by the keepalive - roughly one and a half keepalive intervals of silence. So your offline-detection latency is set by the keepalive, not the will. A long keepalive means a device can be gone for a while before the will fires and the dashboard updates.
Set the keepalive to the detection speed the operation requires, remembering the cellular constraints, then the will delay adds its wait on top. Together they define how fast and how reliably a device shows offline. Tune the keepalive with setting an MQTT keepalive for a cellular link so detection is neither sluggish nor jumpy.
Verify the Online and Offline Transitions
Test both transitions explicitly. Kill a device abruptly - pull power, not a clean shutdown - and confirm the status topic flips to offline after the keepalive and will-delay window, and that a dashboard connecting afterward reads offline from the retained value. Then bring the device back and confirm its birth message flips the status to online promptly.
Test the false-offline case too: briefly interrupt the link within the will delay and confirm the device reconnects before the will fires, so the status never flickers to offline. A birth-and-will scheme that survives both a real death and a transient blip without lying about either is the one that makes a fleet availability view trustworthy.
Common Mistakes
The most common mistake is setting a will but no birth message, so the status can go offline but never authoritatively back to online, leaving a device stuck offline on the dashboard after it recovers. The second is forgetting retain on either message, so a dashboard connecting between events sees no status at all.
Another is omitting the will delay, so every brief blip flickers the device offline and erodes trust in the whole availability view. And do not expect the will to fire on a clean disconnect - a graceful disconnect normally suppresses the will, which is correct, but means you must publish an explicit offline on planned shutdown if you want that state recorded.
Frequently Asked Questions
How does MQTT last will detect a device going offline?
The device registers a will in its CONNECT packet - a topic and an offline payload. If the client disconnects ungracefully, the broker publishes that will on its behalf after deciding the client is gone. Point the will at a retained per-device status topic so any dashboard immediately reads the device as offline, and pair it with an online birth message.
Why does my device flicker between online and offline?
A brief network blip makes the broker fire the will before the device reconnects, flipping the status offline and back. Set a will delay interval so the broker waits before publishing the will, giving the device time to reconnect and cancel it. Tune the delay to cover normal transient reconnects without hiding a genuinely dead device for too long.
Do I need both a will and a birth message?
Yes. The will announces offline when the device vanishes, and a retained birth message published right after connect announces online and clears the offline state. Without the birth, a recovered device can never authoritatively return to online and stays stuck offline on the dashboard. Both must target the same status topic and both must be retained.
Sources and verification
This page references the protocol specifications published by the organizations below. Editions, product capabilities, and documentation change over time - confirm current requirements and specifications directly with the source.
- MQTT Version 5.0 (OASIS Standard) - OASIS (v5.0, 2019)
Merobix is not affiliated with, endorsed by, or sponsored by these organizations; their names are used only to identify the standards and products discussed.
Automation services
Need help turning this into a working system?
Merobix integrates SCADA, programs Allen-Bradley and Siemens PLCs, and designs and fabricates industrial control panels.
Meeting requests are reviewed before confirmation.