MQTT to InfluxDB with Telegraf

Telegraf is the “influx” way of fetching data from various data sources into multiple destinations, like InfluxDB

Among many others, it can subscribe to MQTT topics and import the data into InfluxDB

The overal documentation of the telegraf configuration is rather poor, lacing examples to illustrate the concepts.

The relevant part of the telegraf config is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[[inputs.mqtt_consumer]]
  servers = ["tcp://<hostname or IP>:1883"]
  ## Topics that will be subscribed to.
  topics = [
    "my/topic/#",
  ]
  ## QoS policy for messages
  ##   0 = at most once
  qos = 0
  ## Connection timeout for initial connection in seconds
  connection_timeout = "30s"
  ## Environment variables can be acessed using the ${variable} syntax
  client_id = "${MQTT_CLIENT_ID}"
  ## Username and password to connect MQTT server.
  username = "${MQTT_USER}"
  password = "${MQTT_PASSWORD}"

  ## Optional TLS Config
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  ## Use TLS but skip chain & host verification
  # insecure_skip_verify = false

  ## Data format to consume.
  ## Each data format has its own unique set of configuration options, read
  ## more about them here:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  data_format="json_v2"
  ## Enable extracting tag values from MQTT topics
  ## _ denotes an ignored entry in the topic path
  ## in my example the topic would by "track/<some property>/<devicename>
  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "track/+/+"
    measurement = "_/measurement/_"
    tags = "_/_/device"
  [[inputs.mqtt_consumer.json_v2]]
    [[inputs.mqtt_consumer.json_v2.object]]
        path = "[@this]"
        timestamp_key = "epoch"
        timestamp_format = "unix"
    ## Rename : fields in JSON = fields in InfluxDB
    [inputs.mqtt_consumer.json_v2.object.renames]
        count_of_elements = "count"
        aux_data_cpu_load = "load"

When using simple data, the data_format entry should be e.g.

1
2
  data_format = "value"
  data_type = "float"

The json_v2 format is important in case multiple JSON objects are transmitted as an array inside a single MQTT message. The json data format is not able to handle arrays of objects.

The whole syntax of the telegraf config file is rather confusing. It is based on ini format, but with a sub-hierarchy delimited by 2-brackets syntax, and further hierarchical information within the (double) brackets.