OPC UA vs MQTT: An MQTT PUBLISH Has No Room for Your Timestamp or Quality
An OPC UA DataValue carries StatusCode, SourceTimestamp and ServerTimestamp with every sample. An MQTT PUBLISH carries a topic and bytes. The comparison that actually holds is OPC UA vs MQTT plus Sparkplug B.
A historian trend had 10-second stairs in it. The value sat flat for 10 seconds, then jumped. The equipment was changing every second. Neither OPC UA nor MQTT was at fault. The edge gateway in the middle was stamping each sample with the time it received the MQTT PUBLISH, and it was batching its uploads every 10 seconds.
Hard to blame the gateway. An MQTT PUBLISH packet has no field for a timestamp. A topic name, a Packet Identifier when QoS is 1 or 2, and payload bytes — that is the whole packet (OASIS MQTT Version 5.0, §3.3 PUBLISH). No quality either, and no type. The gateway used the only clock it had.
So "OPC UA or MQTT" is the wrong question. OPC UA is a protocol that attaches a time and a quality to every value. MQTT is a protocol that moves bytes through a broker. The comparison that holds is OPC UA against MQTT plus a payload convention, and in the plant that convention is almost always Sparkplug B.
OPC UA ships four fields with every value
The unit OPC UA hands you is not a number, it is a DataValue, and it has four fields (OPC UA Part 4 — Services, DataValue).
| Field | What it holds |
|---|---|
Value | The value. A Variant, so the type travels with it |
StatusCode | 32 bits. Whether to trust this sample |
SourceTimestamp | When the device produced the value |
ServerTimestamp | When the server received it |
The top two bits of StatusCode are the severity. Good is 0x0000_0000, Uncertain is 0x4…, Bad is 0x8…. You do not have to memorise the code table — >> 30 splits every code into three buckets. DateTime is a 64-bit integer counting 100-nanosecond intervals from 1601-01-01 00:00 UTC (Part 6 — Mappings). Not milliseconds. Decode it against the Unix epoch by mistake and you get dates in the 1600s.
Having two timestamps is the point. The one that belongs on a trend X axis is SourceTimestamp. Use ServerTimestamp and you reproduce those 10-second stairs inside the OPC UA hop instead. If the server polls the device, the gap between the two timestamps is the poll period. Reading StatusCode and SourceTimestamp covers that separately.
The server rewrites the numbers you asked for
This is where the time goes. Every OPC UA subscription parameter is a "requested → revised" pair (Part 4, CreateSubscription and CreateMonitoredItems).
RequestedPublishingInterval→RevisedPublishingIntervalRequestedMaxKeepAliveCount→RevisedMaxKeepAliveCountRequestedLifetimeCount→RevisedLifetimeCount- A MonitoredItem's
samplingIntervalandqueueSize→revisedSamplingIntervalandrevisedQueueSize
A samplingInterval of 0 means the fastest rate the server can practically manage; -1 means use the subscription's publishing interval. LifetimeCount has to be at least three times MaxKeepAliveCount, and a server that gets a smaller value raises it and tells you so in the revised field.
Plenty of clients log what they requested. Very few log what came back. Asking for 250 ms, running at 1000 ms, and finding out weeks later by staring at a trend is a recurring way to lose an afternoon. Print the four revised values right after you connect. It is one line. Subscription tuning is in sampling and publishing intervals and deadbands are in DataChangeFilter. If you want a percent deadband, the node needs an EURange property (Part 8 — Data Access) — without the engineering range the server has nothing to take a percentage of.
Default port is opc.tcp 4840.
MQTT only carries things, and that is deliberate
MQTT 5.0 is an OASIS Standard dated 2019-03-07. 3.1.1 is dated 2014-10-29, and plenty of brokers and devices still speak only that.
Ports are 1883 plain and 8883 over TLS (IANA mqtt and secure-mqtt). Keep Alive is a 16-bit seconds value in CONNECT, and the server disconnects a client that sends nothing within 1.5 times that interval (MQTT 3.1.1 §3.1.2.10). Leave it at 60 and a dead node goes unnoticed for up to 90 seconds. For equipment state that is a long time. Remaining Length is a variable-byte field of at most 4 bytes, which caps one packet at 268,435,455 bytes — in practice your broker's own limit bites first.
QoS 2 is two round trips: PUBLISH → PUBREC → PUBREL → PUBCOMP. I keep finding sites that turned it on for telemetry by default. It buys exactly-once delivery, it does not buy ordering, and strapped to a few thousand one-second tags it puts the broker on the floor before it helps anyone. Use it for state changes and commands; leave measurements on QoS 0.
What is absent: time, quality, type. All three are the payload's problem. That is not a defect in MQTT, it is MQTT's scope. It is a transport.
What Sparkplug B puts back, and what it does not
Eclipse Sparkplug 3.0 is that payload convention. The topic is a fixed shape.
spBv1.0/{group_id}/{message_type}/{edge_node_id}/{device_id}
message_type is a closed set: NBIRTH, NDEATH, NDATA, NCMD, DBIRTH, DDEATH, DDATA, DCMD, STATE. The payload is Protocol Buffers, and every Metric carries name, alias, datatype, timestamp (epoch milliseconds) and is_null.
Time and type come back. The 10-second stairs at the top of this article do not happen on a Sparkplug link that is wired up properly — the edge copies the OPC UA SourceTimestamp into the Metric's timestamp and the problem is gone.
Quality is a different story. Sparkplug's quality mechanism is per node, not per sample. Register NDEATH as the Will Message in CONNECT and the broker publishes it on your behalf when it detects the connection dropped. A bdSeq metric pairs each birth with its death, and seq rolls through 0–255 with NBIRTH at 0, so a subscriber that sees seq skip knows it missed something and can ask for a rebirth over NCMD.
That is where it parts company with OPC UA. There is no per-sample quality code in the base Sparkplug schema equivalent to OPC UA's StatusCode. "The sensor is alive, comms are fine, and this particular reading is suspect" — OPC UA's Uncertain severity — has nowhere to live. You get is_null and the birth/death lifecycle. You can invent a Metric property for it, but then it is your site's convention, not a standard, and it is only worth doing when you own every subscriber.
QoS and retain are specified per message type, so follow the spec's own table rather than picking one setting for everything. Topic layout is expensive to change once anything has published against it — designing a Sparkplug topic you will not have to change goes through that.
It is not a choice between the two
OPC UA Part 14 (PubSub) defines MQTT as a transport, with UADP or JSON payloads. Run that and the "vs" disappears entirely. Field support is still thin — the UDP variant in PubSub commissioning tends to show up first.
What actually decides it:
- You need to browse an address space to find tags → OPC UA. MQTT has no browse. A wildcard subscription is not discovery; a topic nobody has published to does not exist.
- There are writes or method calls → OPC UA. Write and Call return a StatusCode from the server. Sparkplug NCMD/DCMD have no response. An MQTT PUBACK means the broker took it, not that the device did.
- Three or more consumers that do not know about each other → MQTT. Opening a session per consumer against an OPC UA server scales server load with consumer count.
- The link drops → MQTT, because buffering across the outage becomes the edge's problem. Filling the historian gap afterwards is its own problem.
- Both fit → OPC UA between equipment and the edge, MQTT with Sparkplug B from the edge upwards. It is the most common shape and usually the right one.
If you already run that shape, there is one thing to check next. Confirm the edge is copying the OPC UA SourceTimestamp into the Sparkplug Metric timestamp. If it is not, those 10-second stairs are in your trend right now.