Search code examples
mqttinfluxdbtelegraf

influxdb/telegraf mqtt consumer parsing, value is not named correctly


I have this telegraf configuration

[[inputs.mqtt_consumer]]
  servers = ["tcp://test_mosquitto_1:1883"]
  # data_format = "influx"
  username = "rasp"
  password = "XXXXY"
  topics = [
  "battery/#"
  ]
  data_format = "value"
  data_type = "float" # required

[[inputs.mqtt_consumer.topic_parsing]]
    data_format = "value"
    data_type = "float"
    topic = "battery/+/+/temperature"
    measurement = "measurement/_/_/_"
    tags = "_/site/device_name/_"
    fields = "_/_/_/temperature"
[[inputs.mqtt_consumer.topic_parsing]]
    data_format = "value"
    data_type = "int"
    topic = "battery/+/+/voltage"
    measurement = "measurement/_/_/_"
    tags = "_/site/device_name/_"
    fields = "_/_/_/voltage"

Im pushing topics over mqtt to "battery/hamburg/devicename2312/temperature" and the payload is the value for Temperatur. The location hamburg should be tagged (site) and the device name should be tagged. Everything works except that the value is not named correctly; see influxdb log:

battery,device_name=101A14420210010,host=5cc0065d3907,site=hamburg,topic=battery/hamburg/101A14420210010/temperature value=23.35001,temperature="temperature" 1653991738177023790
telegraf_1   | 

I now have "value" in my influx database and "temperature" (as a string) with value "temperature". I just want Telegraf to save the value to "temperature"

Here you see the mqtt explorer view:

mqtt explorer view


Solution

  • After hours of googling and reading it works now. Here is the changed part of the config:

    [[inputs.mqtt_consumer.topic_parsing]]
        data_format = "value"
        data_type = "float"
        topic = "battery/+/+/temperature"
        measurement = "measurement/_/_/_"
        tags = "_/site/device_name/field"
        fields = "_/_/_/temperature"
        [[processors.pivot]]
        tag_key = "field"
        value_key = "value"
    

    More information here: https://www.influxdata.com/blog/pivot-mqtt-plugin/