JAlcocerTech E-books

Sensor and Telemetry Data: Time-Series in Practice

Most of this book is about business data: orders, customers, events from apps. Sensor and telemetry data behaves differently. It’s append-only, it comes from devices you can’t fully trust, and nearly every question you ask it starts with over time.

This chapter uses three datasets I worked with: a home sensor network, drone flight logs and Formula 1 telemetry.


The Mental Model: Each Store Wants Something Different

Prometheus wants metrics, Elastic wants documents, TimescaleDB wants rows, and MQTT is just the transport.

StoreWantsModelGood at
PrometheusMetrics: the current value of named gauges and countersPull: it scrapes an HTTP endpointMonitoring, alerts on the now
ElasticsearchDocuments: one event record eachPush via an ingest pipelineSearching event history
TimescaleDBRows: PostgreSQL with time partitioningSQLHistory, joins, ad-hoc questions
MQTTNothing. It moves messagesPub/subGetting data off devices

Picking a store is picking which of those questions you’ll find easy. Once that choice is clear, the bridges follow:

  • MQTT → Prometheus: easy if payloads are numeric and topics are stable. A small exporter subscribes to MQTT and exposes the values for scraping.
  • TimescaleDB → Prometheus: unnatural. Prometheus expects current values, not historical rows, so you export a derived gauge (latest value, a recent average) from a custom exporter or a scheduled job.
  • MQTT → Elasticsearch: Logstash, Elastic Agent or a small consumer turns messages into documents.

Case 1: Home Sensors into TimescaleDB

The plant monitor (DHT22 and MLX90614 on a Pico W and an ESP32, publishing over MQTT) needed history, not just a live gauge. (IoT x Plants)

Storage. TimescaleDB in a container, with one narrow table:

CREATE TABLE readings (
    ts     TIMESTAMPTZ      NOT NULL DEFAULT NOW(),
    topic  TEXT             NOT NULL,
    value  DOUBLE PRECISION NOT NULL
);
SELECT create_hypertable('readings', 'ts');

create_hypertable partitions the table by time behind the scenes. You keep plain SQL, and time-range queries stay fast as the table grows.

Ingestion and serving. Two small processes, each in its own tmux session on the Pi:

  • mqtt_to_db.py subscribes to the sensor topics and inserts rows.
  • A FastAPI app (uvicorn main:app) reads from the database and pushes new readings to a web page over websockets.

Asking questions. Hourly averages per sensor, the query every dashboard ends up needing:

SELECT time_bucket('1 hour', ts) AS hour,
       topic,
       avg(value) AS avg_value,
       min(value) AS min_value,
       max(value) AS max_value
FROM readings
WHERE ts > now() - INTERVAL '7 days'
GROUP BY hour, topic
ORDER BY hour DESC;

Derived data. The raw readings aren’t the point. The decision metric is VPD (vapour pressure deficit), computed from temperature and humidity. At 70% RH it’s about 0.51 kPa at 15 °C and about 1.27 kPa at 30 °C: same humidity, more than twice the drying power. VPD is a derived dataset: rebuildable from the source rows at any time, so store the readings and compute VPD from them.


Case 2: Drone Flight Logs, Batch Not Stream

An FPV drone’s flight controller (Betaflight) writes a blackbox log to onboard flash. Nothing streams during the flight. After landing, the log is decoded to JSON and analysed. (Drone Telemetry 101)

That’s a reminder that “telemetry” doesn’t always mean streaming. If the decision happens after the event (tuning, battery planning, learning to fly), a batch file plus a script is the right architecture.

Selected throttle bands from one 158-second flight:

ThrottlePowereRPMCurrent
10–15%~7.7 W~7461.82 A
20–25%~8.7 W~8172.21 A
30–35%~11.9 W~1,0663.07 A
35–40%~14.9 W~1,1773.88 A
40–45%~16.5 W~1,2104.46 A

Two findings that only show up once the data is shaped like this:

  • Power rises faster than RPM as throttle increases.
  • Around 35–40% throttle the quad “coasts” at roughly 14–15 W and ~1,170 eRPM. That’s the efficient cruising band.

The output is a PWA that plots each flight, built on the decoded JSON: no database at all.


Case 3: Formula 1, Someone Else’s Telemetry

F1 Data Animations uses the fastf1 Python package, which sources timing and telemetry through an API. Detailed car telemetry goes back to 2018; lap timing goes back to 1950.

The lesson is about coverage: always check how far back each field goes before designing an analysis. A model that needs telemetry silently drops every season before 2018.


Rules That Hold Across All Three

Timestamps come from the source. Messages arrive out of order: a node reconnects and replays, or a broker restarts. Store the device’s timestamp and order by it, not by arrival time.

Make ingestion idempotent. Devices retry, and MQTT QoS 1 is “at least once”. Give each reading a natural key (device + timestamp) and make writing it twice harmless, for example a unique constraint with ON CONFLICT DO NOTHING.

Raw is the system of record, the rest is derived. Keep raw readings for as long as you can afford to. Hourly averages, VPD and per-band summaries can always be recomputed.

Plan retention on day one. Raw data at full resolution for weeks, aggregates for years. TimescaleDB does both natively: continuous aggregates keep a rollup up to date, and a retention policy drops old raw chunks:

CREATE MATERIALIZED VIEW readings_hourly
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 hour', ts) AS hour, topic, avg(value) AS avg_value
FROM readings
GROUP BY hour, topic;

SELECT add_retention_policy('readings', INTERVAL '90 days');

Match the architecture to when you decide. Live alerts need a stream and a metrics store. Analysis after the fact needs files and a script. The drone never needed Kafka.

For the device side of these systems (sensors, firmware, MQTT and edge logic), see the companion book Build Your Own IoT Stack.