Troubleshooting
The payload format the s7comm write node accepts from MQTT
Short answer
Injected values write to the PLC while the MQTT-derived message fails because the s7comm:write node receives the wrong runtime type. Rebuild the payload as an array containing one object per destination, serialize it once with JSON.stringify at the final boundary, and verify the type with a Debug node before enabling live writes.
On this page
- Why do injected test values write to the PLC but the MQTT message does not?
- Which path is broken, the MQTT link or the PLC write?
- What runtime type leaves each node in your flow?
- What structure does the s7comm write node accept?
- Do the field values fit the PLC destinations they map to?
- How do you prove the fix before writing live values?
- Questions asked
Your injected string and REAL values land in the PLC, but the same values arriving inside the MQTT message are rejected by the s7comm:write node. That combination points at one cause: the runtime type of the payload at the write node's input, not the values themselves. The flow that resolved this fault rebuilt the payload as an array containing one object per PLC destination and serialized it once with JSON.stringify, spelled with a capital J, immediately before the write node. Work the checkpoints in order: prove the PLC path with the working injection, inspect the payload type at every transformation, rebuild the structure to the node's documented contract, then verify each value against its declared PLC type.
Why do injected test values write to the PLC but the MQTT message does not? #
The failing writes fail on runtime type, not on values: your injections arrive at the write node in the structure it already accepts, while the message from the broker arrives in a different type, and correcting values cannot fix a type mismatch.
The s7comm:write node consumes a Node-RED message payload and encodes it into S7 write requests; it never reads the display form shown in the debug sidebar. A successful injected write is therefore evidence about three things at once: the PLC connection, the destination addressing, and the node's input contract. The MQTT-derived message violates exactly that contract. A JSON string, a parsed object and an array of objects render almost identically in the sidebar while remaining three different inputs, which is why no amount of value editing in the transformation changed the outcome.
Which path is broken, the MQTT link or the PLC write? #
Check the two connections separately: a received MQTT message proves the broker path, and your previously working injected write proves the PLC path, so rerun that injection through the same write node before touching any transformation.
| Checkpoint | Observation | Decision |
|---|---|---|
| MQTT delivery | Message arrives at the MQTT in node | No arrival is a connection fault: fix the broker link before the payload |
| PLC path | Rerun the working injected write on the same node and connection | Success moves the fault to the transformation; failure means connection or addressing |
| Connection settings | Broker address and port, PLC address and slot | Compare against the working configuration instead of retyping assumed defaults |
| Destination selection | The tag or data block address each write targets | Confirm the MQTT flow writes the same destinations the injection used |
| Payload transformation | Type and contents immediately before the write node | Working injection plus failing MQTT write: inspect the node's input contract next |
A message that never arrives is a broker-side fault of its own; the case where a broker refuses a plain MQTT connection outright is covered in the article on Node-RED MQTT failing when the broker rejects a plain connection (article:node-red-mqtt-fails-when-the-broker-rejects-plain-mqtt). With both paths proven, the message itself is the only remaining variable.
What runtime type leaves each node in your flow? #
Inspect the payload's type, not only its contents, at four points: at the MQTT input, after conversion, after field extraction, and immediately before the write node, because a JSON string, an object and an array of objects display almost identically in the debug sidebar.
Node-RED's own user guide points at the Debug node as the way to determine whether a property contains a String or an Object, so set one debug point per stage, show the complete msg, and read the type next to the value.
| Representation at a debug point | What it is | What it means for the write |
|---|---|---|
| JSON text | A serialized string that contains structure | Nothing can be extracted until it is parsed once |
| JavaScript object | Named fields a function can access directly | Extract the fields without another conversion |
| Array of objects | One element per write destination | The structure the resolving flow built |
| Double-serialized text | A string that parses to another string | Serialization ran twice; the node receives quoted text |
If the first debug point shows text, parse once before selecting fields; if it already shows an object, extract without parsing again. The parse-then-extract order is the same rule that lets a flow write tags on an edge gateway from an MQTT JSON payload (article:write-tags-on-an-edge-gateway-from-an-mqtt-json-payload).
What structure does the s7comm write node accept? #
Build the payload the installed node's help panel documents: in the flow that resolved this fault, the working structure was a JavaScript array containing one object per PLC destination, serialized once to JSON text with JSON.stringify at the final boundary before the write node.
Two layers are in play and they are easy to conflate. The logical structure is the array of destination objects; the representation is JSON text. The posted resolution named both: the message reaches the PLC as an object within an array, and the line that made the difference converted the payload to a JSON string properly. The working spelling is JSON.stringify, the stringify method of the built-in JSON global; lowercase json is not a defined object, and a lowercase call stops the function node before the write.
// Illustrative transformation. JSON.parse, JSON.stringify, typeof and
// Array.isArray are JavaScript built-ins. The entry field names shown
// here (destination, value) are examples only: take the real per-entry
// names from the help panel of your installed s7comm node.
let data = msg.payload; // JSON text or object, as received
if (typeof data === "string") {
data = JSON.parse(data); // JSON text -> JavaScript value
}
let entries = [
{ destination: "data", value: String(data.data) },
{ destination: "datetime", value: String(data.datetime) },
{ destination: "devaddr", value: String(data.devaddr) },
{ destination: "fcnt", value: Number(data.fcnt) },
{ destination: "port", value: Number(data.port) },
{ destination: "rssi", value: Number(data.rssi) }
];
msg.payload = JSON.stringify(entries); // serialize once, at the final boundary
return msg;
Serialize exactly once. Passing an already-serialized string through JSON.stringify a second time wraps it in quotes and escapes the inner ones, leaving the write node to parse text instead of reading a structure. The resolved flow serialized once at the final boundary; if the help panel of a different S7 node package documents a live array input instead, that package's contract wins, and the decisive check in every case is the runtime type your working injection presented at the same node.
Do the field values fit the PLC destinations they map to? #
Check each string against its declared length and each number against REAL precision before the write: a 19-character timestamp fits a 20-character destination, an 8-character device address exactly fills an 8-character destination, and a frame counter of 1,042 stays exact because it is below the 16,777,216 single-precision limit.
As posted, the flow declares six elements for the PLC: data as STRING[22], datetime as STRING[20], devaddr as STRING[8], and fcnt, port and rssi as REAL.
| Element | Declared PLC type | Check before writing |
|---|---|---|
| data | STRING[22] | Length within the maximum; hex payload kept as text |
| datetime | STRING[20] | Length includes separators and formatting |
| devaddr | STRING[8] | Leading zeros and letter case preserved |
| fcnt | REAL | Finite integer within single-precision exactness |
| port | REAL | Numeric value, not quoted digits |
| rssi | REAL | Finite value with its sign preserved |
Worked example, one captured message:
{"data":"A412FF01987C","datetime":"2024-05-06 08:31:42","devaddr":"1A2B3C4D","fcnt":1042,"port":2,"rssi":-87}
- data "A412FF01987C": 12 characters against a 22-character maximum — fits.
- datetime "2024-05-06 08:31:42": 10 date characters + 1 space + 8 time characters = 19, against a 20-character maximum — fits; appending ".123" for milliseconds gives 23 and overflows, so either the format drops the milliseconds or the destination grows.
- devaddr "1A2B3C4D": 8 characters against an 8-character maximum — fits with zero margin, so a longer address must be rejected, never truncated.
- fcnt 1042: S7 REAL is IEEE 754 single precision with a 24-bit significand, so integers up to 2^24 = 16,777,216 are stored exactly and 1,042 survives the round trip unchanged.
- port 2 and rssi -87 arrive unquoted, so they are already numbers; a quoted "2" in the source JSON would need explicit conversion before the write.
Guard the conversions. JavaScript's Number() turns an empty string and null into 0, and a missing field into NaN, so validate presence and reject invalid input rather than writing a plausible-looking substitute; silently truncating an overlength timestamp corrupts it character by character.
How do you prove the fix before writing live values? #
Replay one captured MQTT message through the rebuilt transformation with the write node disabled, compare the final payload with your working injection element by element, then enable a single test destination first so the first live write touches a tag you chose for that purpose.
Treat every test write as a write to a running controller: use a scratch area the PLC program never reads, and keep anything the control or safety logic depends on out of the test path.
- Capture one real MQTT message and disable the write node, so the replay transforms without writing.
- Run the message through parse, extraction and assembly, with a Debug point reporting the type at each stage.
- Before serialization, confirm Array.isArray(entries) is true; after serialization, confirm the payload starts with a bracket, not a quotation mark.
- Compare the result against the working injection: array structure, per-entry field names and value types must match one for one.
- Enable one test destination and write once, then read the value back from the PLC and compare it character for character and digit for digit.
- Enable the remaining destinations one at a time, and leave the type-checking Debug point in place through the first production day.
Decision: if the injected write still succeeds and the Debug panel shows the same array-of-objects type at the write node's input that the injection produced, enable the mapping — that is the resolved configuration. If the types match element for element and the write still fails, the fault sits downstream of the payload: check the destination addressing against the working injection first, then the installed node version's documented input limits.
Questions asked
How many bytes does a STRING[22] destination occupy in the PLC?
An S7 string stores two bookkeeping bytes ahead of its characters: one for the declared maximum length and one for the current length. STRING[22] therefore occupies 24 bytes in the data block, STRING[20] needs 22 bytes, and STRING[8] needs 10 bytes. Size the area with those two extra bytes included, or the write lands misaligned and later reads return shifted characters.
What happens if the function node spells the serializer json.stringify?
The function node throws an error before any write leaves the flow. JavaScript exposes the serializer on the JSON global object, so a lowercase json name is undefined and the message fails at that line. Node-RED marks the message as errored and nothing reaches the PLC; a catch node, if the flow has one, is where that error shows up.
Can the built-in JSON node replace the parse step in a function node?
Yes, for the text-to-object step. Node-RED documents the JSON node as the way to convert between serialized JSON text and a JavaScript object, so it can replace the parse call at the head of the flow. It cannot assemble the array of destination objects, so a function node still builds that structure in the shape the write node's help panel documents.
Should the entire MQTT message object go to the write node?
No. The received message carries broker and transport properties alongside your data, and the write node consumes a payload built only from the destination and value entries it documents. Extract the process fields, wrap them in the structure the help panel shows, and keep broker metadata out of the write path entirely.