How-to
Parse MQTT XML in Telegraf with inputs.mqtt_consumer.xml
Short answer
Yes. The MQTT consumer decodes XML with the xml input data format: set data_format to xml inside the inputs.mqtt_consumer table and put the XPath field and timestamp mappings under inputs.mqtt_consumer.xml. The inputs.file example in the documentation is only one host for the same parser, and a dial tcp connection refused error comes from the broker connection, before any parsing.

On this page
- The XML parser rides on the MQTT consumer
- Clear the transport before the parser
- Read the payload as elements, not flattened text
- Prove the XPath with a local file
- Move the tables to the MQTT consumer
- Worked example: the value 16 point stamped 2021-08-03T13:38:42.656Z
- Where to look when values still do not arrive
- Questions asked
Yes. Telegraf's MQTT consumer decodes XML payloads, and for an OPC UA style DataValue message no custom plugin is needed. Set data_format = "xml" inside the [[inputs.mqtt_consumer]] table, then put the timestamp mapping and the XPath field paths under [[inputs.mqtt_consumer.xml]]. The parser configuration belongs to the consumer, not to inputs.file; the documentation's file example is just one of several inputs that host the same data format. One warning before the mapping: dial tcp 127.0.0.1:1883: connect: connection refused is a broker-side TCP failure, and nothing reaches the parser until you clear it.
The XML parser rides on the MQTT consumer #
The XML data format is a parser, not a file reader. Every input that supports input data formats can attach it, and inputs.mqtt_consumer is one of them: data_format = "xml" inside the consumer's own table selects the parser, and the nested [[inputs.mqtt_consumer.xml]] table configures it. The documentation demonstrates all of this on inputs.file, which makes the parser look file-bound; it is not. Use the file input as a bench test, then move the same tables to the consumer. The payload stays XML end to end, and nothing converts it to JSON.
Clear the transport before the parser #
The startup failure dial tcp 127.0.0.1:1883: connect: connection refused means the destination rejected the TCP connect. Telegraf never reached the subscription, so the XML settings never ran. Port 1883 is plain MQTT over TCP; read the servers line of the configuration the service actually loads and compare it with the address printed in the error.
127.0.0.1 is the loopback of whatever environment Telegraf runs in: inside a container it is the container's own loopback, the fault behind Telegraf MQTT connecting to localhost in Docker, and a broker container refusing the listener is the Mosquitto connection refused case for Telegraf in Docker. For a TLS broker on port 8883 reached with ssl://, point tls_ca, tls_cert and tls_key at files the service account can read, and keep certificate-chain and hostname verification on: take insecure_skip_verify = true out of an operating configuration and fix the trust or hostname problem instead.
Read the payload as elements, not flattened text #
The string first seen at the consumer, a number, a status and two timestamps run together, is the payload with its tags stripped. XPath selects by element structure, so capture the raw message before choosing any path. This publisher sends:
<DataValue xmlns="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Value>
<Value>
<UInt16>16</UInt16>
</Value>
</Value>
<StatusCode><Code>0</Code></StatusCode>
<SourceTimestamp>0001-01-01T00:00:00</SourceTimestamp>
<SourcePicoseconds>0</SourcePicoseconds>
<ServerTimestamp>2021-08-03T13:38:42.656Z</ServerTimestamp>
<ServerPicoseconds>0</ServerPicoseconds>
</DataValue>
The two nested Value elements are legal XML, and the path needs both segments to reach the UInt16 leaf. The document declares a default namespace, and the demonstrated parser accepted unprefixed XPath expressions against this payload; keep the whole document when you test, and rerun the test if the publisher changes its namespace or element names. SourceTimestamp carries 0001-01-01T00:00:00, the placeholder this publisher writes when the source has no time, so the mapping reads ServerTimestamp, and neither picoseconds element is used.
Prove the XPath with a local file #
Before touching the live subscription, run the parser on a captured payload with the file input and a debug file output:
[[inputs.file]]
name_override = "xml"
files = ["xmlparser.xml"]
data_format = "xml"
[[inputs.file.xml]]
timestamp = "/DataValue/ServerTimestamp"
timestamp_format = "2006-01-02T15:04:05.999Z"
[inputs.file.xml.fields_int]
value = "/DataValue/Value/Value/UInt16"
statuscode = "/DataValue/StatusCode/Code"
[[outputs.file]]
files = ["xmlparser.out"]
influx_sort_fields = true
The rig writes one line per parse with both integer fields and a nanosecond point time, which the worked example below verifies digit for digit. Passing here proves the paths and the layout; only the table names change when the mapping moves to the consumer.
Move the tables to the MQTT consumer #
- Open the configuration the Telegraf service actually loads and find the active
[[inputs.mqtt_consumer]]table. - Leave its
servers,topicsand security lines untouched, and adddata_format = "xml"inside that table, above the nested XML table. - Add
[[inputs.mqtt_consumer.xml]]with the timestamp line and the fractional-second layout; keep.999in the layout because the payload carries a fractional second. - Add
[inputs.mqtt_consumer.xml.fields_int]with the two leaf paths. - Delete every expression copied from the documentation's sensor example; none of them match this document.
- Leave the output block alone, reload the service, and confirm one metric per published message.
The consumer part of the configuration, with the broker lines left as you have them:
[[inputs.mqtt_consumer]]
# your servers, topics and TLS settings stay here
data_format = "xml"
[[inputs.mqtt_consumer.xml]]
timestamp = "/DataValue/ServerTimestamp"
timestamp_format = "2006-01-02T15:04:05.999Z"
[inputs.mqtt_consumer.xml.fields_int]
value = "/DataValue/Value/Value/UInt16"
statuscode = "/DataValue/StatusCode/Code"
The expressions to delete in step 5 belong to the sensor document:
/Bus/child::Sensor
substring-after(Sensor/@name, ' ')
Variable/@consumers
string('example')
The measurement name follows the input's naming options: the xml measurement in the bench rig comes from name_override = "xml" on the file input, so set on the consumer the name your dashboard queries.
Worked example: the value 16 point stamped 2021-08-03T13:38:42.656Z #
Inputs, each from the captured MQTT payload or the configuration above:
- Field value:
16, a unitless count insideUInt16, from the payload. - Status code:
0, unitless, fromStatusCode/Codein the same payload. - Timestamp string:
2021-08-03T13:38:42.656Z, UTC, fromServerTimestampin the same payload. - Reference layout:
2006-01-02T15:04:05.999Z, from thetimestamp_formatline.
The rule: the parser converts the selected timestamp string into a nanosecond count since 1970-01-01T00:00:00Z, the only time form InfluxDB stores, and writes both fields as line-protocol integers.
The arithmetic:
- Days from the epoch to the date: 51 years x 365 days = 18,615 days, plus 13 leap days = 18,628 days to 2021-01-01, plus 214 days = 18,842 days to 2021-08-03.
- 18,842 days x 86,400 s per day = 1,627,948,800 s.
- Time of day: 13 x 3,600 s + 38 x 60 s + 42 s = 49,122 s.
- 1,627,948,800 s + 49,122 s + 0.656 s = 1,627,997,922.656 s since the epoch.
- 1,627,997,922.656 s x 1,000,000,000 ns per s = 1,627,997,922,656,000,000 ns.
Result: the rig's output line xml statuscode=0i,value=16i 1627997922656000000 carries exactly that point time, so the layout and the arithmetic agree.
Decision: the point lands at 13:38:42.656 UTC on 2021-08-03, which is event time, not receive time. A dashboard window or retention rule that ends before that instant hides the point even though the write succeeded, so keep the ServerTimestamp mapping while the publisher's clock is the time you trust. To carry both times on one metric, see keeping both arrival time and payload timestamp in Telegraf.
Where to look when values still do not arrive #
Trace in order, transport, subscription, parser, output, query, and stop at the first stage that fails:
| Symptom | Stage that failed | Action |
|---|---|---|
connect: connection refused at startup |
TCP connect to the broker | Fix the listener, address or port first; the parser never ran |
| Connected, no metrics, no parse errors | Subscription | Compare topics with the publisher's exact string; the demonstrated subscription /TestAlex1 keeps its leading slash as part of the topic name |
| Connected, parse errors in the log | XML parser | Capture the raw payload, then correct the paths or the layout |
| Metrics in the database, stale value on screen | Query window | The point carries event time; widen the window or change the time source |
| Consumer healthy, nothing written | Output | Work the output-side checks below |
A consumer that connects and parses yet still writes nothing has failed on the output side; why the MQTT consumer connects but writes no data to InfluxDB sorts that stage. The database side has its own checklist in troubleshooting an MQTT consumer that writes nothing to InfluxDB.
Questions asked
Does the MQTT consumer need a custom plugin to parse XML?
No. The XML parser is a data format that any supporting input can host, so the consumer decodes the payload with the same tables the file input uses in the documentation. A custom plugin becomes worth writing only when the payload needs a transformation the XPath field, tag and timestamp mappings cannot express, and this DataValue structure needs none.
Why does the value arrive as 16i with an i on the end?
The fields_int section stores whole numbers, and i is the integer marker in InfluxDB line protocol, so 16i is an integer field, not a string. Keep counts and status codes in fields_int: selecting the same data through one of the parser's other field sections changes the field type, and a type change on an existing field is rejected on write.
The publisher sometimes puts loose text such as 9999 inside the outer Value element. Does that break the mapping?
No. Mixed content is legal XML, and the leaf path /DataValue/Value/Value/UInt16 selects only the 16 in the inner element. Selecting the outer Value element instead can concatenate the nested element's text with the loose text, which is why the mapping targets the leaf and not the wrapper.
Why map ServerTimestamp instead of SourceTimestamp?
Because SourceTimestamp here holds 0001-01-01T00:00:00, the value this publisher writes when the source has no time. A point mapped to it lands at year one, outside every dashboard window and retention rule, while ServerTimestamp carries the event time the server recorded, so the mapping follows the server.
Can the metric carry Telegraf's receive time instead of the payload time?
Yes. Delete the timestamp and timestamp_format lines and the parser stamps each metric with the moment Telegraf received the message. Keep the payload mapping while the publisher's clock is trustworthy and you want buffering or retransmission delays kept out of the event time; drop it when receive time is what the trend must show.


