IMDHMIControls LibraryShop main store

Troubleshooting

Why an OPC UA Int16 write fails when the payload is a string

SCADA and data logging Published 5 min read

Short answer

The write fails in Node-RED, not on the OPC UA server: the Function node sets msg to the string that parseFloat(temp).toFixed(0) returns, so the OpcUa-Item never gets a message object with a numeric payload. Keep msg, assign Math.round(Number(msg.payload)) to msg.payload, and return msg; with rounding already enabled on the Range node, the Function node can go entirely.

On this page
  1. The write dies at the Function node
  2. The broken line, one number at a time
  3. Keep msg, round the payload, return msg
  4. The Range node already rounds, so delete the Function node
  5. Checklist before you write to a live tag
  6. If a numeric payload still fails
  7. Questions asked

The write never reaches the OPC UA server. In this flow the Function node runs msg = parseFloat(temp).toFixed(0) and returns the result, so it hands Node-RED a string where every downstream node needs the message object. Node-RED documents the rule for the Function node without exceptions: it must always return a msg object, and returning a number or string results in an error. Keep the message object, write the rounded number into msg.payload with Math.round(Number(msg.payload)), and return msg. Because the Range node in the same flow already has rounding enabled, its output is already the integer you want — you can delete the Function node and wire the Range node straight into the OpcUa-Item. Below: where the flow breaks, the arithmetic that proves it, the corrected code, and a checklist for the whole chain.

The write dies at the Function node #

The chain runs like this: an analog input node of type mraa-gpio-ain on pin 0, sampling every 1000 ms into msg.payload; a Range node scaling 0–1023 counts to 0–100 with rounding enabled; a Function node; an OpcUa-Item and OpcUa-Client from the node-red-contrib-opcua package, with the item node declaring item n=2;s=Tag18, datatype Int16, value type number, and the client node set to action write on the endpoint defined in the OpcUa-Endpoint configuration node.

Everything upstream of the Function node is healthy — the Debug panel after the Range node shows a clean integer. The fault sits on the middle line of the Function node:

var temp = Number(msg.payload);
msg = parseFloat(temp).toFixed(0);
return msg;

toFixed(0) returns a string, and that string is assigned to msg itself, so the message object is destroyed. Node-RED reports a message-type error at the Function node and forwards nothing: the item and client nodes never run. No session fault, no certificate problem, no addressing error — the write fails inside Node-RED, before any OPC UA traffic exists.

The broken line, one number at a time #

Follow one reading through. The analog node delivers 423 raw counts, and the Range node converts to the scaled value with its rounding option:

423 × 100 ÷ 1023 ≈ 41.35, rounded to 41 — a JavaScript number in msg.payload.

The Function node then takes that 41 through five steps:

  1. Number(41) → 41, still a number
  2. parseFloat(41) → 41, still a number
  3. (41).toFixed(0) → "41", a two-character string
  4. msg = "41" overwrites the message object with that string
  5. return msg hands Node-RED a string, which the runtime rejects

Two details carry the diagnosis. toFixed(0) formats a number as fixed-point text and never returns a number, so the payload type was already wrong before the assignment destroyed the object. And the arithmetic was already finished: the Range node's rounding produced the final integer, so the Function node contributed nothing but those two faults. Switch rounding off and the payload arrives as 41.35 — toFixed(0) still returns the string "41". The type, not the rounding, is what breaks the write.

Keep msg, round the payload, return msg #

The correction changes the assignment and nothing else:

var temp = Number(msg.payload);
msg.payload = Math.round(temp);
return msg;

Math.round(41.35) is 41 and the type stays number, so the item node receives a genuine numeric payload for its Int16 conversion. The two-line alternative parseFloat(temp.toFixed(0)) computes the same integer but detours through a string; Math.round never leaves the number type, so prefer it. Two rules now cover every Function node you place in front of an OPC UA write: assign results to a property such as msg.payload, never to msg itself, and return msg.

The Range node already rounds, so delete the Function node #

With rounding enabled, the Range node outputs the final integer directly, so the corrected Function node adds nothing on this flow. Connect the Range node output straight to the OpcUa-Item and remove the Function node — that also removes the only node in the chain able to overwrite the message object. Keep a corrected Function node only when it does real work, for example clamping readings outside the 0–100 span before the write.

Checklist before you write to a live tag #

Walk the chain once from left to right:

  • Analog input: the mraa-gpio-ain node on pin 0, interval 1000 ms, delivering raw counts in msg.payload.
  • Range node: 0–1023 in, 0–100 out, action scale, rounding enabled.
  • Debug node after the Range node: the payload shows as a plain number with no surrounding quotes; quotes mean a string is still in the path.
  • Function node, if kept: assigns only to msg.payload, and return msg is the last line.
  • OpcUa-Item: item n=2;s=Tag18, datatype Int16, value type number.
  • OpcUa-Client: action write, endpoint taken from the OpcUa-Endpoint node.
  • Debug node after the OpcUa-Client: the write result appears and no message-type error shows.

Keep the flow pointed at the OPC UA simulator server it already targets until the corrected value shows in that last Debug panel — a payload of the right type with the wrong value is still a wrong reading once this feeds a live system.

If a numeric payload still fails #

When msg.payload is a number and the failure persists, the cause has moved onto the OPC UA side. A write whose payload type disagrees with the type the server declares for the node comes back as the type-mismatch status Bad_TypeMismatch defined in the OPC UA specification, so confirm the datatype on the OpcUa-Item matches what the server actually exposes for n=2;s=Tag18 and that its value dropdown is set to number. If you fire manual test writes from an Inject node, give that node a numeric payload too: the datatype an Inject node carries propagates into the item node and can silently become the type the client sends. Faults that begin only once the value leaves the client are a different family — why a SCADA write to a PLC real address fails silently and why OPC UA writes fail while reads stay good pick up from the server side.

Questions asked

What values fit an OPC UA Int16 item?

Int16 is a 16-bit signed integer, so it holds whole numbers from -32,768 to 32,767. A value scaled to 0–100 and raw counts up to 1023 both sit well inside that range. If the value you need to write later becomes a decimal fraction or leaves that span, change the datatype on the OpcUa-Item to match, because the item type and the payload type have to agree.

How do I see in the Debug panel whether the payload is a string?

In the Node-RED debug sidebar a numeric payload displays as plain digits, for example 22, while a string payload displays wrapped in quotes, for example "22". Set the debug node to show msg.payload, trigger the flow once and look: quoted digits mean a string conversion such as toFixed(0) is still in the path, and the typed write fails until the payload is a real number.

Why does a manual test write still carry the wrong type?

Look at what fires the flow. When an Inject node triggers a manual write, the datatype configured on that Inject node feeds into the OpcUa-Item and can become the type the client sends, overriding the number you expect. Give the Inject node a numeric payload as well; the OPC UA side then validates datatype and value together before anything goes to the server.

Keep reading

  • Troubleshooting

    Why OPC UA writes fail while reads stay good

    Good reads prove only the read path. A write rejected as 0x80004005 is a generic failure, not a permission verdict: read the node's AccessLevel and UserAccessLevel under the session's real identity, check the write bit, then split bridge from source with a direct OPC UA write.

    SCADA and data logging

  • Troubleshooting

    Why OPC UA writes fail to an Omron NJ PLC

    A working OPC UA read proves only the read path. Test the write from a diagnostic client. If the direct write fails, route writes through FINS: declare a global variable as ARRAY[0..39] OF WORD with AT %E0_9000, enter the address as E0_9000 with no percent sign, and write whole 16-bit words, because a FINS write replaces every bit in the word.

    SCADA and data logging

  • Explainer

    When an S7-1500 needs no SIMATIC PC Station for OPC UA

    Yes. The S7-1500's embedded OPC UA server serves a custom client on the PC end to end: enable the server in the CPU's hardware configuration, publish variables through user-defined server interfaces, trust the client certificate, and keep the published node count inside the CPU class limit of 1,000, 5,000 or 30,000 nodes. No SIMATIC PC Station, no SIMATIC NET and no PLC-side OPC UA program are needed.

    SCADA and data logging