IMD SolutionComputing LibraryShop main store

How-to

Filter MQTT fields in Telegraf before they reach InfluxDB

Edge computing and IIoT Published 7 min read

Short answer

Yes: add fielddrop to the mqtt_consumer input block in telegraf.conf and list the parsed field names, for example ENERGY_ApparentPower or the suffix glob *_ReactivePower. The filter runs at the end of the input, so it shapes new points only and never rewrites points already stored in InfluxDB. Drop a minority of fields with fielddrop, keep a minority with fieldinclude, and filter tags with tagdrop.

On this page
  1. Where the filter sits in the data path
  2. Match the parsed field names, not the payload leaves
  3. Add fielddrop to the existing input block
  4. Prove the drop before the restart
  5. What the drop list does to the point
  6. fielddrop, fieldinclude or tagdrop
  7. Verify the write after the restart
  8. The decision for each case
  9. Questions asked

Yes. Add a fielddrop list to the [[inputs.mqtt_consumer]] block in telegraf.conf and Telegraf discards the matching fields before the output writes anything to InfluxDB. Match the parsed, flattened names the JSON parser produces — ENERGY_ApparentPower for an ApparentPower member nested under ENERGY — not the leaf names visible in the payload. The filter runs at the end of the input, so it shapes new points only: it never rewrites points already stored, never edits the MQTT message and never touches another input block. Drop a minority of fields with fielddrop, keep a minority with fieldinclude, and filter tags with tagdrop. The steps below take a working MQTT-to-InfluxDB pipeline from eleven parsed fields per point to the seven worth trending, with a test that proves the drop before the service restarts.

Where the filter sits in the data path #

Trace the hop order once, because the filter has exactly one place in it. The device publishes a JSON payload to a topic on the broker; the broker delivers it to the mqtt_consumer subscription; the JSON parser turns each message into one point in the mqtt_consumer measurement; fielddrop removes matching field keys at the very end of that input; the output plugin writes what remains to InfluxDB. Because the pipeline arrives already logging, every hop on both sides of the filter is proven, and the only change under discussion sits at the parser boundary.

Two consequences follow from that position. The parser still reads every member of every payload, so fielddrop trims the write, not the parsing work. And the option belongs to one input block: a fielddrop list under another plugin does not affect these points, and this list does not affect any other input. If the pipeline itself still has to be built, follow the walkthrough that stores MQTT messages in InfluxDB with Telegraf first, then come back and trim it.

Match the parsed field names, not the payload leaves #

The JSON parser flattens nesting with an underscore: a member one level below a parent key parses under the parent name joined to the leaf name, and the JSON input format documentation references a nested value by its flattened name. An ApparentPower member under ENERGY therefore parses as the field ENERGY_ApparentPower, and the drop list has to spell the whole key.

Inspect one parsed point instead of guessing. The test command in a later section prints each point after filtering, which is the cheapest way to confirm spelling, capitalization and underscore placement before any glob is trusted. On an energy payload whose readings sit under ENERGY, the names and decisions are:

Payload member Parsed field key Drop decision
ENERGY.ApparentPower ENERGY_ApparentPower exact entry in the list
ENERGY.ReactivePower ENERGY_ReactivePower matched by *_ReactivePower
ENERGY.Factor ENERGY_Factor matched by *_Factor
ENERGY.Period ENERGY_Period matched by *_Period
ENERGY.Total, ENERGY.Voltage, ENERGY.Current ENERGY_Total, ENERGY_Voltage, ENERGY_Current no pattern matches, kept

One quoting trap ends more of these edits than any real pattern mismatch: inside a quoted TOML value a leading space is a literal character, not indentation. A pattern written as " _Factor" searches for a key that begins with a space and can never match ENERGY_Factor. Write every pattern so its first character sits directly after the opening quote.

Add fielddrop to the existing input block #

Edit the block that already logs, and edit nothing else in it. A second [[inputs.mqtt_consumer]] block pointed at the same broker and topics would subscribe twice and write every reading two times. The fielddrop line goes inside that block, below the settings you already run; the example shows a device publishing under a topic such as tele/fan/SENSOR:

[[inputs.mqtt_consumer]]
  ## Keep the servers, topics, credentials and parser settings you already run.
  servers = ["tcp://localhost:1883"]
  topics = ["tele/fan/SENSOR"]
  data_format = "json"

  ## Field drop list: flattened keys and suffix globs.
  fielddrop = ["ENERGY_ApparentPower", "*_Factor", "*_Period", "*_ReactivePower"]

The servers, topics, credentials and parser options stay exactly as the deployment runs them. Only the fielddrop line is new, which keeps one variable in the change: if behavior shifts after the restart, the list is the only candidate cause.

Prove the drop before the restart #

Run the input test with the same file Telegraf will load:

telegraf --config telegraf.conf --test --input-filter mqtt_consumer

The test gathers one batch of points, applies fielddrop at the end of the input and prints each point without writing anything to InfluxDB. Work the results in order:

  1. If the command stops with a configuration error, fix the file first: a telegraf.conf that fails to load keeps the previous configuration running, and no restart applies a list that never loaded.
  2. Read the printed point key by key. Every targeted field must already be absent, and kept fields such as ENERGY_Total and ENERGY_Voltage must still appear with plausible values.
  3. If a targeted key still prints, its pattern missed: compare it against the printed key for spelling, capitalization and stray whitespace, correct the list, and run the test again. Nothing has been stored at this stage, so a wrong guess costs nothing but a second run.
  4. Restart the service the way the deployment normally reloads Telegraf, through its service manager on the host or a container restart. A running process reads telegraf.conf at start only, so the new list does nothing until that restart.

What the drop list does to the point #

Take the example payload as the parser sees it. The Time member is consumed as the point timestamp, and the ENERGY object flattens to eleven fields: ENERGY_TotalStartTime, ENERGY_Total, ENERGY_Yesterday, ENERGY_Today, ENERGY_Period, ENERGY_Power, ENERGY_ApparentPower, ENERGY_ReactivePower, ENERGY_Factor, ENERGY_Voltage and ENERGY_Current. The list above matches four of them, one exact key and three suffix globs, so the arithmetic per point is 11 parsed fields − 4 matched patterns = 7 fields written, which is 4 ÷ 11 ≈ 36 percent fewer field values per point. The seven survivors are ENERGY_TotalStartTime, ENERGY_Total, ENERGY_Yesterday, ENERGY_Today, ENERGY_Power, ENERGY_Voltage and ENERGY_Current; the string member TotalStartTime survives because no glob matches it, and it only leaves if its exact key joins the list. Every point still lands in the mqtt_consumer measurement with its timestamp; only its width changes.

fielddrop, fieldinclude or tagdrop #

Setting What it filters Choose it when
fielddrop Field keys, removed from the points this input emits The drop list is the shorter list
fieldinclude Field keys, everything unmatched removed The keep list is the shorter list, for example three trended values in a wide payload
tagdrop Tag keys The excess sits in tags, never in measurement values

The three are not interchangeable, and the deciding count is trivial: write both lists against the parsed keys and run the shorter one. Readings such as power, voltage and current parse as fields, so the metering case above is a fielddrop case. A tag filter never removes a field carrying the same text, and fielddrop never removes a tag; when a pattern seems dead, the test output shows which key space the name actually lives in.

Verify the write after the restart #

Run the checks in order. First, the input is alive: the Telegraf log shows a fresh subscription to the topic with no parse errors. If the bucket goes quiet instead, the fault sits upstream of the filter, and the checklist for a Telegraf MQTT consumer that connects but writes no data to InfluxDB walks that path step by step.

Second, the write is trimmed: query points newer than the restart, confirm each kept field carries a value you can read on the device, and confirm the targeted keys are absent from the new points. Third, the history is untouched: points recorded before the change still carry the dropped fields, because input filtering never rewrites stored data. An old trend continues to show the removed values, and that is expected behavior, not a failed edit.

Change one thing at a time while you are in here. If silence follows the first restart, revert the fielddrop line before suspecting the broker: a malformed list can keep the whole input block from loading, and a block that fails to load looks exactly like a lost connection.

The decision for each case #

  • Working pipeline, a short list of unwanted field keys — the case this article resolves: fielddrop in the mqtt_consumer block, patterns spelled exactly as the parsed keys spell them, proven with the test command, then one restart.
  • A short list of wanted keys in a wide payload: fieldinclude with those keys, so anything the publisher starts sending later stays out by default. Build the keep list from what the dashboard trends; the InfluxDB tag and field schema for a PLC historian dashboard is the planning step that fixes that list before the config changes.
  • Excess values in tags rather than fields: tagdrop on the tag keys, remembering that a tag filter cannot remove a field.
  • Values already stored before the change: no filter reaches them. Old points keep every field they arrived with until the bucket's retention policy expires them or a documented delete scoped to a measurement and time range runs — prove such a delete on a scratch bucket before pointing it at production history.

Questions asked

Does fielddrop reduce the traffic between the device, the broker and Telegraf?

No. The filter runs on the Telegraf host after parsing, so the device still publishes the full payload and the broker still delivers every message to the subscription. To cut the traffic itself, lower the publisher's reporting rate or filter on a broker-side bridge. fielddrop only trims the points Telegraf emits toward InfluxDB.

Can fielddrop remove the payload timestamp as well?

No. When json_time_key names a member such as Time, the parser consumes that member as the point timestamp instead of creating a field from it, so no fielddrop pattern can match it. If the time text must also be stored as an ordinary string field, that is a parser string-field setting, not a filtering change.

Does dropping fields reduce series cardinality in the InfluxDB bucket?

No. Series cardinality comes from the unique combinations of tags on the points, not from the fields. fielddrop narrows the width of each point the input emits while the series count stays exactly as it was. Only a change to tags, the measurement name or the topic-derived identifiers moves cardinality.

Do suffix globs keep working when a second device publishes the same readings with different nesting?

Yes, as long as the parsed keys keep the same tail. The glob *_ReactivePower matches any field key ending in _ReactivePower, however long the flattened prefix in front of it runs. What breaks the match is a different leaf name after parsing, so run the same test command against each new publisher and extend the list from the keys that actually print.

Keep reading

  • Troubleshooting

    Telegraf MQTT consumer connects but writes no data to InfluxDB

    An empty 0 / 10000 output buffer with both the MQTT and InfluxDB connections up means no metrics are reaching Telegraf's parser. Prove live delivery with a second subscriber on a different client ID before touching parser or buffer settings: the two startup connections are independent events, and with qos = 0 and a non-persistent session, traffic published while Telegraf is offline is unrecoverable.

    Edge computing and IIoT

  • How-to

    Turn an MQTT topic into InfluxDB fields with Telegraf

    Telegraf reads /temperature/boiler/room3 with payload 21.7 as a metric carrying a topic tag and a value field. A five-processor chain (regex, converter, regex, regex, pivot) renames the measurement, builds location=room3 and pivots the middle segment into the field key; one Starlark processor does the same in eight lines. Either way the output line is temperature,location=room3 boiler=21.7 with the original timestamp.

    Edge computing and IIoT

  • Troubleshooting

    Why Telegraf MQTT consumer writes nothing to InfluxDB Cloud

    Nothing reaches the bucket because three faults act at once: the broker URL on port 8883 has no ssl:// scheme, so the consumer never subscribes; the published payload is not valid line protocol, so the parser rejects it; and the token variable expands empty inside the service, so writes return HTTP 401. Fix all three, delete tag_keys for the influx data format, and give each Telegraf instance its own client_id.

    Edge computing and IIoT