Explainer
What a Node-RED simulator for OPC UA and MQTT data teaches
Short answer
A simulator stands in for the PLC, SCADA server or broker you cannot connect to yet, so you can build and prove the whole Node-RED acquisition path — input node, debug, one JSON decode, normalization, dashboard. The rule it teaches: decode each payload exactly once — set the MQTT input to a parsed JSON object and drop the JSON node, or keep the raw text and decode with one JSON node.
On this page
A simulator stands in for the PLC, the SCADA server or the broker you cannot connect to yet — a compliance review, a segmented network or a machine that has not shipped can block live access for months. It publishes realistic machine-state and process values, so you can build and validate the entire Node-RED acquisition path before any of that exists: input node, debug check, one JSON decode, normalization, dashboard. The lesson that carries into production is decode discipline. An MQTT input node set to a parsed JSON object output already delivers an object, and a JSON node behind it is redundant — it can fail on the object input or hand the next node a string instead of the object it expects. This article follows one simulated message end to end, shows where double decoding breaks a flow, and states what a simulator can and cannot prove.
What a simulator replaces, and what it cannot prove #
Identify the sender before editing anything. An OPC UA simulator runs an OPC UA server, and an OPC UA client node reads or subscribes at that server endpoint. An MQTT simulator publishes to a broker, and an MQTT input node subscribes by topic. PackML is neither of those transports: it is a machine state model, and the same state fields can arrive through either node. Which protocol carries them is decided by the node you configure, not by the PackML label.
| Simulated source | What runs on the simulator side | Node-RED entry point | First proof of life |
|---|---|---|---|
| OPC UA process values | An OPC UA server | OPC UA client node at the endpoint | Connection status plus a changing value |
| MQTT messages | An MQTT publisher | MQTT input node on the topic | Subscription connected, topic and payload arriving |
| PackML machine state | Either transport carrying the state model fields | Whichever protocol node you chose | State and mode changing coherently |
Place a debug node directly after the protocol input before you build anything else. No message at that debug node clears the dashboard, the mapping and the decode of suspicion — the fault sits between the simulator and the runtime, and the work described in fixing MQTT sensor data missing in Node-RED begins at the broker, the topic and the subscription. A raw message at the debug node means the protocol path works, and everything after it is logic.
A passing simulation proves the flow. It cannot prove the plant: the real sender topic tree, its OPC UA namespace, its security policy and its update timing stay unknown until the first live connection. Treat simulator success as evidence about your logic, never as a commissioning result.
The path to build in observable stages #
- Start the simulator, and read its protocol, host, port and topic or item identifier from its own configuration rather than from assumed defaults.
- Add the matching input node: broker and exact subscription topic for MQTT, endpoint and item for OPC UA.
- Wire a debug node immediately after the input, deploy, and confirm connection status, message arrival and payload type.
- Decode once, at one point — the next section fixes where.
- Normalize: map protocol-specific fields into stable application fields, and keep the original topic or source identifier on the message for troubleshooting.
- Connect the consumer last. Only when the normalized output matches the expected structure should the flow feed storage or a dashboard — for example when you route MQTT sensor data into Grafana on an edge PC.
Do not begin with the dashboard widget. Presentation hides whether the fault is connectivity, decoding or mapping, while one stage at a time gives every stage a pass condition you can observe before the next one exists.
Decode the payload exactly once #
The MQTT input node can do the parsing itself: with its output set to a parsed JSON object, it detects JSON in each payload and msg.payload arrives as an object. In that configuration a following JSON node is redundant, and depending on the action it is set to, it can fail on the object input or serialize the object back into a string. Either failure breaks field access downstream or mutates the message between stages. When the input emits raw text or a buffer instead, exactly one JSON node after it is the explicit conversion boundary; the Node-RED cookbook documents that node as the parser for JSON that arrives as a string.
Check the payload type, not its characters. The debug sidebar labels each entry with the property type — object, string, array — and a string that looks like JSON is still serialized text that no node can traverse by field. One deliberate parsing point means malformed messages fail at a known place and every node after it receives the same type.
| MQTT input output setting | msg.payload arrives as | A JSON node after it |
|---|---|---|
| A parsed JSON object | object | Remove it: redundant, and it can error or turn the object back into text |
| A string of JSON | string | Keep exactly one, before any field access |
| A buffer | buffer | Keep exactly one, after confirming the text encoding |
| A payload that is not JSON | per the input setting | Do not reach for it as a generic decoder; match the parser to the format |
The rule survives nesting. A parsed top level does not guarantee that every consumer reads nested fields correctly, which is why SCADA MQTT points come back blank with nested JSON payloads even after a correct first decode.
When the decoded payload feeds a dashboard rather than a debug panel, the same single-boundary discipline applies on the consumer side: one validation pass where the payload enters the presentation layer, as in validating Ignition Perspective JSON before its widgets bind to the fields.
Worked example with illustrative numbers #
Suppose a simulated filler publishes one message per second on the topic packaging/line1/filler/state, with the payload {"state":"EXECUTE","mode":"AUTO","count":412}. The string is 45 characters, all ASCII, so it occupies 45 bytes on the wire. A ten-minute validation run at that rate carries 1 × 600 = 600 messages and 600 × 45 = 27,000 bytes, or 27 KB.
With the MQTT input set to a parsed JSON object, msg.payload is an object with three properties, and the debug sidebar labels it as an object — there is nothing left for a JSON node to do. With the same input left on raw output, msg.payload is the 45-byte string; one JSON node later, the object is identical to the first case. The decode point moves and the object does not. These are example numbers: payload size and publish rate come from whatever your simulator emits, and both scale the totals linearly without touching the rule.
Map PackML states and modes deliberately #
A PackML simulator beats a random-number generator because its fields are bound to a documented model. ISA-TR88.00.02 standardizes machine states, modes and PackTags for discrete machinery, and the OPC UA for PackML companion specification maps that same state model into an OPC UA address space, so a state value in the simulated payload corresponds to a defined state rather than to whatever the flow happens to expect.
Verify that state, mode and the counters belong to one machine instance — one topic prefix or one OPC UA object — and map the state by name against the model defined states. In the example payload above, a state of EXECUTE with mode AUTO says the machine is producing. If a test run shows the state field changing value on every scan, that is the simulator variation, and logic that treats any changing value as a transition will misbehave against a real machine too. Gate downstream logic on the named states, never on the fact that some value moved.
Moving from simulation to the plant connection #
Move in this order: simulator, then a real broker in a test segment, then the live PLC or SCADA path. The flow rarely changes; the endpoint, the security policy and the topic or namespace tree do. Read the real sender host, port and topic from its deployment configuration and copy those exact values into Node-RED: a service name that resolves inside a container network may not resolve from the host, and a published port can differ from the service internal port. Where the live system enables authentication or encryption, match it. A socket that connects while the session or subscription fails puts the fault above the transport layer, and the node status plus the runtime log name the stage that failed.
Keep the first live connection read-only: a flow that has only ever seen simulated data must not write to a control path, so keep the publish path and the control path separate until every mapping has been proven against real data. That separation is the subject of keeping IO data publish and control separate in Node-RED.
When the validated flow leaves the bench it belongs on the hardware that will run it in the plant — a fanless industrial mini PC as the dedicated edge host — rather than the engineering laptop that ran the simulation. The production topology is the one the simulation rehearsed: bridging OPC UA PLC data to MQTT with an edge PC is the same shape with the simulator swapped for the machine.
The decisions, in order. No sender available: run the simulator and build input, debug, decode, normalize, dashboard, decoding each payload exactly once. Sender available: keep the flow, change the endpoint, credentials and topic tree, and prove the first live pass read-only before the flow serves dashboards or storage.
Questions asked
Can the MQTT input node parse JSON by itself?
Yes. With its output set to a parsed JSON object, the MQTT input node detects JSON in every payload and delivers msg.payload as an object, so a JSON node after it has nothing left to parse. The Node-RED cookbook documents the JSON node as the parser for payloads that arrive as text or a buffer. Choose one decoding place and keep the payload type stable after it.
Do I need a real OPC UA server to test OPC UA reads?
An OPC UA client node has no data of its own: it always reads from a server process at the configured endpoint, and in a test bench that server is the simulator. Point it at the simulator endpoint address and port exactly as you would later point it at a machine server, and change only the endpoint when you cut over.
Can a JSON object be turned back into MQTT topics?
Yes. Community nodes such as node-red-contrib-json-to-mqtt flatten a JSON object into an MQTT topic tree, which helps when a JSON API must feed topic-based subscribers. It is the reverse of the decode step described above: one conversion, one direction, at one deliberate point in the flow.
Does the JSON node work in both directions?
It converts in both directions between JSON text and a JavaScript object, and with its action left on automatic it picks the direction from the input type. That is why a redundant JSON node behind an already-parsed payload can hand the next node a string instead of an object. Set the action explicitly rather than leaving it on automatic.
How much traffic does one simulated machine add?
At one message per second with the 45-byte example payload from this article, an hour-long session moves 45 × 3,600 = 162,000 bytes, or 162 KB — negligible for any broker or LAN. The real cost is debug volume: 3,600 sidebar entries in an hour, so count messages or log them to storage instead of printing every payload.