IMD SolutionComputing LibraryShop main store

Troubleshooting

Why Telegraf drops a hex string field from an MQTT sensor payload

Edge computing and IIoT Published 7 min read By IMD Solution engineering team

Short answer

The classic JSON parser in Telegraf's MQTT consumer turns numeric properties into fields and drops string values, so the hex pdin_data never reaches InfluxDB. List it in json_string_fields to keep it as a string field, then decode it with processors.starlark or processors.execd into numeric temperature and humidity fields before the write; a float cast cannot decode hex bytes.

On this page
  1. The parser keeps numbers and drops strings
  2. Prove where the string is lost
  3. Keep the string in the metric
  4. Decode the bytes with a processor
  5. Worked example: decoding the sample 01B9FF0000F4FF00
  6. Write raw and decoded values under distinct names
  7. Questions asked

Your field is not stuck in the broker, and InfluxDB is not rejecting it. The JSON parser in Telegraf's MQTT consumer turns JSON numbers into metric fields and drops string values that no one listed, so counter_code, counter_data and pdin_code arrive while the hex pdin_data is discarded before any processor or output sees it. The repair is two stages, both inside Telegraf and both before the write: list the property in json_string_fields so the hex text survives parsing, then decode it with processors.starlark or processors.execd into numeric temperature and humidity fields. A float cast cannot do the job, and the cut you sketched, B9FF for temperature and F5FF for humidity, does not match your own sample. Both points are worked through below.

The parser keeps numbers and drops strings #

A Telegraf field is typed: float, integer, boolean or string. The classic JSON parser behind the mqtt consumer only builds fields from JSON values it can type as numbers. Your counter values are JSON numbers, so they become fields. pdin_data is a JSON string, and unlisted strings are skipped. Nothing is logged, because nothing failed: dropping unlisted string values is the parser's documented behavior, not a fault in your broker or your message.

That placement decides where the fix can live. Processors run after the input's parser, so processors.starlark and processors.execd never receive a property the parser has already discarded; a decoder added on its own changes nothing. InfluxDB does accept string fields, and this string simply never reached the database. The mirror-image fault, numeric values landing in a separate string measurement, runs through a different parser path and is covered in why Telegraf writes MQTT float values into the string measurement.

Prove where the string is lost #

Three checks pin the loss to the parser before you change any configuration:

  • Capture one complete message with an authorized MQTT subscriber on the same topic: the topic, the full JSON, the raw pdin_data text and the receipt time. Keep it; it is the test input for every step after this one.
  • Confirm the numeric siblings are fresh, not stale rows. If the measurement receives nothing at all, the fault sits upstream of parsing, and Telegraf MQTT consumer connects but writes no data to InfluxDB is the trail to follow first.
  • Run Telegraf once against the captured message with only a file output attached and read the emitted line. Counter fields present and pdin_data absent on that line is the proof: the drop happens at parse time, inside Telegraf, before any output plugin.

Keep the string in the metric #

Name the property in the classic parser's string-field list so it survives parsing:

[[inputs.mqtt_consumer]]
  # broker, topic and JSON settings you already have stay unchanged
  json_string_fields = ["pdin_data"]

json_string_fields tells the JSON parser which properties to keep as string fields instead of dropping. A Telegraf tracker issue records the key being refused inside mqtt_consumer on an old release line; if your installed build rejects it, update the agent rather than working around the parser. The documented stopgap on such builds is tag_keys = ["pdin_data"], which preserves the text as a tag instead of a field: acceptable for a bench test, wrong as a permanent home, because every distinct payload becomes a new tag value and a new series.

Then re-run the file-output test. The passing check is the complete text on the metric, character for character, leading zero intact.

Decode the bytes with a processor #

With the string in the pipeline, the conversion is ordinary byte decoding. Telegraf has no stock converter for hex-encoded values (the need has its own entry in the plugin tracker), so the decode belongs in one of two processors:

processor where the code runs choose it when
processors.starlark inside the Telegraf process the layout is fixed and you want one self-contained config file
processors.execd in an external program Telegraf starts and feeds the decode needs a library or code you already maintain outside Telegraf

The decoder's job, in order:

  • Validate. Require exactly 16 hexadecimal characters, 8 bytes, and nothing else. A value that fails writes no numeric fields for that sample and logs the raw text; never substitute a zero, which downstream reads as a valid measurement.
  • Slice per the sensor's documented process-data layout: offsets, field widths, byte order, signed representation, scale and unit. The layout decides these, not the sample; the worked example below shows how far apart two defensible readings of one payload sit.
  • Assemble each integer and apply the documented conversion: engineering value = counts × scale + offset.
  • Emit the results as separate numeric fields and keep the raw string alongside them through commissioning.

The validation step, as illustrative pseudocode with this article's own names, not any plugin's API:

raw_text   : string  = "01B9FF0000F4FF00"   # the pdin_data field, example capture
byte_count : integer = length(raw_text) / 2  # 8 for this capture
hex_only   : bool    = true                   # test: every symbol is 0-9 or a-f
# the gate passes when byte_count == 8 and hex_only == true

Do not route any of this through a float cast. A float conversion parses decimal text: on your sample it stops at the first letter and fails, and a parser lenient enough to keep going hands back the leading digits as 1, a wrong number rather than an error. The B and the F in pdin_data are exactly what such a parser cannot take. Hex reaches a number only through bytes to integers, in the documented order.

Worked example: decoding the sample 01B9FF0000F4FF00 #

Inputs, with where each comes from:

  • pdin_data = 01B9FF0000F4FF00: the captured sensor message.
  • The layout rule you stated: the first 4 symbols carry the temperature.
  • The cut you proposed: temperature B9FF, humidity F5FF.
  • Byte order, signedness, scale and offset: in none of the material above. They live in the sensor's process-data layout, so this example decodes counts, not degrees or percent.

The rule in words: two hexadecimal symbols make one byte; slice the symbols the layout names, assemble them into an integer in the documented byte order, then engineering value = counts × scale + offset.

The arithmetic:

  1. Length: 16 symbols ÷ 2 symbols per byte = 8 bytes, so the sample passes the validity gate. Its bytes are 01 B9 FF 00 00 F4 FF 00.
  2. Temperature by your own rule, symbols 1-4 = 01B9. Big-endian: 0x01B9 = 1 × 256 + 185 = 441 counts. Signed or unsigned makes no difference, since 441 sits far below the 32768 two's-complement boundary. Read little-endian instead, the same bytes give 0xB901 = 185 × 256 + 1 = 47361 counts, 107 times as large, which is why byte order comes from the layout and not from a guess.
  3. Your proposed B9FF is symbols 3-6, straddling the second and third bytes; read big-endian signed it equals 47615 − 65536 = −17921 counts. The stated rule and the proposed cut disagree, and on your own sample the rule wins: 01B9.
  4. Humidity: F5FF occurs nowhere in the sample. Symbols 11-14 read F4FF: big-endian unsigned 0xF4FF = 244 × 256 + 255 = 62719 counts; as signed 16-bit, 62719 − 65536 = −2817 counts. Two defensible readings of the same four symbols sit 65536 counts apart, and only the layout's signedness and scale pick which one becomes a humidity value.

Result: this sample carries 441 temperature counts big-endian in symbols 1-4; the humidity bytes are F4FF at symbols 11-14, waiting on two documented constants to become %RH.

Decision: implement the decode behind the 16-symbol gate (any other length writes no numeric fields for that sample), take temperature from symbols 1-4, and discard the B9FF and F5FF cut. Store the counts under raw-count field names until the sensor layout supplies scale and offset; then temperature = counts × scale + offset, verified once against the sensor's own indicated reading before the counts field is retired.

Write raw and decoded values under distinct names #

Two naming rules keep the repair clean:

  • Give the decoded numbers their own field names, for example temperature and humidity, and leave pdin_data as the string field it now is. A field name keeps the type of its first write; a later write of a different type is rejected as a type conflict and that sample is lost. Retire the counts fields by stopping their writes, never by changing their type.
  • Verify at the database, not on a dashboard. Watch Telegraf's output log for the write response, then query the measurement directly with the real tag set and time range and confirm three things in one row: the raw string, the numeric decoded fields and the sample timestamp.

The same field-type discipline drops data in other Telegraf inputs too; the pattern on another input is shown in a walkthrough of Telegraf CSV input type conflicts and dropped values.

Questions asked

Why must pdin_data stay out of tag_keys once the repair is done?

A tag value is part of a series identity, and the raw payload changes with every sample, so each message creates a new series. The series count climbs without bound, the index grows with it, and queries slow down: the classic high-cardinality failure. The tag route is acceptable for a bench test and nothing longer; move the text back to a string field as soon as the parser keeps it.

Do the sensor or the broker need any change for this fix?

No. The broker forwards the payload untouched, and the sensor's JSON is well formed; the numeric siblings prove that. Everything changes inside Telegraf: one parser option to keep the string and one processor to decode it. The only thing needed from the sensor side is its documentation, the process-data layout with offsets, byte order, scale and offset constants. Nothing on the device is reconfigured.

How is the decoder tested before it writes to the live database?

Feed the captured message back through a test setup: publish it once to a test topic, run a Telegraf instance with the same input and processor but only a file output, and read the emitted line. The raw string must match the capture character for character, and the decoded counts must equal the hand arithmetic, 441 for the sample's temperature. Only then point the real output at InfluxDB and confirm one accepted write.

Could the decoding happen at the sensor or gateway instead of in Telegraf?

Yes, when the publisher can be reconfigured: decoding at the source and publishing numeric temperature and humidity fields removes the processor entirely and is the cleanest long-term shape. With fixed device firmware and a gateway configuration you cannot touch, Telegraf is the last component that sees the payload before the database, and the processor there is the right place for the decode.

Keep reading

  • Troubleshooting

    Decoding hex float32 MQTT payloads in Telegraf

    The JSON parser stores the vs field as text because that is what the config asks for; no parser option decodes bytes. Strip the one-byte device ID, decode the remaining eight hex characters as a little-endian DCBA float32, and run the conversion at ingestion in a Starlark or execd processor so InfluxDB receives a real number.

    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

  • How-to

    Filter MQTT fields in Telegraf before they reach InfluxDB

    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.

    Edge computing and IIoT

Why a historian report template lacks identification fields