write a single value
You can write a single value to an OPC UA Variable using the OPC UA Write Node
.
The Write Node allows you to send values to variables on the OPC UA server, enabling you to control devices, update setpoints, and modify server-side data.
The Write Node
is designed to handle various data types automatically and provides flexible ways to specify both the target variable and the value to write.
Step 1: add the Write Node to your flow
- Drag and drop the
OPC UA Write
node from the palette to your workspace - Connect an Inject node to provide input data to the Write node
- Add a Debug node after the Write node to see the results
Your flow should look like this:
[Inject] → [OPC UA Write] → [Debug]
Step 2: configure the Write Node
-
double-click the Write node to open its configuration dialog
-
set the node name (optional):
Name: Temperature Setpoint Writer
-
configure the endpoint:
- Select an existing endpoint from the dropdown, or
- Click the pencil icon to create/edit an endpoint
- Example endpoint:
opc.tcp://localhost:4840
-
set the AttributeId (usually leave as default):
AttributeId: Value
-
configure the NodeId (optional - can be set via message):
enter the nodeId of the UA variable you want to write to, for example:
NodeId: ns=2;s=TemperatureSetpoint
You can use alternative formats to specify the UA Variable like browse path. or use the browse button to select the variable from the server.
noteYou can also leave this field empty and set the NodeId via the input message, which is useful if you want to write to different variables dynamically.
-
click done to save the configuration
Step 3: configure the inject node
The inject node is used to provide the value to write to the OPC UA variable.
-
double-click the Inject node to configure it
-
set the payload with the value you want to write:
msg.payload: 25.5
No dataType required:Note that we are not specifying the DataType here, the Write node will automatically determine the type based on the targeted variable in the OPC UA server and will perform the appropriate conversion.
-
set the NodeId (if not configured in Write node):
msg.nodeId: "ns=2;s=TemperatureSetpoint"
-
Configure data type (optional, mostly not needed):
On rare occasions, you may want to specify the data type explicitly, but usually, the Write node handles this automatically.
msg.dataType: "Double"
Note: valid values for msg.dataType
include:
Byte
SByte
UInt16
Int16
UInt16
Int32
UInt32
Double
Float
Int64
UInt64
String
Boolean
DateTime
ByteString
Guid
ExtensionObject
LocalizedText
Step 4: deploy and test
- click deploy to deploy your flow
- click the Inject node button to trigger the write operation
- check the debug panel for the write result
Expected output:
{
"payload": 25.5,
"nodeId": "ns=2;s=TemperatureSetpoint",
"statusCode": "Good",
"dataType": "Double",
"attributeId": "Value"
}
Configuration options
Node Name
A friendly name for the node that appears in the flow editor. This helps identify the purpose of the node when you have multiple Write nodes.
Endpoint
The OPC UA server connection. This defines which server the node will connect to for write operations.
Important: All nodes sharing the same endpoint use the same connection, making it efficient for multiple operations.
AttributeId
Specifies which attribute of the target node to write to:
- Value (default): The node's current value
NodeId
The identifier of the variable to write to. This can be:
- Node Configuration: Set directly in the node configuration
- Message Topic: Provided via
msg.topic
- Message NodeId: Provided via
msg.nodeId
Priority: msg.nodeId
> msg.topic
> Node Configuration
NodeId formats
see NodeId definition for more details on NodeId formats.
Input message formats
Basic write message
msg = {
payload: 42.5, // Value to write
topic: "ns=2;s=Temperature" // Target variable
}
Message with data type
msg = {
payload: 100,
nodeId: "ns=2;s=Pressure",
dataType: "Int32" // Explicit data type
}
Message with a ExtensionObject as a Value
We can write a complex data type like an ExtensionObject by specifying the TypeId and the Body of the ExtensionObject in the payload. The syntax of a extensionObject follows the OPCUA JSON format for ExtensionObject (v1.05)
msg = {
payload: {
"TypeId": "ns=2;i=1001", // ExtensionObject TypeId
Body: {
"Field1": "Value1",
"Field2": 123
}
}
}
Message with a Variant
msg = {
payload: {
dataType: "Double", // optional, can be incepted by the Write node
arrayType: "Array", // optional, can be incepted by the Write node
value: [23.45, 34.56, 45.67] // Array of doubles
},
nodeId: "ns=2;s=MyDoubleArray"
}
Message with a DataValue
msg = {
payload: {
statusCode: "Good",
sourceTimestamp: "2024-01-27T09:55:24.897Z",
sourcePicoseconds: 0,
serverTimestamp: "2024-01-27T09:55:24.897Z",
serverPicoseconds: 0,
value: {
dataType: "String", // optional, can be incepted by the Write node
value:"Hello OPC UA" // String value to write
}
},
nodeId: "ns=2;s=MyDoubleArray"
}
Data Type Handling
Automatic Type Conversion
The Write node automatically converts JavaScript values to appropriate OPC UA types:
Numbers:
- Integers → Int32, UInt32, etc.
- Decimals → Float, Double
Strings:
- Text → OPC UA String
Booleans:
- true/false → OPC UA Boolean
Arrays:
- [1, 2, 3] → OPC UA Array
Matrix
- [ 1, 2, 3, 4, 5, 6 ] → you need to specify the dimensions = [2,3] in the payload
Explicit Type Specification
For precise control, specify the data type explicitly:
msg = {
payload: "123",
dataType: "Int32", // Convert string to integer
nodeId: "ns=2;s=Counter"
}
Complex Data Types
For complex structures, use the variant format:
msg = {
payload: {
dataType: "Double",
value: [
1.1, 2.2, 3.3,
2.1, 2.2, 2.3
], // Array of doubles
arrayType: "Array",
dimensions: [2,3] // 2 rows, 3 columns
},
nodeId: "ns=2;s=DoubleArray"
}
Output lessage
The Write node outputs a message containing the write operation results:
{
// Original input
payload: 25.5,
nodeId: "ns=2;s=TemperatureSetpoint",
// Write results
statusCode: "Good", // Operation status
dataType: "Double", // Actual data type written
attributeId: "Value", // Attribute that was written
message: undefined // Error message (if any)
}
Status Codes
- Good: Write completed successfully
- BadNodeIdUnknown: Variable not found
- BadTypeMismatch: Data type conversion failed
- BadUserAccessDenied: Insufficient permissions
- BadNotWritable: Variable is read-only
Error Handling
Common Errors
-
Connection Issues:
Status: BadConnectionClosed
Message: "Connection to server lost" -
Invalid NodeId:
Status: BadNodeIdUnknown
Message: "The node id refers to a node that does not exist" -
Type Mismatch:
Status: BadTypeMismatch
Message: "Cannot convert value to target type" -
Permission Denied:
Status: BadUserAccessDenied
Message: "User does not have permission to write"
Error Detection
Check the statusCode
in the output message:
if (msg.statusCode !== "Good") {
node.error(`Write failed: ${msg.statusCode}`);
return null;
}
Best Practices
- Always Check Status: Verify
statusCode
in the output message - Handle Errors Gracefully: Implement proper error handling
- Use Appropriate Data Types: Match the server's expected types
- Validate Input: Check payload values before writing
- Connection Management: Reuse endpoints for multiple nodes
- Security: Ensure proper authentication and authorization
Troubleshooting
Write Operation Fails
- Check Connection: Verify endpoint configuration and server availability
- Verify Permissions: Ensure user has write access to the variable
- Check NodeId: Confirm the variable exists and is writeable
- Data Type: Verify the value can be converted to the target type
- Server Logs: Check OPC UA server logs for additional error details
Node Status Indicators
- Green Dot: Write completed successfully
- Red Dot: Write operation failed
- Blue Dot: Write operation in progress
- Gray Dot: Node not connected/configured
Next Steps
After mastering single value writes, explore: