Katman MQTT Client
Loading account

Why Am I Receiving Old MQTT Data After Connecting?

Old MQTT data may be retained messages. Check freshness and the retain flag with the Katman MQTT client to understand why stale values appear after connecting.

By Katman

You connect your MQTT client, subscribe to a topic, and a value appears immediately. The device has been offline for hours. Where did that message come from?

Often, the answer is a retained message: a value the broker saved earlier and delivered when you subscribed. Receiving it now does not mean the device just sent it.

What the broker remembers

When a publisher sets the retain flag, the broker can keep that message for future subscribers. A new subscription normally receives the saved message for each matching topic. MQTT 5 subscription options can change this behavior.

The broker keeps one retained message per topic. It gives a dashboard a last known value immediately, but that value may no longer describe the device.

There is an easy trap here: a newer message published without retain does not replace the older retained value. Existing subscribers may see the new reading, while someone subscribing later receives the older saved one. The MQTT specification defines this behavior.

A recent arrival can contain an old measurement

The client's receive time tells you when a message arrived. A timestamp in the payload can tell you when the measurement was taken, if the device clock is reliable.

Include a measurement timestamp when freshness matters. Agree on how old a value may be before your application marks it stale, and show its age alongside it. A temperature without a time can look reassuring long after a sensor stops reporting.

A retained temperature alone does not prove that a device is online.

What to check when old data appears

  • Look at the received retain flag. It helps identify retained delivery, but does not tell you the message's age. MQTT 5's Retain As Published option also affects how this flag is forwarded.
  • Compare the payload's timestamp with the arrival time. If the payload has no measurement time, you may be unable to determine how old the reading is.
  • Check the publisher's retain setting. Updates to a latest-state topic need a consistent retention policy.
  • Check session behavior too. A resumed persistent session can deliver queued messages from while you were offline. Old data has more than one possible cause.

The Katman MQTT client shows the received retain flag in the message table and a Retained label in the inspector. Examine those alongside the payload when investigating unexpected readings.

Keep useful state, retire stale values

Retained messages suit state that a new subscriber needs immediately, such as a reported operating mode. Retaining a one-time command can deliver an old instruction long after its purpose has passed.

Turning retain off for future publishes leaves an existing retained value in place. To clear it through MQTT, publish a retained message with a zero-byte payload to that exact topic. Clearing your client's message view does not clear the broker's saved value.

MQTT 5 Message Expiry Interval can limit how long a message remains available for delivery. Your application still needs a freshness rule for values already received: show their age, mark them stale, or remove them when updates stop arriving.