Stop a Stale HMI Command From Replaying After a PLC Restart
A pump nobody started runs after a power blip. The six places a command survives a restart — retentive bits, MQTT RETAIN, driver write cache — and the sequence number that ends it.
The power blipped once at 2 a.m. The PLC came back, and the feedwater pump started before the operator got back to the screen. Nobody pressed anything. The Start pressed four hours earlier was still sitting true in retentive memory, and the PLC read it as a new command on the scan after first scan.
This is about where that bit hides before it comes back, and why one sequence number ends the whole class of problem. Normal operation is fine: the operator presses, the HMI writes a bit, the PLC sees it, the bit drops. It only breaks when the PLC, the driver or the HMI client dies in that gap — and that moment almost never arrives during commissioning.
Six places a command survives
Commands do not default to disappearing. Nearly every industrial stack is built to preserve values, so the forgetting is the part you have to ask for explicitly.
| Where it lives | Why it survives | How to check |
|---|---|---|
| PLC retentive memory | The value is already restored before Logix's S:FS (first scan) runs | Set the bit true, then power cycle |
| MQTT retained message | MQTT v3.1.1 §3.3.1.3 — a publish with RETAIN set is stored by the broker and delivered immediately to every new subscriber on that topic | mosquitto_sub -t <cmd topic> on a fresh subscription |
| SCADA driver write cache | Store-and-forward queues writes during a comms break and flushes on reconnect | Driver log, right after reconnect |
| Redundant server sync | A pending write replicates to standby and is reissued after failover | Run the failover right after a command |
| HMI tag default value | Client restart applies the tag's initial value | Restart only the HMI client |
| Script variables | Global scope that outlives a screen change | Leave the popup open and change screens |
MQTT is the one you can pin down with a clause, so check it first. A retained command topic stays there until something clears it — and the only way to clear it is to publish a zero-length payload with RETAIN set to the same topic (v3.1.1 §3.3.1.3). On MQTT v5.0 you can shorten its life with the Message Expiry Interval property (identifier 0x02), but the right value for a command topic is not an expiry time. It is not using RETAIN at all.
One request bit is not enough
A single-bit command has no way to separate "requested" from "executed". The minimum is four signals.
| Signal | Direction | Width | What it does |
|---|---|---|---|
Cmd_Req | HMI → PLC | BOOL | Requests the action |
Cmd_Seq | HMI → PLC | DINT | Separates a new request from a replayed one |
Cmd_Ack | PLC → HMI | INT | 0 pending, 1 accepted, negative values are rejection reasons |
Cmd_Last | PLC → HMI | DINT | The last sequence the PLC actually processed |
Make Cmd_Seq an INT and it wraps at 32767, and at that moment the comparison decides the request is old. Use DINT or UDINT. Doing the wraparound comparison properly is its own problem.
Returning Cmd_Last to the HMI is the part people skip. Without it the HMI has to guess whether its request landed, and an HMI that guesses writes again on reconnect.
The PLC consumes the request, not the HMI
Letting the HMI drop the bit breaks in both directions. Drop it too fast and the PLC scan never sees it — a 200 ms HMI pulse against a 10 ms scan is fine, but add remote I/O and an OPC UA subscription and the publishing interval alone costs hundreds of milliseconds. Drop it too slowly and the PLC treats true as a new command every scan.
The PLC-side order is:
- Check
Cmd_Reqis true andCmd_Seq <> Cmd_Last. If not, done. - Check mode, permissives, interlocks and authorization.
- Write
Cmd_Last := Cmd_Seqbefore starting the action. This line is what prevents re-execution. - Start the action and write the result to
Cmd_Ack.
Put step 3 after step 4 and a PLC that dies in between runs the command twice. The order is the whole mechanism.
IEC 61131-3 defines R_TRIG and F_TRIG as standard function blocks for edge detection. Convenient, but they do not survive a restart: after a restart the block's internal state is at its initial value, so a held true bit looks like a rising edge on the first call. The sequence comparison is not a replacement for R_TRIG — it covers the window R_TRIG cannot see.
How much the protocol gives you
What you get for free depends on what you are speaking.
- Modbus gives you nothing. FC05 (Write Single Coil) carries value
0xFF00for ON and0x0000for OFF (Modbus Application Protocol v1.1b3 §6.5), and the request has no sequence and no idempotency marker. Modbus TCP's 2-byte Transaction Identifier exists to match responses to requests, not to stop replays. You build all of it yourself — see coil and register write safety. - DNP3 has select-before-operate. IEEE 1815 puts a Select before the Operate, and the outstation rejects an Operate that arrives after its select validity window closes. A transaction cut short by a restart therefore expires on its own. Direct Operate skips the protection entirely. More in select-before-operate.
- OPC UA differs by service. The Write service (OPC UA Part 4) changes a value, and values persist. A Method call is an invocation — it does not get reissued by a reconnect, and it returns a rejection reason as a StatusCode such as
Bad_UserAccessDeniedorBad_OutOfRange. For commands, Method beats Write; calling a Method from an HMI button covers it.
The startup gate is not "online"
A PLC being up is not the same as a PLC being ready to accept commands. Just after first scan, remote I/O is still connecting, drive comms are absent, and interlock inputs read 0. A 0 interlock does not mean safe. It means unknown.
Conditions to pass before enabling commands:
- First scan complete, and every I/O connection healthy.
- Interlock and permissive data not stale — judge on update timestamp, not value.
- Device feedback current.
- Mode source (local / remote / auto) determined.
- Time sync valid if the audit needs a timestamp. A few seconds of NTP error here is the difference between proving and not proving whether the operator pressed twice.
And when it is blocked, put the reason on the screen. ISA-101.01 holds that the HMI must tell the operator the state of the system; a greyed-out button with no reason does the opposite. In the field a reason-free grey button produces exactly two things — a phone call, then a bypass. Rejected — I/O initializing, 8 s produces neither.
Audit the transaction, not the click
IEC 62443-3-3 asks for recorded access-control and operator-action events in SR 2.8 (Auditable events), trustworthy times for those records in SR 2.11 (Timestamps), and attribution in SR 2.12 (Non-repudiation). One row per mouse click meets none of the three.
What belongs in the row:
- Operator ID and workstation.
- Command name, target equipment, requested value.
- The
Cmd_Seqvalue. Without it you cannot tell a double-click from a replay. - Three timestamps: request, accepted, result.
- PLC result code or rejection reason.
- Whether a controller restart or comms break occurred during the transaction.
That last field is the one most often missing and the first one wanted during an incident review. Manual override audit trails covers the record design.
Restart testing from idle proves nothing
Restart a PLC while nothing is happening and nothing happens. The failures live mid-transaction, so pick the awkward moment deliberately, in safe simulation or a controlled commissioning window.
- Press the command, restart the HMI client before the PLC writes
Cmd_Last. - Press the command, restart the PLC immediately after it writes
Cmd_Last. - Break the HMI–controller network right after a command and restore it 60 s later.
- Restart the SCADA server with a confirmation popup open.
- Restart a gateway that has a write buffer.
- Fail over a redundant server pair with a command pending.
- Power up the PLC with the command bit true in retentive memory.
For each case, record whether the action ran 0, 1, or 2+ times — as a number. The right answer varies: a batch addition must be 0, a safety-related stop can tolerate 2. Knowing the number is the point. Case 7 catches almost every site the first time it is run.
The failures you actually see
| Symptom | Cause | Prevention |
|---|---|---|
| Command fires right after reconnect | Driver store-and-forward flushed its queue | Disable write cache on command tags, check the sequence |
| Same command runs twice | A held true bit reads as a rising edge after restart | Write Cmd_Last before the action |
| Command disappears | HMI pulse shorter than scan + publishing interval | PLC clears it after consuming |
| Every machine moves when the broker restarts | RETAIN on command topics | Never RETAIN; clear existing ones with an empty payload |
| Button enables too early | "Online" mistaken for "ready" | Gate on staleness and I/O status |
| Operator clicks repeatedly | Pending state and rejection reason not on screen | Render Cmd_Ack as a sentence |
What to check at commissioning
Six lines per important command is enough.
- The PLC, not the HMI, is the defined owner of clearing the request.
- Each of PLC / HMI / server / gateway has been restarted, and no stale request executed.
- The PLC rejects when startup data is invalid, and the HMI shows why.
Cmd_Seqis DINT or wider, andCmd_Lastcomes back to the HMI.- One audit row carries the sequence and all three timestamps.
- No retained messages exist on any command topic.
None of this says make every button complicated. Do it for pump start, valve open, fault reset and batch addition — the ones where a wrong execution physically moves something. A single bit is fine for the rest.
Next time equipment moves by itself after a restart, check retained messages and the driver write cache before you open the logic. That is where it usually ends. If the problem is feedback that never comes back, command feedback timeouts is the other article.