MQTT retained stale sensor value startup - PLC Playground troubleshooting guide

MQTT Shows a Value Before the Sensor Starts? Check the Retained Flag

A new subscriber can receive a retained value that was published before it connected. Immediate data arrival does not establish current sensor health. Inspect message age and application status before accepting it as a fresh measurement.

MQTT application design example; behavior depends on protocol version and configuration.

What is happening?

Retained messages provide the latest retained value for a topic, not a complete history or proof that its producer is alive. The application must define timestamps, validity, and how retained state is cleared or replaced.

Check these five things

  1. Inspect topic, retained delivery, payload timestamp, and quality fields.

  2. Compare the publisher lifecycle with subscriber connection time.

  3. Define acceptable measurement age.

  4. Test producer absence and subscriber reconnect.

  5. Specify how invalid or retired measurements are represented.

Worked example

A teaching subscriber connects at noon and immediately receives a retained temperature from 09:00. The number looks normal but the publisher has been offline for three hours. Payload time and a health contract prevent that value from being presented as a live reading.

Quick diagnostic reference

Property Meaning
Retained Stored topic state
Recent timestamp Age evidence
Producer health Separate status contract

The mistake to avoid

Do not use retained action commands casually. A reconnecting consumer can receive an old instruction, so command lifecycles require an explicit reviewed design.

How to verify the fix

Verify first connection, reconnect, stale publication, and producer restart. Confirm the display and downstream consumers distinguish retained historical state from accepted fresh data.

Field-work boundary: These are educational diagnostic guides. Use the exact equipment manuals and approved site procedures. Electrical testing and machinery changes belong to qualified, authorized personnel; control hazardous energy and validate affected functions before release.

Technical reference

HiveMQ: MQTT Essentials. The examples and diagnostic tables above are original teaching material; they do not replace the product manual.

Continue troubleshooting

Previous Post