Detect device offline status using MQTT Last Will and application-level heartbeats

domain: iot-general · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Configure LWT in every device's CONNECT packet: set willTopic to devices/<id>/status, willMessage to {"online":false}, willQoS to 1, and willRetain to true so new subscribers immediately see the last known status
  2. On successful connection, immediately publish {"online":true} with retained=true to the same devices/<id>/status topic to overwrite the LWT from any previous session; this establishes a clear online indicator
  3. Implement an application-level heartbeat: have each device publish a small message (e.g., {"ts":<epoch_ms>}) to devices/<id>/heartbeat on a fixed interval (e.g., every 60 seconds) independent of telemetry traffic
  4. On the backend, maintain a last-seen timestamp per device updated on every heartbeat receipt; run a periodic job (e.g., every 30 seconds) that compares now() against last-seen and marks devices as offline if they have not been heard within 2-3 heartbeat intervals
  5. Combine both signals: use LWT for fast detection of clean disconnects and the heartbeat timeout for detecting silent failures (device hangs, network black-hole) where TCP stays open but the device stops sending
  6. Emit offline events to an alerting or logging system when a device transitions to offline; include the last-seen timestamp and the reason (LWT vs heartbeat timeout) to distinguish clean disconnects from crash or network failure scenarios

Known gotchas

Related routes

enable and tune zigbee2mqtt's device availability feature to detect offline zigbee devices
zigbee2mqtt.io · 5 steps · unrated
Understand and apply MQTT QoS levels, retained messages, and Last Will and Testament in practice
mqtt.org · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans