One OPC UA tag per structure, derived tags for its fields
A controller often holds a device as one structure: a motor with a command, a status, a speed and limits. This page is for the engineer who has to bring many such devices in. The practice is to bind one OPC UA tag to the structure and give every field a derived tag that reads its field out of that value, instead of binding one OPC UA tag per variable.
The pattern
In a tag type (UDT) it is two kinds of member:
| Member | Kind | Holds |
|---|---|---|
doc | opcua | The whole structure: the node named by {ID} on the server {OPCUA}. The type's Root member |
rSpeed, Control.bAuto, ... | derived | One field of doc: sourceTag is ./{RootMember} and property is the field's path in the structure |
Everything in the type below the root is a view of the root's value. A type made this way for a motor:
{
"path": "Devices.Motor",
"name": "Motor",
"rootMemberKey": "doc",
"parameters": [
{ "name": "OPCUA", "dataType": "OPC-UA Server", "defaultValue": "" },
{ "name": "ID", "dataType": "String", "defaultValue": "{InstanceName}" }
],
"members": [
{ "memberPath": "doc", "kind": "opcua", "producerModuleId": "io.scadex.driver.opcua",
"producerInstanceRef": "{OPCUA}", "binding": { "nodeId": "ns=3;s=\"{ID}\"" } },
{ "memberPath": "rSpeed", "kind": "derived", "sourceTag": "./{RootMember}", "property": "rSpeed",
"engUnit": "rpm", "formatString": "0" },
{ "memberPath": "Control", "kind": "folder" },
{ "memberPath": "Control.bAuto", "kind": "derived", "sourceTag": "../{RootMember}", "property": "Control.bAuto" }
]
}
ID defaults to the instance's name, so an instance named after its data block needs only the server:
{ "path": "Plant.Motors.M101", "typeId": "<type id>", "parameters": { "OPCUA": "Line1_PLC" } }
A device whose node does not follow its name sets ID too. A type may also take the whole node as its parameter ("nodeId": "{ID}", and ID set to ns=3;s=\"M101\"), which is how the AllWaves types are made; the data block name is easier to read.
Three details carry the pattern:
- Write
{RootMember}, notdoc. It is a built-in that names the type's root member, so a type that renames its root needs no edit in every field. - The path to the root follows the depth. A member at the top reads
./{RootMember}; a member one folder down reads../{RootMember}, two folders down../../{RootMember}. The folder members are listed in the type so the tree has somewhere to put the field. - A sub-device is a nested type. A measure with its own fault detections becomes a member of kind
udtwithmemberParamsthat hand the nested type its server and node:{"OPCUA": "{OPCUA}", "ID": "..."}, the node written once in the outer type for every device of it. The nested type has its own root and its own derived members. When the outerIDis the node in full, the nested one is that node plus the sub-device's field ({ID}."FaultDetection").
The dashboard makes this type for you. In the connection's browse tree, the wand on a structured variable (Create a UDT type from this structure's fields) reads the node's current value, learns its fields and builds the type with doc as the root member. See OPC UA.
Why it is better
- One monitored item per device. The driver subscribes to each bound node separately (a sampling interval of 1,000 ms and a queue of 16 each). A device of forty fields costs one item with the pattern and forty without. The AllWaves tags were moved this way: 20 types stand for all of its devices, and only the 119 tags that no device owns stayed single OPC UA tags.
- The fields agree. Every derived tag takes its value, quality and timestamp from the one structure value, so a screen that shows speed and mode together never shows two different reads.
- One place to change. A field added to the type is a derived member, not a node id to type in every instance. The node of a device is one parameter.
- One quality. When the session drops, the root goes bad and so does every field below it, with the same reason.
Derived or expression
| Use a derived member for | Use an expression member for |
|---|---|
A field of a structure (property) | A value computed from several tags |
One bit of a status word (sourceBit, 0 to 63) | A scale, a label, a unit conversion |
| A copy of another tag under a second name | Anything that needs if, arithmetic or text |
A derived tag costs nothing to compute and carries the source's quality as it is. An expression tag runs a script on every input change and takes the worst quality of its inputs, so keep them for what a derived tag cannot say. See Tags and Expression tags.
Writing
A derived tag that follows a property refuses writes: the gateway reads a part and cannot rebuild the whole structure. A derived tag that follows a bit can be written (it reads the word, changes the bit and writes the word back).
For a field that operators must change, such as a setpoint, bind it as its own OPC UA member next to doc, on the field's own node. It is built from the same {ID}:
{ "memberPath": "rSetpoint", "kind": "opcua", "producerModuleId": "io.scadex.driver.opcua",
"producerInstanceRef": "{OPCUA}", "binding": { "nodeId": "ns=3;s=\"{ID}\".rSetpoint" } }
That costs one more monitored item for that field. See Model a motor as a tag type for node ids built from a parameter.
When not to use it
- A handful of variables. A device of three scalars is simpler as three OPC UA members.
- Variables that are not in one structure. The pattern needs one node that holds them all.
- A server that cannot describe its structure. The driver then keeps the value as bytes, and there are no fields to read. See OPC UA.
What has been proven
The pattern is in the built-in [system]dtl type (the S7 date and time structure, doc plus eight derived members) and in the 20 types the AllWaves import produces. The importer's tests check that each structure becomes one type with doc as its root and one derived member per field, that folders and nested devices follow, and that a unit or format that differs between devices becomes a parameter. Decoding of structured values has not been checked against a real server, and AllWaves has not run against its PLCs. Try one device on a real controller before you build forty.