Help needed: Using telegraf to parse mqtt with JSON payload

universal-dilettant
Sun, 02 Apr 2023 15:25:09 +0000
Telegraf

Hi,
I’m new to telegraf. That being said,
I’m trying to parse some mqtt topics with telegraf, with the (later) goal to import them into influxDB. The mqtt topics are like this:

mqtt_consumer,host=xxxx,topic=mtr/hzg/temp value="24.8" 1680447906430751221
mqtt_consumer,host=xxxx,topic=mtr/pwr/evu/SENSOR value="{\"Time\":\"2023-04-02T17:05:07\",\"EHZ541\":{\"cnt_imp\":3648.8,\"cnt_exp\":213492.8,\"mtr_cur\":-554}}" 1680447908136350043

They are either simple topic / value pairs (like the first one) or contain some JSON as payload.
The following telegraf.conf created above snippet.

[global_tags]

[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = "0s"
  hostname = ""
  omit_hostname = false

[[outputs.file]]
  files = ["stdout"]

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = [
    "mtr/hzg/#",
    "mtr/pwr/evu/SENSOR",
  ]
  client_id = "telegraf"
  username = "UUUUUUU"
  password = "PPPPPPPP"
  data_format = "value"
  data_type = "string"

By replacing data_type with float, all good for the topic “mtr/hzg/#”.

Next, trying to parse JSON with the addition of the following config line (just appended to above config file)

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = [
    "mtr/pwr/evu/SENSOR",
  ]
  client_id = "telegraf"
  username = "UUUUUUUU"
  password = "PPPPPPPPP"

  data_format = "json_v2"

  [[inputs.mqtt_consumer.json_v2]]
    [[inputs.mqtt_consumer.json_v2.object]]
      tags = [ "cnt_imp" ]
    [[inputs.mqtt_consumer.json_v2.fields]]
      cnt_imp = "float"
      cnt_exp = "float"
      mtr_cur = "float"

It produces the following result

2023-04-02T15:17:30Z I! Loading config file: /etc/telegraf/telegraf.conf
2023-04-02T15:17:30Z E! error loading config file /etc/telegraf/telegraf.conf: plugin inputs.mqtt_consumer: line 28: configuration specified the fields ["fields"], but they weren't used

What am I missing??

Thanks
Joe

11 posts - 5 participants