IMD SolutionComputing LibraryShop main store

Troubleshooting

Why your MQTT publisher sends data after a power cycle or run-to-stop

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

Short answer

When an MQTT publisher transmits on power cycle or run-to-stop without its enable bits, the message is almost always the client's connect-time snapshot or a retained message the broker replays, not your logic. Gate the publisher enable with a first-scan false bit, clear or filter retained messages, and move the runtime to the vendor's current release before rewriting working logic.

On this page
  1. Three causes, in the order to check them
  2. Read the broker log first
  3. Clear or ignore retained messages
  4. Update the runtime before rewriting logic
  5. Gate the publish on a first-scan false bit
  6. Verify before returning it to service
  7. The decision for each case
  8. Questions asked

If your MQTT publisher transmits right after a controller power cycle, a run-to-stop or a stop-to-run transition while every enable bit is false, your logic did not turn it on. The message is one of three things: the client's connect-time snapshot, a retained message the broker replays to the new session, or a defect in the runtime version. Two of the three are fixed outside the program, in how you gate the publish and in the broker's stored message state. Work the causes in the order below, and update the runtime before you rewrite working logic.

Three causes, in the order to check them #

  • The connect-time snapshot. Many controller MQTT services publish a full set of topics the moment the client session establishes. The enable and trigger bits govern change-of-state and periodic publishing; the handshake snapshot ignores them.
  • A retained-message replay. In MQTT 3.1.1, a message published with the retained flag set is stored by the broker and delivered to every future subscriber of that topic, even if your publisher is disabled and never sends again.
  • A runtime defect. A client defect can re-arm one publisher across power and mode transitions. It shows up as the same single publisher firing every time, with no change in your logic.

Each cause leaves a different fingerprint in the broker log. Read the log before you change anything.

Read the broker log first #

Enable broker-side logging for that client, or capture the packets on the broker's port during one power cycle, and note the timestamps of the CONNECT and the unwanted PUBLISH plus the retained flag on each message. The pattern decides the cause:

  • PUBLISH in the same second as CONNECT, retained flag set: the broker replayed a stored retained message. Your publisher may not have transmitted anything at all; delivering a retained publish ahead of the subscription acknowledgment is legal broker behavior, which is the situation described in MQTT Engine disconnect: retained PUBLISH before SUBACK is legal.
  • PUBLISH in the same second as CONNECT, retained flag clear: the client published its connect-time snapshot on its own.
  • PUBLISH on the run-to-stop or stop-to-run edge while the session stays up, retained flag clear: something re-arms that publisher on mode change. Go to the runtime version before the program.

Worked example. Your inventory logic requires the conveyor-run bit to hold for 5 s before it sets the publisher enable. The broker log shows CONNECT at 14:32:07 and the unwanted inventory PUBLISH at 14:32:07, retained flag clear. The gap between connect and publish is 0 s, which is less than the 5 s your logic needs to set that enable, so the enable bits were provably false when the message went out. That is the connect-time snapshot, not your logic. If the publish had carried the retained flag instead, the same timestamp would still clear your logic: the broker, not the controller, produced the message.

Clear or ignore retained messages #

Two fixes, and you should use both:

  • Clear the stored message. From any client, publish a message to the exact same topic with the retained flag set and an empty payload. The broker replaces the stored message with the empty one and sends nothing to the next subscriber of that topic.
  • Filter on the consumer. Have the server-side consumer check the retained flag on inbound messages and drop the ones that carry it. The first fresh publish after connect then carries the current state, and nothing from the broker's history reaches your inventory table.

Update the runtime before rewriting logic #

In one reported case, the controller's programming suite ran version 4.5.0.34, publisher1 of a single client fired on every power cycle and mode transition, and the vendor's first instruction was to move to the current release, 4.7.4.2, before touching the program. The single-publisher, single-client pattern is the tell: if the current release cures it, it was a runtime defect; if it persists on the current release, the fingerprint in the broker log points at that publisher's trigger configuration or stored topic state. Get the runtime only from the vendor's software download page, and record the version you leave the plant with. For the gating structure these controllers publish with, Productivity 1000 MQTT publish on trigger publisher enable shows the same trigger-and-enable arrangement this fix preserves.

Gate the publish on a first-scan false bit #

Build a bit that is false for the first scan after every power-up and every entry into run. Most PLC instruction sets document a first-scan system bit for exactly this purpose; where yours has none, build one with a coil sealed in by its own normally open contact: false on the first scan, true from the second scan on, and it resets itself on every run-mode entry.

Then put that bit's normally closed contact in series with the publisher enable, so that the enable evaluates as the real enable condition AND NOT the first-scan-complete bit. During the first scan after a power cycle or a stop-to-run, the bit is false, so the publisher enable is forced false no matter what the held bits say. Your logic then has to set the enable deliberately, and the snapshot or re-arm publish never leaves the controller. Because first-scan logic re-executes on every entry into run, this one gate covers both the power cycle and the mode transition.

Verify before returning it to service #

  1. Turn on broker-side logging with the retained flag per message, or capture packets on the broker port for the whole test.
  2. Power-cycle the controller three times. Expect zero publishes from that client between each connect and the first deliberate enable.
  3. Toggle run to stop and back three times. Expect the same zero.
  4. Force the real enable condition and confirm the publishes match the trigger exactly: one per qualifying change, no burst.
  5. Keep the consumer-side retained filter in place as a second layer even after the fix passes.

Run these tests during planned downtime, because power-cycling the controller stops the machine.

The decision for each case #

  • Retained flag set on the unwanted publish: broker replay. Clear the stored topic message and drop retained messages on the consumer. No program change is needed.
  • Retained flag clear and the publish lands in the same second as CONNECT: connect-time snapshot. Install the first-scan gate; the rest of your program is fine.
  • Publish lands on the run-to-stop or stop-to-run edge with the session still up: move the runtime to the vendor's current release first. If it still fires there, capture the broker log and the runtime version and report it to the vendor. Do not add server-side masking that silently drops records, because it will eventually swallow a legitimate publish too.

Questions asked

How do I clear a retained message that replays on every connect?

The broker stores that empty message in place of the old one and no longer delivers anything to the next subscriber of that topic. Confirm from a separate test client that a fresh subscription returns nothing before you reconnect the publisher.

Why does only one publisher of one client do this?

Because that publisher differs from the others in either its trigger configuration or its topic history. Check whether its topic ever carried a retained message and whether its trigger is change-of-state rather than a level. If it alone fires on every power cycle and mode transition on the current runtime version, report it to the vendor with a broker-side log.

Should the enable bits in my logic stop a connect-time publish?

Not by themselves. On many controllers the enable and trigger bits govern change-of-state and periodic publishing, while the client still sends a snapshot when the session establishes. Gate the publisher enable with a first-scan false bit if the consumer must never receive unsolicited records, and keep a retained-message filter on the consumer as a second layer.

What if the unwanted publishes continue after the update and the gate?

Capture the broker log with timestamps and retained flags for the exact messages and compare them against the controller's session connects and mode transitions. If a publish still lands in the same second as a connect or a mode change while the gated enable is provably false, send that log and the runtime version to the vendor rather than adding masking on the server.

Keep reading

  • Troubleshooting

    Why a Flexy BASIC script publishes MQTT once then stops

    The SENDDATA timer handler publishes only when MQTT STATUS returns 5, and the script never calls MQTT CONNECT again after a failure, so one LSTERR 28 at startup or any later drop silences every following cycle. Trace STATUS on each cycle, add a controlled reconnect to the connect label, and move the SUBSCRIBE loop after CONNECT.

    Edge computing and IIoT

  • Troubleshooting

    Why buffered Sparkplug B data is rejected after reconnect

    Replayed samples are dropped because the MQTT Publisher component in System Platform v2023 P01 sends them without the per-metric is_historical = true flag the Eclipse Sparkplug B specification defines for late data, so the broker treats old timestamps as suspect. No setting in that component sets the flag; keep MQTT for live values and recover the outage window through a separate backfill path.

    Edge computing and IIoT

  • How-to

    Bridging OPC UA PLC data to MQTT with an edge PC

    Keep the OPC UA server on the PLC side and give MQTT publishing its own role on a separate edge PC that acts as the OPC UA client and MQTT publisher. Commission the two links independently — first a verified tag read, then a verified subscriber message — and only then widen the tag set.

    Edge computing and IIoT

Why a historian stops after storage deletion errors