IMD SolutionComputing LibraryShop main store

Explainer

Why MQTT has no sensor values to fetch after a Node-RED restart

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

MQTT stores nothing you can query, so after a Node-RED restart a subscription alone returns nothing until the next publication. The current value comes from a retained message on each state topic; transitions missed while offline come from a resumed persistent session with the same client ID; a fresh reading needs a request on a command topic.

On this page
  1. A subscription is not a read
  2. Retained messages carry the last state
  3. A persistent session carries the missed transitions
  4. Replaying a 10 s sensor through a 3 minute Node-RED restart
  5. When startup needs a fresh measurement
  6. The decision for each case
  7. Questions asked

There is nothing in an MQTT broker to fetch, because MQTT is a routing protocol, not a database. A subscription tells the broker which topics to send you from now on; it does not ask for anything that already happened. After a Node-RED restart, only three mechanisms put values back on the wire: retained messages (the last published state, one per topic), the queue of a resumed persistent session (the transitions missed while the flows were down), or the sensor's next publication. Choose by requirement — retain for state, a session queue for transitions, a request on a command topic when startup must read the process fresh — and configure all three so a restart costs you nothing.

A subscription is not a read #

The data path is sensor to broker to Node-RED. The broker forwards each publication to the clients whose subscriptions match at that moment; the MQTT specification defines no operation that reads messages from the past. When the flows restart and the client reconnects, the subscription is active again and the Node-RED-to-broker path is proven, but nothing arrives until something is published, retained, or queued for you.

Two consequences follow. A connected client proves only its own path: it says nothing about whether the sensor is still publishing, so a dead sensor and an idle topic look identical from Node-RED. And topic matching is case-sensitive, so you can subscribe successfully to a topic no device publishes and simply stay quiet forever. Read the exact topic string from the publisher's configuration before changing any message settings.

Retained messages carry the last state #

When the publisher sets the retain flag, the broker stores that message as the state of its topic and delivers it automatically to every new matching subscription. Each later retained publication replaces the stored one, so a topic holds exactly one value — the most recent — and never a history. This state lives in the broker, so Node-RED picks it up after a restart with no cache of its own.

Test it directly: open a fresh subscription to the exact topic. If a message arrives with no new publication, it was the retained one, and the delivery carries the retained indication so the flow can label it. If nothing arrives, check the publisher's retain setting, the topic spelling, and read permission on that topic.

The retained value is the last reported state, not the present condition. It can predate a dead sensor by hours, and Node-RED's reception time only says when the message arrived — that gap is why a Node-RED dashboard shows stale MQTT sensor values long after the plant moved on. Carry the measurement time in the payload and show the age next to the number; for anything that acts on the value, age beyond what the process allows is bad-quality data, not current state.

A persistent session carries the missed transitions #

Retain hands back one value per topic. When the requirement is every change that happened while Node-RED was down, the mechanism is the session queue: the broker keeps the subscriptions of a persistent session and queues matching publications while the client is offline, then delivers them in order on reconnect. Standard QoS queues messages for an offline client only when several conditions hold at once:

  • The client reconnects with the same client ID, so the broker finds the stored session.
  • The connection does not discard that session: in MQTT 5, Clean Start is 0 and the Session Expiry Interval has not run out. A clean-session connection throws the queue away instead of resuming it.
  • The publications were sent at QoS 1 or 2 and the subscription was made at QoS 1 or 2 — the levels at which the broker owes delivery. A QoS 0 publication is never queued.
  • The broker itself kept the session. An unpersisted broker restart erases sessions and queues, which is why an MQTT subscriber stops receiving after a broker restart until it resubscribes.

The queue is a backlog, not a snapshot: its newest message is the current state, and everything before it is the transitions you asked to keep.

Replaying a 10 s sensor through a 3 minute Node-RED restart #

The case: one temperature publisher, and the question of what the broker can hand back when the flows come up.

Inputs, each with its source:

  • Publish interval: 10 s per message — an example rate; substitute your sensor's configured rate.
  • Restart window: 180 s from flows stopped to subscribed again — an example downtime; measure yours the same way.
  • Publication and subscription QoS: 1 — the lowest level at which the broker must queue for an offline session.
  • Clean Start 0 with a Session Expiry Interval of 3600 s — the documented MQTT 5 connection pair that keeps an existing session alive; the value is an example setting, sized below.

Rule, in words: while the session sits offline and unexpired, the broker queues every matching QoS 1 publication, one per publish interval, so the queue depth is the restart window divided by the publish interval, and the whole queue replays in order on reconnect.

Arithmetic: 180 s ÷ 10 s per message = 18 messages queued.

Result: on reconnect the broker delivers 18 messages in order. The 18th carries the newest state; the 17 before it are the transitions missed during the restart.

Decision: the queue exists only while the restart window stays below the Session Expiry Interval, and 180 s sits well under 3600 s, so the replay happens. Size the expiry above the longest restart you will tolerate — at 3600 s, any restart shorter than that keeps its queue, and a longer outage discards the session and leaves only retained state. Then split by requirement: a dashboard keeps the newest message and drops the other 17; an alarm or event log consumes all 18.

When startup needs a fresh measurement #

Retained and queued values are old by construction — the newest thing the broker knows, not the process. If the application must read the plant at startup rather than recall it, the sensor or its gateway has to support a request: a command topic it listens on and a response topic it answers on. Read both from the device documentation.

Order matters. Connect Node-RED and establish the response subscription before publishing the request, or a fast device answers before anyone is listening. Send the request without the retain flag, because a retained command is redelivered whenever a device subscribes or reconnects. Match the response to its request with the device's correlation field, sequence number, or measurement timestamp, wait out a configured deadline, and mark the value unavailable when it passes. The broker routes that exchange; it never turns a subscription into a sensor read.

The decision for each case #

Commit by requirement:

  • Current value on a screen after a restart: retain on every state topic. One message per topic, delivered on subscribe, no cache in Node-RED — and show its age.
  • Every change missed during downtime: a persistent session, meaning a fixed client ID, Clean Start 0, a Session Expiry Interval above the longest restart, and QoS 1 or 2 on both the publication and the subscription.
  • A reading taken after startup: a request on the device's command topic, response subscribed first, with a deadline and stale marking on timeout.
  • Trends and history: not MQTT's job. Write values to a store at the edge as they arrive — routing MQTT sensor data into Grafana on an edge PC is one way to keep them queryable — and leave restart behavior to retained state and the session queue.

The first three compose. Retain for state, the session for transitions, requests for freshness: configured together, a Node-RED restart costs you neither the current picture nor the events in between.

Questions asked

Does the retained value survive a broker restart?

Only when the broker is configured to persist it. Delivery after a Node-RED restart needs nothing but a running broker, but delivery after a broker restart needs the retained messages and the sessions written to disk, which is broker configuration the client cannot request. Verify it by restarting the broker itself: a fresh subscription should still receive the retained value, and the reconnecting client should still get its queue.

Do clients that are already subscribed also receive a retained publication?

Yes. Retain is an addition, not a redirect. A publication with the flag set is delivered to every client already subscribed exactly like a normal message; the flag only makes the broker additionally store it for future subscribers. Existing subscribers therefore see each retained publication the moment it happens, and only new or reconnecting subscribers receive the stored copy on subscribe.

Can a second Node-RED instance use the same client ID?

No. When a new connection presents a client ID the broker already knows, it takes over that session and disconnects the previous connection. Two instances sharing one ID will therefore disconnect each other repeatedly, and neither holds a stable queue; a development instance can silently steal the production session. Give each instance its own ID and keep that ID fixed across restarts so the intended session resumes.

What changes with MQTT 3.1.1 instead of MQTT 5?

MQTT 3.1.1 has a single clean-session flag and no Session Expiry Interval. With the flag off, the broker keeps the session, but how long it keeps it while you are offline is the broker's decision, not a number you set. MQTT 5 splits the control: Clean Start governs what the connection does to an existing session, and the Session Expiry Interval states in seconds how long the session outlives the connection.

Keep reading

  • Troubleshooting

    Fix MQTT sensor data missing in Node-RED

    A green MQTT connection in Node-RED proves only a broker session, not that the GSM module published a value or that the flow subscribes to its topic. An http in node cannot receive MQTT publications at all: use an mqtt in node. Trace RS-485 acquisition, publish result, broker identity and topic match in order, and fix the first stage with no positive result.

    Edge computing and IIoT

  • Troubleshooting

    Why a Node-RED dashboard shows stale MQTT sensor values

    A Serial Monitor reading proves only the stages before publish, so a stale Node-RED dashboard value comes from after: retained MQTT messages stored under generic topics, or conversions run on failed two-byte I2C reads. Clear the retained messages, gate each read on a two-byte return, publish one verified sample per 5000 ms interval, and move the topics to your own broker.

    Edge computing and IIoT

  • How-to

    Buffer MQTT data in Node-RED during an internet outage

    The MQTT out node's in-memory store dies with the process, so an internet outage loses every sample the flow produces while disconnected. Stamp each sample at acquisition, derive a broker-up flag from the MQTT node status, and persist the queue in a file-backed context store with a capped size. Drain it oldest-first over QoS 1 when the connection returns.

    Edge computing and IIoT

Why InfluxDB in Docker keeps CPU high after a restart