Parsing an array from an MQTT topic with json_v2

I have a mqtt topic that contains an array as “[aa.a,bb.b]” and seems that my configuration is working more or less as the desired values arrive all the way to the influxdb. However testing the configuration does give errors and outputs twice the line protocol line related to this consumer.

Input from MQTT:

go-eCharger/201397/nrg = [233.74,2.48,3.41,1.24,0,0,0,0,0,0,0,0,0,0,0,0]
go-eCharger/201397/tma = [35.5,30.5]

Currently only setting up parsing for the tma variable. Once that works will do nrg also.

Configuration for telegraf:

[[inputs.mqtt_consumer]]
  servers = ["tcp://localhost:1883"]
  topics = [
    "go-eCharger/+/tma", # temperature sensors
  ]

  data_format = "json_v2"
  topic_tag = ""

  connection_timeout = "30s"
  qos = 0
  username = "telegraf"
  password = "Password"

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "go-eCharger/+/+"
    measurement = "measurement/_/_"
    tags = "_/chargerid/_"

  [[inputs.mqtt_consumer.json_v2]]
    [[inputs.mqtt_consumer.json_v2.object]]
      path = "@this"
      optional = true
      tags = [""]
      fields = [""]
      [[inputs.mqtt_consumer.json_v2.field]]
        path = "0"
        rename = "t1"
        type = "float"
      [[inputs.mqtt_consumer.json_v2.field]]
        path = "1"
        rename = "t2"
        type = "float"

Output from test:


2022-09-23T08:05:41Z D! [serializers.influx] could not serialize field "": invalid field key; discarding field
> go-eCharger,chargerid=201397 t1=26,t2=21 1663920341263071772
2022-09-23T08:05:41Z D! [serializers.influx] could not serialize field "": invalid field key; discarding field
> go-eCharger,chargerid=201397 t1=26,t2=21 1663920341263071772

What should I change, to get the serializer error away? Or should I just ignore the error, also the line protocol is sent out for every field match. the serializer errors appear also in /var/log/telegraf.log and would fill up the local storage quite soon unless i can figure out what to change in the config to be valid.

After debugging the serializing issue in my configuration, I found out that it was related to using json_v2.object which resulted with an unnecessary empty object.

By removing

    [[inputs.mqtt_consumer.json_v2.object]]
      path = "@this"
      optional = true
      tags = [""]
      fields = [""]

And leaving just the two json_v2.field statements, I was able to get rid of the error, and the two values for t1 and t2 are now updated correctly.

2 Likes

@oh2th Thank you for sharing your solution! We really appreciate it.