Correct way to configure json_v2 with mqtt_consumer

monotux
Mon, 07 Oct 2024 13:09:01 +0000
Telegraf

I’ve spent some time trying to understand how to use json_v2 with my mqtt_consumer flow, without any luck.

My json payload looks like this:

{
    "model": "Bresser-3CH",
    "id": 130,
    "channel": 1,
    "battery_ok": 1,
    "temperature_C": 12.66667,
    "humidity": 19,
    "mic": "CHECKSUM",
    "protocol": "Bresser Thermo-/Hygro-Sensor 3CH",
    "rssi": -61,
    "duration": 1753996
}

Of these, the following are tags:

channel

id

model

The following are the fields I’m interested in:

humidity

temperature_C

rssi

battery_ok

The rest (protocol, mic, duration) are not interesting and if possible I’d like to ignore them.

I have this setup using the json data format, and it works OK:

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = [
    "home/OpenMQTTGateway_lilygo_rtl_433_ESP/RTL_433toMQTT/#"
  ]

  topic_tag = "topic"
  qos = 0
  data_format = "json"

  username = "telegraf"
  password = "notmyrealpassword"

  tag_keys = [ "model", "id", "channel" ]

  json_string_fields = []

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "+/+/+/+/+/+"
    tags = "site/_/_/model/channel/device_id"

I’m not even sure the topic parsing is relevant with this setup?

I had tried with something like this, but getting an error message that my JSON was invalid, which surprised me! What is wrong with below?

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = [
    # Real topic structure
    # "home/OpenMQTTGateway_lilygo_rtl_433_ESP/RTL_433toMQTT/Bresser-3CH/1/130",
    "home/OpenMQTTGateway_lilygo_rtl_433_ESP/RTL_433toMQTT/#"
  ]

  topic_tag = "topic"
  qos = 0
  data_format = "json_v2"

  username = "telegraf"
  password = "notmyrealpassword"

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "+/+/+/+/+/+"
    tags = "site/_/_/model/channel/device_id"

    [[inputs.mqtt_consumer.json_v2]]

      [[inputs.mqtt_consumer.json_v2.field]]
      path = "humidity"
      type = "int"

      [[inputs.mqtt_consumer.json_v2.field]]
      path = "battery_ok"
      type = "int"

      [[inputs.mqtt_consumer.json_v2.field]]
      path = "temperature_C"
      type = "float"

      [[inputs.mqtt_consumer.json_v2.field]]
      path = "rssi"
      type = "int"

      [[inputs.mqtt_consumer.json_v2.tag]]
      path = "model"

      [[inputs.mqtt_consumer.json_v2.tag]]
      path = "id"
      type = "int"

      [[inputs.mqtt_consumer.json_v2.tag]]
      path = "channel"
      type = "int"

And oh, in case it’s interesting, here’s my output:

[[outputs.influxdb_v2]]
  urls = ["http://localhost:8086"]
  token = "$INFLUX_TOKEN"
  organization = "foobar"
  bucket = "metrics"

4 posts - 2 participants