2018 SenML: Resolving timestamps in a batch of sensor readings
Read the 2018 SenML model through a greenhouse batch. Resolve two observations and see why a raw record loses meaning if it is split from its base fields.
Archive retrospective · Topic year 2018
In this article
Use a short batch to expose the shared state
A greenhouse gateway takes one temperature reading from each end of a building. The north observation establishes a time for the batch and the south observation follows fifteen seconds later. Repeating the common name prefix, time and unit in every record would make the payload easier to split but less compact. The 2018 SenML specification addresses this tradeoff with base fields in its shared data model. This article uses the JSON representation of that model. The processing goal is to turn the transmission into observations that can be used in isolation. The gateway can send a concise message, while analysts can still ask for the south measurement and get a complete name, time and unit. Translating between those two forms is an ingestion task, and it has a definite place before a worker splits the array.
Resolve the names and the temperature unit
The first record carries a base name, a base time and a base unit. The base name contains the trailing slash itself. Combine it with north or south without inventing a separator. The resulting names are greenhouse/7/north and greenhouse/7/south. Changing the base name later in a pack should affect that record and subsequent records, so sorting the raw array first would change the meaning. The stored observations can be sorted after resolution. The example shares Cel as its unit. The SenML model allows a record unit to override the base unit. This small gateway contract keeps the unit the same for the whole pack and rejects fields outside its known subset. That restriction should be documented for the two ends of the connection. A reader implementing the standard generally has more combinations to handle than a greenhouse importer has agreed to accept.
Keep the event time and arrival time available
Add each record's offset to the shared base. Zero and fifteen appended to the chosen base yield 1540000000 and 1540000015. The values are seconds. In the SenML time convention, a resolved time at or above two to the power of twenty-eight denotes an absolute time. Below that threshold it has relative semantics. The code is for one workflow that needs absolute time, so the relative case is an explicit rejection. A collector that reconnects at the end of the day may deliver a backlog together. Plotting all observations at arrival time can hide a temperature change that occurred much earlier. Keep arrival time for operational questions and use the decoded measurement time for the series. Separately check a plausible date range for your deployment. A valid number in the wrong scale can otherwise waste time during an incident investigation.
Exercise the importer with the second record alone
On Windows, Node.js 24.19.0 produced two resolved records. North was 19.25 Cel at 1540000000 and south was 19.75 Cel at 1540000015. The test also feeds the second record alone as a new pack and checks that the importer throws. This negative case is valuable because a queue that splits records too early can have a passing happy-path test for the first item. No 2018 runtime or full SenML implementation was run. The JavaScript implements an explicitly narrow temperature contract on a current local runtime. It does not accept numeric base values, arbitrary extensions or multiple value kinds. The specification supplies the resolution rules that this sample exercises. Other SenML producers should be checked against an implementation with the vocabulary they actually emit.
import assert from 'node:assert/strict';
const fixture = [
{bn: 'greenhouse/7/', bt: 1540000000, bu: 'Cel', n: 'north', t: 0, v: 19.25},
{n: 'south', t: 15, v: 19.75}
];
function resolveTemperaturePack(pack) {
const base = {};
const supported = new Set(['bn', 'bt', 'bu', 'n', 't', 'v']);
return pack.map((row) => {
assert(Object.keys(row).every(key => supported.has(key)), 'unsupported field');
for (const key of ['bn', 'bt', 'bu']) {
if (Object.hasOwn(row, key)) base[key] = row[key];
}
assert(typeof base.bn === 'string' && typeof row.n === 'string');
assert.equal(base.bu, 'Cel');
assert(Number.isFinite(row.t) && Number.isFinite(row.v));
const time = base.bt + row.t;
assert(Number.isFinite(base.bt) && time >= 2 ** 28, 'absolute time required');
return {name: base.bn + row.n, time, unit: base.bu, value: row.v};
});
}
const actual = resolveTemperaturePack(JSON.parse(JSON.stringify(fixture)));
assert.deepEqual(actual, [
{name:'greenhouse/7/north',time:1540000000,unit:'Cel',value:19.25},
{name:'greenhouse/7/south',time:1540000015,unit:'Cel',value:19.75}
]);
assert.throws(() => resolveTemperaturePack([fixture[1]]));
console.log(JSON.stringify(actual, null, 2));
Store observations for querying and evidence for repair
Write the resolved records to whatever system serves the time series. Retain the original message or a bounded reference to it as appropriate if the ingestion team needs to reproduce an error. Give that evidence a clear lifetime and record which resolution code produced a set of measurements. That makes it possible to explain why an observation disappeared from a dashboard after a format change and to replay a controlled fixture without sending a command to the physical device. Also agree on how retries will work. The same transport message can arrive twice. Conversely, two observations can have the same sensor name and second. If the two cases are possible, a key derived only from name and second can collapse real observations. Look for a reliable message identity, producer scope and record position that match the transport contract.
Test the boundary with a second producer
Build three packs that exercise a base change, a late reading and an omitted required base. Keep expected resolved values for the first two and a clear error for the third. A second producer may send a different encoding later. Run the same semantic cases after decoding that encoding and check the individual timestamps, names, units and values. Give independent tests to encoding and validation, because a mismatched baseline and a parse error call for different repairs. Finally, choose a definition of completion for a batch. The system might complete parsing and then fail to persist the observations. Save only the receipt your storage operation can actually guarantee. When ingestion happens in two stages, record which stage accepted the batch so retries are directed at the right component. The gateway and the data team can then use one concrete record to diagnose a real disagreement.
Reader discussion
Which data would you verify before applying this development in your field?
No comments yet. Be the first to contribute.
Write a comment