← Articles
MQTT/9 min read/— views

Where to Put the MQTT Schema Version So a Gateway Update Can't Silently Corrupt Your Historian

MQTT 5.0 Content Type and User Property, Sparkplug B seq and rebirth, RFC 3339 offsets, and the retained-message trap — versioning edge telemetry payloads so consumers break loudly instead of quietly.

MQTTSCADAEdgeHistorianMES

The day bar became kPa

A gateway firmware update changed a pressure reading from bar to kPa. The field name stayed headerPressure. The value went from 1.8 to 180.

The trend kept drawing. No alarm fired. Eleven days later somebody reading a shift report asked why the process had changed, and that is how we found out. The historian holds eleven days of wrong data, and that part does not get undone.

If the field name is the same, consumers believe it is the same value. That is the whole mechanism.

The topic name is only part of the interface. Consumers also depend on:

  • Field names and nesting.
  • Data types and engineering units.
  • Which clock made the timestamp, and in what format.
  • Quality and stale-data meaning.
  • Null, missing, and default value behavior.
  • Whether messages are full snapshots, deltas, or events.
  • Retain, QoS, and replay behavior.

MQTT 5.0 already has a place for the version

MQTT 5.0 lets a PUBLISH packet carry properties, so the version can sit in the header instead of being mixed into the payload body. Three of them are worth memorising by identifier.

PropertyIdentifierHow to use it
Content Type0x03application/vnd.plant.telemetry+json;v=2.1 — version pinned into the media type
User Property0x26key/value pairs such as schemaVersion=2.1, fwVersion=4.8.2; more than one allowed
Payload Format Indicator0x010 means unspecified bytes, 1 means UTF-8 character data

Send Payload Format Indicator as 1 and the bytes are not valid UTF-8, and the receiver is entitled to refuse with reason code 0x99, Payload format invalid. It breaks immediately instead of being quietly wrong. That is the better outcome.

How large a packet the broker will accept comes back in the Maximum Packet Size property (0x27) in CONNACK. The protocol ceiling is 268,435,455 bytes, because Remaining Length is at most 4 bytes, but real broker settings are far below that. Topic names go up to 65535 bytes.

The catch is how much MQTT 3.1.1 equipment is still in the field. 3.1.1 has no properties at all. There the version has to live inside the payload or in the topic branch.

{
  "schemaVersion": "2.1",
  "source": "line-3/oven-2/gateway-a",
  "timestamp": "2026-06-25T08:14:22.341Z",
  "quality": "good",
  "metrics": {
    "zone1TemperatureC": 184.6,
    "headerPressureKpa": 180.4
  }
}

Putting the unit in the field name is not a style preference. It is the structural fix for the eleven days above. The tidier version is to carry a UNECE Recommendation 20 unit code in the metadata — BAR for bar, KPA for kilopascal, CEL for degree Celsius. OPC UA Part 8's EUInformation draws on the same code list. A code beats the free-text string "kPa" because a code can be compared.

JSON numbers hold one more trap. Most parsers read a JSON number as an IEEE 754 double, and a double cannot hold integers above 2^53 = 9,007,199,254,740,992 exactly. Send 64-bit counters and nanosecond timestamps as strings. Otherwise the last digits of large values change on their own.

With Sparkplug B, the version belongs in the BIRTH

Sparkplug answers the schema question structurally. The Eclipse Sparkplug 3.0 topic namespace is spBv1.0/<group_id>/<message_type>/<edge_node_id>/[<device_id>], with message types NBIRTH, DBIRTH, NDATA, DDATA, NDEATH, DDEATH, NCMD, DCMD, and STATE.

Three things a host-side developer has to know.

  • Metric names only exist in the BIRTH. NDATA and DDATA are allowed to carry the alias alone. The table mapping an alias back to a name, data type, and unit is in the NBIRTH/DBIRTH. A subscriber that missed the BIRTH cannot decode DDATA at all. That is why the schema version belongs in the BIRTH as a metric.
  • seq runs 0 to 255 and rolls over, and NBIRTH is seq 0. It increments by one per message, with 255 followed by 0. A gap means something was missed, and the host then requests a rebirth by writing the Node Control/Rebirth metric true on the NCMD topic. That is the step the host must take after every edge update.
  • Read the is_historical flag. It is set true when store-and-forward replay arrives, meaning: do not treat this as the current value. The same payload carries is_null and is_transient too.

NDEATH is registered as the will message, and the bdSeq metric is what confirms it pairs with the NBIRTH. The topic layout side is in designing Sparkplug topic namespaces.

Whether a change is additive is decided by additionalProperties

Semantic Versioning 2.0.0 says to bump MAJOR for an incompatible change. True, but out in MQTT the boundary of "incompatible" sits in the consumer's configuration.

Adding one optional field is additive only if consumers ignore unknown fields. In JSON Schema, additionalProperties defaults to true, which is why it usually passes. The moment somebody sets "additionalProperties": false, every added field is a breaking change. Check the validation settings before you read the table.

ChangeUsually safe?Field concern
Add an optional metricSafe while additionalProperties is trueBreaking the instant it is false
Add a required fieldBreakingOld publishers cannot provide a value
Rename a metricBreakingHistorian mappings and dashboards go stale
Change unit BAR to KPABreakingValues look plausible and are wrong. The eleven days above
Move the timestamp from gateway time to PLC timeBreakingEvent ordering and report boundaries shift
Add a quality valuePossibly breakingConsumers treat the unknown one as good
Change snapshot publish to deltaBreakingMissing fields get read as zero
Reassign alias numbers (Sparkplug)BreakingWithout a rebirth, values land on the wrong tags

Field systems should stay conservative here. A value that is wrong but plausible is more dangerous than one that clearly fails validation.

Timestamps and quality are not a matter of taste — there are standards

Use the RFC 3339 date-time form for timestamps. The offset is mandatory. 2026-06-25T08:14:22.341Z is UTC; +09:00 is KST. Local time with no offset is not in the RFC 3339 grammar at all. RFC 3339 §4.3 defines a separate convention for "UTC is known but the local offset is not": write it as -00:00, which means something different from +00:00. Sparkplug B is simpler — payload and metric timestamps are both milliseconds since the Unix epoch in UTC. With no offset field, the only remaining question is which clock stamped it.

For quality, borrowing the OPC UA Part 4 StatusCode structure settles the argument cheaply. The top two bits are the severity:

  • Good: 0x00000000
  • Uncertain: 0x40000000
  • Bad: 0x80000000

The three-way split is a structure written into a standard, not a local convention. Publish good/uncertain/bad in JSON and map those three in the historian, and the situation where one gateway sends bad, another BAD, and a third 0 goes away.

Write down the following per schema version:

  • Whether timestamp is made by the PLC, the edge gateway, the broker, or the consuming application.
  • Whether it is sample time, publish time, or broker receive time.
  • Whether a stale metric stays in the last snapshot, drops out, or is marked uncertain.

None of this can change quietly. A historian backfill and a live HMI subscribe to the same topic and use the timestamp differently.

Retained messages still hold the old schema

This is the one that catches people after a deployment. A retained message left by the old gateway is delivered to every new subscriber. The value is weeks old and the schema is v1.

The protocol gives three ways out.

  • Clear it: publish a zero-length payload to the same topic with the retain flag set and the retained message is deleted (MQTT 3.1.1 §3.3.1.3). Put that one line into the firmware-swap procedure.
  • Refuse it: in MQTT 5.0, the SUBSCRIBE subscription option Retain Handling set to 2 means no retained messages are sent at subscribe time (§3.8.3.1). 1 means "only on a new subscription".
  • Let it die: the MQTT 5.0 Message Expiry Interval property (0x02) is in seconds. Set it and old retained messages disappear on their own.

The rollout itself does not finish in one go either. One line goes today, the next at the following shutdown. So publish old and new fields together for a stated period, or put a breaking schema on a v2 topic branch and keep v1 alive temporarily. Give dual publishing an end date. Without one you have a system running two schemas forever.

Verify against the real consumers

A JSON schema check is useful and not sufficient. Walk these paths before production cutover.

  1. The HMI or SCADA client shows live values in the right unit with the right quality.
  2. The historian stores them under the expected tag names and timestamps. Count the tags and compare.
  3. The MES connector takes events with no duplicates and no gaps. QoS 1 is at-least-once and a redelivery sets the DUP flag — duplicates are behavior the protocol permits. Without an idempotency key in the connector, production counts twice.
  4. Alarm logic handles missing values and bad quality safely.
  5. Dashboard jobs ignore unknown optional fields without complaint.
  6. Store-and-forward replay does not turn old payloads into current values. On Sparkplug, confirm is_historical is actually being read.

One happy-path test message is not enough. Include a normal one, a bad-quality one, one with the optional field missing, a retained or replayed one, and a version mismatch.

The record you keep can be short

Per version, this much is enough.

  • Version number, release date, and the breaking changes against the previous version.
  • One example each of a normal, a bad-quality, and a replay message.
  • Per field: type, UNECE unit code, required or optional, meaning.
  • The timestamp and quality rules.
  • Known consumers and their migration status.
  • What happens if the edge update is rolled back.

It has to be readable without the SCADA engineering tool. The person recovering at 3 a.m. has not installed that tool yet. Broker certificate work often lands in the same deployment window, and that is covered in rotating an MQTT broker certificate.

The next thing to check is already decided: pull one payload off production now and see whether the field names carry their units. If they do not, that topic is the candidate for the next eleven days.