Katman MQTT Client
Loading account

How Do You Know an MQTT Device Has Gone Offline?

Use MQTT Last Will and a status topic to detect unexpected disconnects. Learn about detection delays and check device status with the Katman MQTT client.

By Katman

A temperature sensor stops sending readings. Is it disconnected, or has the temperature simply stayed the same?

Silence alone cannot tell you. MQTT's Last Will and Testament, often called LWT, lets the broker publish a message when it detects that a device's connection has ended unexpectedly.

Give the sensor a status topic

Consider a sensor with a separate status topic:

factory/line-1/sensor-7/status

Configure the sensor's MQTT connection with a Will payload of offline on that topic, QoS 1, and Will Retain enabled. After connecting successfully, the sensor publishes online to the same topic, also retained.

The sensor supplies its Will when it connects. The broker holds it until it is needed; setting a Will does not immediately publish offline.

The sensor publishes online after connecting. If its connection is lost unexpectedly, the broker detects the loss and publishes offline.

Retaining both messages gives a new subscriber the latest saved status. Retaining only online leaves a trap: someone subscribing after the failure could still receive that older online value. Our retained-message guide explains why.

Offline does not always appear immediately

A broken network connection can take time to detect. Keep Alive helps the broker notice a silent connection; it does not make failure detection instant.

MQTT 5 can also use a Will Delay Interval to allow a brief reconnection before announcing the loss. The session settings affect that behavior. These rules are defined in the MQTT specification.

A normal, graceful disconnect suppresses the Will. For a planned shutdown, have the sensor publish retained offline before disconnecting normally.

Watch the change in Katman

Connect the Katman MQTT client to the same broker with its own client ID and subscribe to the sensor's status topic. Keep Katman connected while a test sensor connects, loses its connection unexpectedly, and reconnects. Expect online, then offline once the broker detects the loss, then online again.

The Will belongs to the sensor's connection. Katman observes the messages; it does not need a Will configured to watch another device's status.

Treat this as connection status, not a complete health check. A sensor can stay connected while its measurements stop updating. Check measurement timestamps or an expected reporting interval too, and show status as unknown if your dashboard loses its own broker connection.