IMD SolutionComputing LibraryShop main store

Explainer

Sparkplug In node topic is static: route by Group ID with a wildcard

Edge computing and IIoT Published 5 min read By IMD Solution engineering team

Short answer

Nothing documented for the mqtt sparkplug in node templates the Group ID or any other topic segment, so the subscription is a static string. Subscribe with a single-level wildcard in the Group ID position, hold the deployment's Group ID in flow context and reject non-matching topics in a function node; fix the Group ID and wildcard the device instead when the shared-broker traffic matters.

On this page
  1. The subscription topic is a fixed string
  2. Subscribe wide, filter in the flow
  3. The wildcard's position sets the traffic
  4. Verify, then clone
  5. The decision
  6. Questions asked

The topic typed into the mqtt sparkplug in node is a static string: nothing documented for node-red-contrib-mqtt-sparkplug-plus fills the Group ID, or any other segment, from an MMP address or a context variable, and an MQTT subscription does not do parameter substitution. The scalable pattern is to subscribe wider than the flow needs and let a function node decide. Put a single-level wildcard where the Group ID sits, load the deployment's real Group ID into flow context from the string scratchpad area, and reject every topic whose Group ID segment does not match. The price is inbound traffic, because the broker delivers a copy of every matching publish to every matching subscriber, and that price is what decides where the wildcard belongs.

The subscription topic is a fixed string #

The mqtt sparkplug in node from node-red-contrib-mqtt-sparkplug-plus is described as nearly identical to Node-RED's stock MQTT in node with the Sparkplug B handling added: it subscribes to Sparkplug topics and deals with the protocol encoding. Its edit panel takes one topic string, and that literal string is what the node sends the broker when it subscribes. No documented setting of the package reads a variable into it. The broker endpoint is the part you can make dynamic — setting an MQTT broker address dynamically in Node-RED flows covers that — but the subscription filter is not.

The Sparkplug namespace is why a wildcard is safe here. Topics run spBv1.0/Group ID/message type/Edge Node ID/Device ID, and the Group ID and Edge Node ID are user-configurable UTF-8 strings in which the forward slash, the plus sign and the hash symbol are reserved and cannot be used. A + in a subscription therefore always stands for one whole level and never for a character inside an ID, so matching a topic by its segments is unambiguous.

Subscribe wide, filter in the flow #

The working pattern, in order:

  1. Set the in node's topic to spBv1.0/+/DCMD/RIO1/AI1/#, with the plus in the Group ID position and RIO1 and AI1 as the example edge node and device names; message type, edge node and device stay pinned.
  2. Read the deployment's Group ID from the string scratchpad area with a groov input node through its MMP address. If the value never shows up, the failure pattern is the one in why MMP scratchpad values are missing from a Sparkplug payload.
  3. Store the string in flow context, with a change node or a one-line function node.
  4. Pass every received message through a function node that compares the topic's second segment against that value.

An illustrative function-node body: msg.topic is the string the in node delivers, the flow-context key groupId is this example's own name, and its value is a string such as "PlantA" once the scratchpad read has landed.

// group: string, or undefined until the scratchpad read lands
const group = flow.get("groupId");
if (typeof group !== "string" || group.length === 0) {
    return null; // no Group ID yet: drop the command, fail safe
}
// segments: array of strings, e.g. ["spBv1.0", "PlantA", "DCMD", "RIO1", "AI1", ...]
const segments = msg.topic.split("/");
if (segments[1] === group) {
    return msg; // this command is for this group
}
return null;

The proof is two publishes: send a command whose topic carries the configured Group ID and confirm the node behind the filter fires exactly once; then send the same command shape under a different Group ID and confirm nothing fires while a debug node before the filter shows the message arrived. One pass and one drop together are the test; a pass on its own proves nothing about the filter.

Compare whole segments, never substrings #

msg.topic.indexOf(group) is shorter and wrong: IDs that share a prefix cross-match, so a Group ID "Line1" would also accept topics meant for "Line10", and an empty context value then matches every topic on the wire. Split on the forward slash and use strict equality against the one segment the subscription left free.

The wildcard's position sets the traffic #

An MQTT broker matches every publish against every subscription and delivers one copy per matching subscriber, so the copies delivered per command equal the number of nodes whose subscription matches it. The wildcard's position chooses that number.

A worked example, not a measurement: one broker, 3 groups with 4 edge nodes each, so 12 nodes carrying the flow, and 120 device commands issued in an hour at 300 bytes per delivered message.

  • Group ID wildcarded: every command matches all 12 subscriptions, so 120 × 12 = 1440 deliveries per hour; subtract the 120 wanted ones and 1320 arrive only to be dropped. At 300 bytes each that is 1440 × 300 B = 432 000 B, about 432 kB per hour on the broker's outbound.
  • Group ID fixed in the topic, device wildcarded: a command matches only its own group's 4 nodes, so 120 × 4 = 480 deliveries per hour and 360 dropped — exactly one third, which is the number of groups.

The command traffic itself is ordinary DCMD publishing, the kind a control flow produces in sending Sparkplug B device commands from Node-RED, so the real rate can be counted on the broker before you choose.

Subscription (example) Topic edit per deployment Nodes receiving each command
spBv1.0/+/DCMD/RIO1/AI1/# none; the same flow ships everywhere every node on the broker carrying the pattern, across all groups
spBv1.0/PlantA/DCMD/RIO1/# one segment, the Group ID only the nodes of that group

The second row moves the filter one level down: the function node then compares the device segment against a device ID held in flow context.

Verify, then clone #

Before the flow is copied to the fleet:

  1. Confirm the value exists: the editor's context view shows the Group ID in flow context once the scratchpad read has run.
  2. Publish a matching command; the node behind the filter fires once.
  3. Publish a non-matching one; nothing fires downstream while the message is visible arriving at the filter's input.
  4. Deploy the tab to the other nodes unchanged — that is the scalability the static topic could not give you.

The fail-safe direction is what makes this acceptable for commands: with an empty or stale context value the filter drops traffic rather than guessing, so the worst case of a misconfigured node is a command that does not execute, never one that executes against the wrong group.

The decision #

  • One plant broker, a handful of groups, command traffic in the hundreds per hour: wildcard the Group ID and filter in the function node. The example costs 432 kB per hour of discards — noise — and the flow is byte-identical on every node.
  • Groups counted in tens, a broker shared across sites, or continuous command traffic: pay the one-segment topic edit per deployment, fix the Group ID, wildcard below it, and filter on the device segment. Inbound copies then scale with one group's node count instead of the broker's whole fleet.
  • Either way: strict equality on the split segment, the empty-value guard in place, and the two-publish test green before the fleet gets the flow.

Questions asked

Does editing the topic take effect without a redeploy?

No. Node-RED applies node configuration when the flow deploys, and the in node sends its SUBSCRIBE when it connects to the broker, so an edited topic goes live on the next deploy. Schedule the edit for a quiet window: commands published while the node sits between the old and new subscription are lost unless the connection runs a persistent session at QoS 1.

What happens to a command that arrives before the Group ID is in flow context?

It is dropped. The guard returns null while the context value is missing or empty, so a startup race costs one lost command instead of a command executed against a guessed group. Wire the scratchpad read so it runs as soon as the flow starts, confirm the value in the editor's context view, and reissue anything that arrived during the gap; afterwards the filter reads the value fresh on every message.

Can the stock Node-RED MQTT in node do this instead?

The wildcard-plus-filter pattern works behind any MQTT subscription, but the stock in node hands you the payload exactly as published, and Sparkplug B payloads are protocol-encoded. The Sparkplug in node exists to decode that format for you, so keep it in place and do all routing in the function node behind it.

With the Group ID fixed, which segment does the filter compare?

The device segment. Under a subscription with the Group ID fixed and the device level wildcarded, the arriving topic's fifth level, index 4 after splitting on the forward slash, is the device name, so compare that segment against a device ID held in flow context, and keep the same empty-value guard so the fail-safe intent stays explicit.

Keep reading

  • How-to

    Writing MQTT Distributor ACL topic filters for Sparkplug B

    Give each Sparkplug B edge node its own credential with one write filter, spBv1.0/GroupID/+/EdgeNodeID/#, and two read filters, STATE/PrimaryHostID and the same node filter. The ACL is a plain MQTT topic matcher: pin the namespace, group and node as literals, wildcard the message type with + and the device level with #. Any publish or subscribe outside the granted filters disconnects the client, so the NDEATH will and the STATE subscription must be inside the entries.

    Edge computing and IIoT

  • How-to

    Name Sparkplug B group, edge node and device IDs

    Use one Group ID per site (ACME Brewery), one Edge Node ID per aggregation point (Ignition 1), and one Device ID per controller (Wago PLC). Keep forward slash, plus and hash out of the IDs. Write setpoints to the same MQTT Engine tag the PLC publishes: Engine converts the write into a Sparkplug command, Transmission performs it through the local driver, and a DATA message updates the tag.

    Edge computing and IIoT

  • Explainer

    Why MQTT Engine tag hierarchy needs unique Group and Edge Node IDs

    MQTT Engine identifies every Sparkplug publisher by its Group ID and Edge Node ID pair, and Ignition namespace folders never make that identity unique. Two edge nodes sending the same pair collide across sites. Map the Group ID to the company and the Edge Node ID to plant plus area, and add a per-node suffix where an area holds more than one gateway.

    Edge computing and IIoT

Configuring OPC UA to MySQL writes with a Template node