IMD SolutionComputing LibraryShop main store

Explainer

Where sensor units belong in an InfluxDB MQTT schema

Edge computing and IIoT Published 6 min read

Short answer

Put the unit in an ingestion mapping that is applied once per sensor, then write every sample with the unit as a tag. Repeated samples with an identical tag set stay in one series, tags are filterable, and a genuine unit change becomes a deliberate new series instead of a relabeled history.

On this page
  1. Where should a sensor unit live in an InfluxDB schema?
  2. What does the unit tag cost in series cardinality?
  3. How do you apply the unit once when MQTT does not carry it?
  4. Should the unit be a tag or a field on each point?
  5. How do you filter dashboards by quantity type, not just unit?
  6. What happens when a sensor's unit changes?
  7. When do you actually need a separate SQL metadata database?
  8. Decision
  9. Questions asked

Store the unit once, in a mapping held by the component that writes InfluxDB, and attach it to every sample as a tag. The MQTT publisher keeps sending only a sensor identity and a value; the enrichment step turns P01 with 10.2 into a point tagged tagName=P01, unit=BarA. Because InfluxDB identifies a series by its distinct tag set, repeating the unit on every point costs no cardinality, while storing it as a field or in Grafana alone takes away the indexed filtering the tag gives you.

Where should a sensor unit live in an InfluxDB schema? #

The unit belongs with sensor identity, not with each raw reading, so the right home is an ingestion mapping applied once and written as a tag on every point. A timestamped value is process history; the unit is part of the sensor definition. Combining them at write time makes every series self-describing, which is what unit-based dashboard filtering needs.

InfluxDB 2 makes tags part of series identity and indexes them, while fields are neither. That asymmetry decides the question. A tag set such as tagName=P01, unit=BarA defines one series no matter how many points arrive, so the "repetition" the tag seems to imply is repetition within a series, not multiplication of series. A field named unit would repeat the same string on every point without being indexed for discovery. Grafana's schema.tagValues() query, which lists the unique values of a tag in a bucket, works only over tags, which is exactly the mechanism a sensor-selection dropdown uses.

What does the unit tag cost in series cardinality? #

Nothing while the tag value is stable, and one extra series when the unit genuinely changes. InfluxDB counts distinct tag-set combinations as series, not points. The number that matters is the count of distinct sensor-and-unit combinations, never the sample count.

Worked example: take 500 pressure sensors sampled every 10 seconds. One day of data is 500 sensors times 8,640 samples, which is 4,320,000 points. With unit as a tag, all 8,640 samples of P01 with unit=BarA collapse into one series, so 500 sensors yield 500 series and cardinality stays flat. With the same unit written as a string field, you still have 500 series but the writer stores the unit text 4,320,000 times as field payload with no indexing benefit. The failure mode that does inflate cardinality is an uncontrolled vocabulary: if one writer sends unit=BarA and another sends unit=barA, the same sensor splits into two series and the count doubles to 1,000. Pick one canonical spelling per unit and read it from the mapping, never from free-form operator text.

How do you apply the unit once when MQTT does not carry it? #

Put the mapping in the writer that consumes the broker and writes InfluxDB, not in the payload. Build an authoritative table from the instrumentation or scaling records — one row per sensor identity with its canonical unit, for example P01 mapped to BarA — and load it into the ingestion component. On each message, look up the sensor, add the unit tag, and write.

The Telegraf path already exists for the topic-to-field part of this: the article on turning an MQTT topic into InfluxDB fields with Telegraf (article:turn-an-mqtt-topic-into-influxdb-fields-with-telegraf) covers the consumer side, and the same enrichment slot is where the unit lookup belongs. Node-RED can do the same before its InfluxDB output. On any typical edge host, such as a fanless industrial mini PC (https://industrialmonitordirect.com/collections/all-mini-pc/), this lookup is one dictionary access per message and is negligible next to the write itself.

Treat mapping faults as configuration faults. If a message arrives for a sensor with no active unit definition, quarantine that point rather than defaulting the unit: a plausible graph labeled with the wrong unit is more dangerous than a visible data gap. Validate that every incoming sensor resolves to exactly one active unit.

Should the unit be a tag or a field on each point? #

A tag, whenever anything downstream must filter or discover by unit. The tag is indexed and appears in schema.tagValues, so a dashboard variable can enumerate it. The field approach stores the same text on every point and then forces a query-time transformation to surface it.

Grafana does offer the Config from query results transformation, which extracts standard options such as Min, Max and Unit from a query, and in combination with InfluxDB it requires Labels to fields first. In the combination reported in the field, it had to be configured per returned field that all share one query name, and new sensors added weekly turn that into continuing panel maintenance. It is a workaround for units stored as fields, not the primary design. If the unit is a tag, the query returns it beside each series and panels or tables can render it directly.

How do you filter dashboards by quantity type, not just unit? #

Attach a stable quantity classification — pressure, temperature, level, flow — in the same enrichment step, as its own tag. Unit spelling alone is a poor filter key: a generic unit such as percent can describe several process quantities, and filtering on the spelling couples dashboard behavior to naming.

With quantity as a tag, the sensor-selection variable changes from listing every tagName to listing only the tagNames whose quantity equals the wanted class, and the panel query filters on that variable using the dashboard time range in the usual way. This also answers the memory problem with large sensor fleets: nobody has to recall that P01 is a pressure sensor when the dropdown only offers pressures for the pressure panel.

What happens when a sensor's unit changes? #

A unit change is a semantic boundary, so give the new unit a new series and keep the old history under its original meaning. The tag mechanism does this naturally: change unit=BarA to unit=bar on the next write and InfluxDB starts a new series, leaving the old points intact and correctly labeled.

What you must never do is edit a timeless lookup row and let the change relabel history. If a one-row P01-to-unit mapping flips and a query then reads two years of points through that mapping, the entire history is mislabeled at once. Either preserve effective dates in the metadata so each point resolves against the definition active at its timestamp, or accept the new-series boundary that the unit tag already provides. Announce the change in the same change-control record that governs the sensor scaling.

When do you actually need a separate SQL metadata database? #

When the sensor definition carries much more than a unit — many attributes, effective-date history, engineering ownership, edit workflows separate from the data path. For a unit and a quantity class, an ingestion mapping plus tags is enough, and adding a second database costs a service, a synchronization boundary and a join on every query that needs the metadata.

If you do go relational, remember that a SQL store imposes a fixed table schema at write time; the article on ODBC logging halting because the table schema is fixed (https://industrialmonitordirect.com/blogs/knowledgebase/daqfactory-odbc-logging-halts-because-the-table-schema-is-fixed) shows what happens when an incoming definition does not fit the columns. The deciding requirement is not whether the unit repeats in InfluxDB — it does, inside one series, at no cardinality cost — but whether every query needs fast direct unit filtering or a richer governed definition.

Decision #

For a plant sensor fleet publishing over MQTT: keep the payload small — sensor identity and value — hold the unit and quantity class in an ingestion mapping, and write both as tags on every point. Use one canonical spelling per unit, quarantine unmapped sensors, and treat a unit change as a new series with its own effective date. Reserve a separate SQL catalog for definitions with many governed attributes or effective-date history; Grafana's query-results configuration stays a fallback, not the design.

Questions asked

Why not store the unit as a field on every point?

A field is not indexed, so it cannot drive schema.tagValues discovery or direct dashboard filtering by unit. You would repeat the same string on millions of points and still need a query-time transformation to read it. If filtering by unit or quantity matters, the unit belongs in a tag.

Does the unit tag explode series cardinality?

No. Series are identified by the distinct tag-set combination, not by sample count. Half a million samples of one sensor with unit BarA stay in one series. Cardinality grows only with distinct tag combinations, so a controlled vocabulary of unit spellings keeps it flat.

What if my MQTT publisher really cannot be changed?

Put the mapping in the writer instead: Telegraf processors, Node-RED before the InfluxDB output, or any collector that consumes the broker and writes the database. The broker path stays untouched, and the database still receives self-describing points with the unit tag.

Can Grafana fill in units automatically from InfluxDB?

The Config from query results transformation can take unit values from a query and apply them as panel standard options, but in the tested combination with InfluxDB it needed Labels to fields first and had to be configured per returned field. A unit tag read beside the series is the more predictable path.

Should I keep sensor definitions in a separate SQL database instead?

Only when the definition carries many attributes, needs effective-date history, or is maintained independently of the time-series writer. For a unit and a quantity class, an ingestion mapping plus a tag is enough, and every query in InfluxDB can filter on it directly.

Keep reading

  • Explainer

    InfluxDB tag and field schema for a PLC historian dashboard

    Store each PLC signal's identity in a tag, its reading in one field named value, and split the signal types into measurements such as temperature, pressure, flow and alarms, writing binary states only when they change. At a 1.5-second cycle a 130-signal line produces about 86.7 samples per second, so long raw windows are what slow a dashboard: a 30-second mean bucket cuts a ten-signal full-window read from about 288,000 points to 14,400.

    Edge computing and IIoT

  • Selection guide

    Choosing an edge data path for periodic records

    For independent periodic samples, Tag Historian with Edge Sync Services is the reliable path and buffers 35 days locally. For one atomic row per collection cycle, use MQTT Transmission records landing in MQTT Recorder. For true transaction groups, license a standard gateway for that node.

    Edge computing and IIoT

  • Troubleshooting

    Historian stores zero after an MQTT edge node reconnects

    A 0 stored just before the true value at every MQTT edge reconnect is a reported pattern on Ignition 8.1.5 through 8.1.15 with the MQTT Transmission and Engine modules 4.0.6 through 4.0.10. Confirm it by querying stored records around each reconnect, exclude those zeros from process data, and remove them with a timestamped rule instead of a blanket zero filter.

    Edge computing and IIoT