Emre Şahin

Diario de ingeniería

Time is a sensor too

In IoT telemetry, timestamps lie. Event time, late batches, clock skew and dead-reckoning through GPS loss, with uncertainty made explicit.

Emre Şahin ·

We tend to treat a timestamp as metadata: a neutral label attached to the real measurement. On a rail condition-monitoring platform I contribute to, that assumption turned out to be the root of a whole class of bugs. A timestamp is a measurement. It comes from a clock, clocks drift, and the moment a reading reaches the server says very little about the moment it was taken.

The platform locates vibration readings on a rail line. Sensors on a vehicle measure vibration, a separate GPS unit reports position, and the backend combines them to say where along the line something happened. That works neatly in a demo. In the field, the data arrives late, out of order, in batches and with gaps, and the code has to cope with all of it.

Two clocks, one axis

The first problem we found was structural. The live position tracker kept a single time axis, and two sources fed it:

  • GPS fixes, stamped with the device's event time.
  • A 1 Hz heartbeat, stamped with the server's wall clock.

Those two are equal only if the device clock matches the server clock and delivery is instant. Neither is true in practice, and every quantity derived from time differences inherited the error:

  • Clock drift or latency distorted every dt, every speed estimate and every dead-reckoning step.
  • An out-of-order fix (older than the last accepted one) skipped the plausibility check, moved the position backwards along the line, and then made the next tick jump forward by a huge dt.
  • A late batch looked like a long outage. The tracker dead-reckoned a trajectory that never happened, then contradicted it when the real data arrived.
  • Device silence and GPS loss were indistinguishable. The system only observes arrivals. "The device is offline" (freeze) and "the device is in a tunnel without GPS" (dead-reckon) produced the same signal.

Separate the clocks, then project

The fix is an old idea from stream processing: never subtract event time from processing time. Keep them on separate axes and translate between them explicitly.

Old:  outage_age     = now(wall) − last_fix(event)          // mixes clocks
New:  event_now      = last_fix(event) + (now − arrival_of_last_fix)
      silence        = now − last_arrival_of_any_packet

The projection moves the device's own timeline forward by however much real time has passed since we last heard from it. On clean, real-time data it is identical to the wall clock, so existing behaviour is unchanged. On messy data it stops two clocks from bleeding into each other.

On top of that, three rules:

  1. Bounded out-of-orderness. A late or out-of-order observation does not rewind live state. It can still be stored and analysed, but the live tracker only advances.
  2. Silence is not an outage. If no packet of any kind has arrived, freeze. If packets arrive but GPS fixes do not, dead-reckon.
  3. Only live fixes drive the live tracker. Batched history goes to analysis, not to the real-time estimate.

Interpolation is a claim

The analytical path had a quieter version of the same problem. To place a stored vibration reading on the line, it looked up the GPS track at that timestamp and interpolated between the surrounding fixes. The gap between those fixes was bounded only at the outer edges of the track. Inside it, two fixes a long time apart were still joined by a straight, constant-speed line, and the answer came back as a confident coordinate with no error bar.

That is not a position. It is a guess, presented as a measurement.

The interesting part was that the right pattern already existed in the codebase, in the peak analysis. It used what we started calling an evidence ladder:

  1. A GPS fix within a few seconds of the reading: use it, with the receiver's accuracy as the uncertainty.
  2. Two fixes bracketing the reading within a short window: interpolate, with uncertainty that grows with the distance between them.
  3. Further out, a bounded dead-reckoning step whose uncertainty keeps growing.
  4. Beyond a distance threshold: refuse. Return "unlocated".

The work was mostly to apply that standard consistently. A robust locator brought the ladder to the surface map, sharing its tolerances with the peak analysis so the two cannot drift apart. Readings that cannot be honestly placed are dropped from the map instead of being drawn somewhere plausible.

Every position now carries its uncertainty. On a rail line that matters, because positions are expressed as linear references (distance along a specific track) rather than raw coordinates. A metre of error can be the difference between two adjacent assets. An inspection report that says "here, ±15 m" can be acted on. One that says "here" when the truth is "somewhere in this 2 km stretch" is worse than no answer.

Two devices, two clocks

Matching readings to GPS fixes inside a few-second window assumes the vibration sensor and the GPS unit agree on the time. They are physically separate devices, so they do not, and a systematic offset quietly chooses the wrong fix.

We added a small clock-skew estimator. From matched pairs it computes the median offset between the two devices, its spread, and whether the bias exceeds half the matching window. The median keeps a few bad matches from moving the estimate. It turns an invisible distortion into a number someone can watch.

We did not yet wire it into the pipeline to correct the matching. Correcting would change values that customers already see, and that deserves its own flagged rollout. Observing first and correcting later is a deliberate order.

Constraints we kept

The work happened inside firm boundaries, and I think the boundaries made it better:

  • The external contract stays byte-identical. New behaviour is behind flags or isolated, and the existing characterisation tests must stay green on clean data.
  • No synthetic data, no new writes. The analytical path is read-only. If we cannot measure it, we do not invent it.
  • Pure functions. The timeline, the locator and the skew estimator have no I/O, which made the nasty scenarios (late batch, skewed clock, out-of-order fix) easy to put in unit tests.

One gap we could not close: the telemetry tables store only event time. Without a received_at column, lateness and device clock drift cannot be measured after the fact. Adding it needs a schema change, which was out of scope, so it is written down as the first recommendation for the next schema round. Better to record a known blind spot than to pretend we can see it.

The general lesson

If you take one thing from this: treat time as an input with its own error model, not as ground truth.

  • Keep event time and processing time on separate axes, and never subtract one from the other.
  • Decide what late data is allowed to change, and make live state monotonic.
  • Distinguish "no data" from "no signal".
  • Attach uncertainty to every derived value, and set a threshold where the honest answer is "unknown".
  • Store arrival time next to event time. You will want it the first time a customer asks why a reading is in the wrong place.

Sensors will always be imperfect. The design choice is whether the system admits that or hides it behind confident numbers.