Sparkplug API
Sparkplug is an open-source MQTT specification that defines a standard topic namespace and payload format for industrial IoT devices. ThingsBoard acts as a Sparkplug-aware MQTT broker supporting Sparkplug B up to version 3.0.
Concepts
Section titled “Concepts”Edge of Network (EoN) Node
Section titled “Edge of Network (EoN) Node”The EoN Node is any MQTT v3.1.1 or v5.0 client that manages a session and acts as a gateway between field devices (PLCs, RTUs, sensors) and the MQTT broker. ThingsBoard connects to EoN Nodes directly — each EoN Node is represented as a device in ThingsBoard.
Sparkplug B Device
Section titled “Sparkplug B Device”A single EoN Node may represent multiple physical devices. ThingsBoard decodes metrics from the Sparkplug payload and stores them as device Attributes or Time-Series Data. You can push updates back to device metrics via shared attribute changes or RPC commands.
Device Naming & Uniqueness
Section titled “Device Naming & Uniqueness”To prevent naming collisions across different groups or nodes, ThingsBoard enforces a fully qualified, unique namespacing convention for Sparkplug child devices (topics that include a deviceId) using their hierarchy:
{groupId}:{nodeId}:{deviceId}- Avoids Collisions: Ensures that multiple devices with the same
deviceIdcan coexist seamlessly if they belong to different Groups or Nodes. - Clear Identification: Makes it easy to identify the exact Group and Node a device belongs to directly from the ThingsBoard device list.
Naming Comparison Examples:
Section titled “Naming Comparison Examples:”| Aspect / Context | Before (Legacy) | After (Current) |
|---|---|---|
EoN Node (e.g., groupId: Group_A, nodeId: Node_5) | Plant_A | Plant_A (or any unique name according to Sparkplug 3.0 requirements) |
Child Device (e.g., deviceId: Sensor-01) | Sensor-01 | Group_A:Node_5:Sensor-01 |
Topic Structure
Section titled “Topic Structure”spBv1.0/{group_id}/{message_type}/{edge_node_id}[/{device_id}]| Segment | Description |
|---|---|
spBv1.0 | Sparkplug B version namespace |
group_id | Logical grouping of EoN Nodes |
message_type | One of the types listed below |
edge_node_id | Unique identifier of the EoN Node |
device_id | (optional) Identifier of a device managed by the EoN Node |
Group ID, edge node ID, and device ID must be valid UTF-8 strings — the characters +, /, and # are reserved and must not be used.
Message Types
Section titled “Message Types”| Type | Direction | Description |
|---|---|---|
NBIRTH | Device → Server | EoN Node birth certificate — announces the node and all its metrics |
NDEATH | Device → Server | EoN Node death certificate — sent as MQTT Will Message |
DBIRTH | Device → Server | Device birth certificate — announces a device and its metrics |
DDEATH | Device → Server | Device death certificate |
NDATA | Device → Server | EoN Node metric data update |
DDATA | Device → Server | Device metric data update |
NCMD | Server → Device | Command to an EoN Node |
DCMD | Server → Device | Command to a device |
STATE | Server | Sparkplug Host Application online/offline state |
NRECORD | Device → Server | EoN Node record message |
DRECORD | Device → Server | Device record message |
BIRTH messages must use QoS 0. NCMD/DCMD subscriptions must use QoS 1. The MQTT Will Message (NDEATH) must use QoS 1.
Connection Requirements
Section titled “Connection Requirements”Persistent vs Non-Persistent Connections
Section titled “Persistent vs Non-Persistent Connections”Persistent (recommended for always-on devices):
- Stay connected at all times
- Never send
DISCONNECTduring normal operation - Host Applications track real-time state via BIRTH/DEATH messages within the Keep Alive period
Non-Persistent (GPS trackers, periodic sensors):
- Connect temporarily, send data, then disconnect
- Send
MQTT DISCONNECTbefore going offline for a graceful disconnect - Do not send DEATH messages — Host Applications only see the Last Known Good data with a timestamp
For non-persistent devices: send a DEATH certificate upon reconnecting before disconnecting again so the Host Application maintains accurate state.
Protocol settings for non-persistent sessions:
- MQTT 3.1.1 →
cleanSession = true - MQTT 5.0 →
cleanStart = trueandsessionExpiryInterval = 0
Metric Aliases
Section titled “Metric Aliases”An alias is an optional uint64 that identifies a metric to reduce payload size. Rules:
- If no alias is used, the metric Name must be included in every message.
- Aliases defined in
NBIRTH/DBIRTHmust be unique across all metrics of that node. NBIRTH/DBIRTHmust include both the metric name and its alias.NDATA,DDATA,NCMD,DCMDmust use only the alias — the name must be omitted.
Getting Started
Section titled “Getting Started”Step 1 — Create a Device Profile
Section titled “Step 1 — Create a Device Profile”- Go to Profiles → Device profiles and click + to add a new profile.
- Name it (e.g.
MQTT EoN Node) and go to the Transport configuration tab. - Select MQTT transport type and enable MQTT Sparkplug B Edge of Network (EoN) node.
- Enter metric names to store as shared attributes instead of telemetry — these are the metrics you want to push back to the device. Wildcard suffix
*is supported (e.g.Node Control/*,Device Control/*,Properties/*). - Save the profile.
Step 2 — Create the EoN Node Device
Section titled “Step 2 — Create the EoN Node Device”- Go to Entities → Devices and click + to add a device.
- Name it (e.g.
Node 1) and select the profile created in Step 1. - Open the device details and copy the Access Token (or configure other credential types).
Step 3 — Connect Your EoN Node
Section titled “Step 3 — Connect Your EoN Node”Use the access token as the MQTT username, connect to port 1883 (plain) or 8883 (TLS), and begin publishing BIRTH and DATA messages following the Sparkplug B topic structure.
For testing, use the ThingsBoard Sparkplug emulator:
docker run -e SPARKPLUG_SERVER_URL='tcp://$THINGSBOARD_HOST:1883' -e SPARKPLUG_CLIENT_MQTT_USERNAME='YOUR_ACCESS_TOKEN' thingsboard/tb-sparkplug-emulator:latestThe emulator will publish NBIRTH for the node and DBIRTH for two simulated devices. ThingsBoard automatically creates child device records for each Sparkplug device using the {groupId}:{nodeId}:{deviceId} unique naming format.
Step 4 — View Telemetry and Attributes
Section titled “Step 4 — View Telemetry and Attributes”- Latest telemetry tab of the EoN Node device shows metric values published via
NDATA/DDATA. - Shared attributes tab shows metrics configured in Step 1 that ThingsBoard stores as attributes instead of telemetry.
Child devices (e.g. Group_A:Node_5:Sparkplug_Device_1) appear automatically on the Devices page with their own telemetry and attributes.
Pushing Updates to Devices
Section titled “Pushing Updates to Devices”Via Shared Attributes
Section titled “Via Shared Attributes”Change a shared attribute in the UI, dashboard widget, or REST API. ThingsBoard automatically sends the updated value to the device as a DCMD (for device metrics) or NCMD (for node metrics).
Payload format when sending from shared attributes (ThingsBoard adds the command wrapper automatically):
{"MyFloat": 123.345}To send an explicit command type, use this format as the attribute value:
{"NCMD": {"MyFloat": 123.345}}{"DCMD": {"MyFloat": 123.345}}{"NDATA": {"MyFloat": 123.345}}{"DDATA": {"MyFloat": 123.345}}Via RPC Command
Section titled “Via RPC Command”Send a server-side RPC using the REST API, a dashboard widget, or a Rule Chain node. See Command & Control for the full RPC reference.
| Property | Value |
|---|---|
method | NCMD (to EoN Node) or DCMD (to device) |
params | {"metricName": "...", "value": ...} |
Reboot an EoN Node:
{ "method": "NCMD", "params": {"metricName": "Node Control/Reboot", "value": true}}Reboot a device:
{ "method": "DCMD", "params": {"metricName": "Device Control/Reboot", "value": true}}Was this helpful?