MQTT 5 Will Delay gives a robot client a grace period between losing its network connection and having the broker publish its Will Message. If the client reconnects and continues the same Session before the delay expires, the broker must suppress that Will.

This behavior reduces false offline alarms during brief wireless roaming or cellular handoffs. It is not a generic timer that always fires after disconnection. Session lifetime, reconnect choices, normal shutdown, and the reason the Session ends can all change the publication time.

The broker stores a conditional message

When CONNECT sets the Will Flag, the Will Topic, payload, QoS, retain flag, and Will Properties become part of server Session State. Will Delay Interval is a Four Byte Integer measured in seconds. If omitted, it defaults to zero, so there is no delay before normal Will publication conditions are evaluated.

After an abnormal connection close, the server delays the Will until the interval passes or the Session ends, whichever happens first. This whichever first rule is crucial. A Will Delay of 60 seconds cannot keep the message pending for a minute if Session Expiry is zero, because the Session ends as soon as the network connection closes.

For a grace period to work, Session Expiry must normally be longer than Will Delay. A gateway might use a 30-second Will Delay and a 300-second Session Expiry. That gives it 30 seconds to reconnect and continue the Session without announcing an outage, while preserving subscription state longer if recovery takes more time.

Recovery can cancel the pending Will

If a new network connection continues the same Session before Will Delay elapses, the server must not publish the Will. The reconnect uses the same Client Identifier with Clean Start 0, and the broker reports Session Present 1 when stored state exists.

Suppose a mobile robot loses Wi-Fi at second 0 with a 30-second delay. It reconnects at second 12, resumes the Session, and continues telemetry. The pending offline Will is cancelled. Subscribers should not see a brief offline event followed immediately by online status.

If the robot reconnects at second 40, the delay has already elapsed and the broker should have published the Will. The later reconnect cannot retract a message already delivered. Applications need an online announcement or current-state topic to supersede it.

A new Session is not recovery

Opening a connection with the same Client Identifier does not always suppress the Will. If Clean Start is 1, the existing Session ends. MQTT 5 says the Will for the exiting connection is sent because the Session ends. The new connection is a replacement, not continuation.

Similarly, if Session Expiry occurs before the delay, the server publishes the Will when the Session ends. Setting Will Delay longer than Session Expiry can intentionally make the Will signal Session expiration, but it does not postpone publication beyond that expiration.

A second connection that takes over a Client Identifier requires careful testing. The suppression behavior depends on continuing the existing Session with Clean Start 0 and a positive delay. Do not treat any socket reconnect as proof that the Will was cancelled.

Normal disconnection removes the Will

A DISCONNECT with Reason Code 0x00, Normal disconnection, removes the stored Will Message. This is different from waiting for Will Delay. A planned robot shutdown should announce its state explicitly if applications need that information, then disconnect normally so the broker does not later publish an abnormal-failure Will.

MQTT 5 also defines DISCONNECT reason 0x04, Disconnect with Will Message, for cases where a client deliberately requests Will publication. This can be combined with timing behavior for specific workflows, but operators should distinguish it from an unexpected network failure.

Will Retain and Will QoS control how the message is delivered once publication occurs. They do not change whether Session recovery suppresses a pending Will. Message Expiry on the Will becomes the publication expiry interval when the Will is published; it is separate from Will Delay.

Design the robot presence contract

Choose a Will Topic that represents communication status, not physical safety. A missing MQTT connection does not prove a robot is stopped, powered down, or unsafe. The payload should identify the client, include a state such as communication_lost, and carry application timestamps or sequence information needed by consumers.

Set Will Delay from measured reconnect behavior. It should exceed common transient outages without hiding meaningful loss for too long. Set Session Expiry long enough to cover that delay and the desired subscription recovery window. Document the pair together because reviewing either value alone is misleading.

Consumers should use a state machine. An offline Will is evidence that the broker's publication condition occurred. A later online message with a newer application timestamp can restore connected status. Safety systems need independent, validated channels rather than inferring machine safety from MQTT presence.

Exercise every race

Disconnect a test gateway abnormally and reconnect the same Session before the delay. Confirm no Will appears. Repeat after the delay and confirm one Will appears. Use Session Expiry shorter than Will Delay and verify publication occurs when the Session ends.

Reconnect with Clean Start 1 before the timer expires and confirm the old Session's Will is published. Send normal DISCONNECT and verify the Will is removed. Test broker restart behavior, because the specification permits delayed publication after a server failure until a later restart.

Record connection generation, Client Identifier, Clean Start, Session Present, Will Delay, Session Expiry, disconnect reason, and actual Will publication time. These fields explain whether suppression was correct.

Will Delay is a recovery grace period tied to Session continuity. It prevents transient network loss from becoming an immediate offline signal, but only when the client resumes the same Session before the earlier of delay expiration and Session end.