How-to
Sending Sparkplug B device commands from Node-RED
Short answer
Build each Sparkplug B DCMD in Node-RED from the alias map in the device's latest DBIRTH, one metrics entry per value, and never hard-code an alias. Keep the sequence counter on its documented progression, zero through 255 with wrap-around, unless a captured command proves the receiver accepts the minus-one convention.
On this page
To send a Sparkplug B device command from Node-RED, build the DCMD payload from the alias map the device published in its most recent DBIRTH, one metrics entry per value, and resolve every alias at send time — never store one as a constant in the flow. Run the sequence field on its documented progression unless a captured command from a working publisher proves your receiver accepts the minus-one convention. This article gives the payload structure, the alias rule, the sequence decision with a wrap-around example, the flow steps, and the checks to run before a command touches real I/O.
What a DCMD payload contains #
A device command is an MQTT publish on the device command topic — spBv1.0/{group_id}/DCMD/{edge_node_id}/{device_id}, using the same group, edge node and device identifiers the births used. The payload carries three things: a timestamp set when the flow builds the command, a metrics array with one entry per value being written, and the sequence number. Each metrics entry names the target by alias with the metric name left empty, carries its own timestamp, and declares the data type and the value. That data type must match what the device's DBIRTH declared for the metric, because the receiving decoder uses it to select the value field inside the protobuf message.
Why an alias is never a constant #
The alias exists to shrink the payload: the device sends each full metric name once, in the DBIRTH, and every later message refers to the point by number. The specification allows that number to change at every birth, and different devices reuse the same numbers for different points, so an alias means nothing without its device topic. Keep the name-to-alias table keyed by device topic and metric name, and overwrite it every time a new DBIRTH arrives on that topic. An alias hard-coded in a Function node keeps working until a reconnect renumbers the metrics, and it then fails silently: the payload stays valid while it writes the wrong point.
Every reconnect brings fresh births, and a forced rebirth refreshes all of them at once; when to use the Sparkplug rebirth tag on an edge node covers when forcing one is the right call.
Choosing the sequence strategy #
The payload specification requires a sequence number in every Sparkplug message and defines the progression: the NBIRTH carries zero, every later message increments by one, and after 255 the counter wraps back to zero. That rule works while one client owns every message on its metrics. A DCMD published from a separate Node-RED flow comes from a client that cannot see the primary publisher's counter, and the specification gives a second publisher no way to join that progression.
The applications tested in the field all publish DCMD with a sequence of minus one — a value outside the counter's range that a receiver can read as a command standing outside the progression. Decoders print it two ways: a signed decoder shows minus one, an unsigned one shows 18446744073709551615, the two's-complement reading of the same 64-bit wire pattern. That convention is observed behavior, not part of the documented rule, so it earns a place in your flow only after verification.
The decision per case:
- Your flow births the node and its devices itself — run the documented counter: zero at birth, plus one per message, wrap from the top of the range to zero.
- A second publisher, and a captured working command showed the minus-one value that your receiver accepted — publish it.
- Receiver behavior unknown — capture and decode a working client's command before anything is commissioned.
Example: wrapping the sequence counter after 255 messages #
Take one publisher that births its own edge node. The NBIRTH carries sequence 0, and 250 subsequent messages — readings and the occasional device birth — take the counter up through 250. Five more messages bring it to 255, the top of the range. The next message computes 255 + 1 = 256, and the field holds only the values 0 through 255, so the counter wraps: 256 - 256 = 0. The message after the wrap carries 0 and the one after that carries 1, exactly as the birth did. A receiver that lost sync sees the counter jump from the top of the range back to zero; that jump is the wrap the specification defines, not a sequence error, and it is why a publisher must never restart its counter at an arbitrary value mid-stream.
Building the command flow in Node-RED #
- Subscribe to births. Point an MQTT input node at the DBIRTH level under the same namespace and rebuild the name-to-alias table for each device topic on every birth message.
- Trigger. Accept the command request from whatever drives it — a dashboard button, a schedule, an upstream message — and select the target device topic.
- Resolve. Look up every requested metric name in that topic's table; if an entry is missing, the flow has not seen a birth for it, so drop the command instead of guessing a number.
- Build the payload with one metrics entry per value and the payload timestamp set at construction.
- Set the sequence field by the strategy above: the documented counter when this flow owns the births, the verified convention when it does not.
- Encode the object with the installed protobuf encoder and publish the encoded bytes on the device command topic.
- Run the round-trip checks in the next section before the flow commands live equipment.
A two-metric payload built in a Function node:
var timestamp = Date.now();
msg.payload = {
timestamp: timestamp,
metrics: [
{ name: "", alias: 4, timestamp: timestamp, dataType: "Boolean", value: true },
{ name: "", alias: 1, timestamp: timestamp, dataType: "Int32", value: 3 }
],
seq: -1
};
return msg;
The field names — timestamp, metrics, name, alias, dataType, value, seq — come from the Sparkplug B payload definition. The aliases 4 and 1 and the values true and 3 are illustrative; substitute what the latest DBIRTH actually published for your device. The minus-one sequence belongs in this field only if the capture described above confirmed the receiver accepts it; otherwise it carries the shared counter.
If the Sparkplug client node itself stalls on Connecting while the broker socket is already up, fix the handshake first: Sparkplug node stuck on Connecting while MQTT connects walks that diagnosis.
When the protobuf encoder drops fields #
A second payload shape circulates in the field: a numeric data-type code instead of the type name, a type-specific value field, and a nested properties object — one reported flow used datatype: 11 with booleanValue and an engUnit property. The reported symptom is that properties went into the encoder but never came out of the decoded result. That gap is a schema mismatch between the object the flow builds and the message definition the installed encoder implements; the available evidence does not identify a supported field layout that restores properties on decode.
Two rules follow. Do not mix the two shapes in one flow: read the input contract of the encoder you have installed and build exactly that object. And do not commission a command whose behavior depends on a properties field until a round-trip test shows the field survives encoding and decoding. The same family of schema mismatches bites birth messages as well; resolving Sparkplug NBIRTH protobuf parsing errors lists the checks that isolate them.
Verify the command before commissioning #
- Decode the outbound payload and confirm every intended metric sits in the single metrics array — nothing extra, nothing missing.
- Compare each alias against the latest DBIRTH on that device topic, not against the table from the previous session.
- Round-trip the types: a Boolean that comes back as a string, or an integer that comes back as a double, fails downstream without an error.
- Inspect the decoded sequence value and treat the two displays of the all-ones 64-bit pattern as one command, not two different ones.
- Force a broker disconnect and reconnect, let the new DBIRTH through, and confirm the next command uses the refreshed aliases.
- If properties vanish after decode, stop commissioning that field and reconcile the object shape with the encoder's schema.
A DCMD is a write to live I/O, so prove the whole flow against a bench device or a shadow group before pointing it at running plant equipment.
The decision set, end to end: aliases are resolved from the latest DBIRTH at send time; the sequence field runs the documented counter when your flow owns the births and the verified convention when a second publisher commands; the payload object matches the installed encoder's schema exactly; and nothing touches live equipment until the round-trip and the reconnect test both pass.
Questions asked
How do I capture a working DCMD to check its sequence value?
Subscribe a plain MQTT input node to the device command topic and record one command from a client already known to work. Feed the captured bytes through the same protobuf decoder you use for DBIRTH and read the sequence field. A signed decoder shows a negative value where an unsigned decoder shows the 64-bit all-ones number, and both are the same wire pattern, so match the representation your own toolchain prints before you publish anything.
Does the same flow work for commands to the edge node itself?
Yes. Node commands use the same payload rules on the node command topic, which is the device command topic without the device level: spBv1.0/{group_id}/NCMD/{edge_node_id}. Payload structure, alias handling and the sequence decision carry over unchanged, and the documented node command that asks for fresh births is how you force every birth to be republished before you send values to a device that just reconnected.
Can a stale alias table corrupt published data too, not just commands?
Yes. Any message that identifies a metric by alias depends on the current DBIRTH mapping, not only device commands. After a reconnect the device may return with different aliases, so a table built from the previous session misroutes both writes and readings while every payload stays structurally valid. Rebuild the mapping on every DBIRTH before trusting any alias-based publish, command or data.
What happens if two Node-RED flows both publish DCMD to the same device?
Only one publisher can own the documented sequence counter, because each increment has to follow the previous message the device actually saw. Either serialize both flows through one shared counter store, or verify the device accepts the interoperability convention and let each flow use it. Serialize the commands themselves as well, so two writes aimed at the same alias cannot interleave.