Configure Telegraf for nested array of timestamps and data

natwez22
Fri, 19 Jul 2024 22:07:23 +0000
Telegraf

I have a json array coming in over mqtt that looks like the following

{
  "Sensor1": {
    "Time": [
      1721423157027428600,
      1721423157031428600,
      1721423157035428600,
      1721423157039428600,
      1721423157043428600,
      1721423157047428600,
      1721423157051428600,
      1721423157055428600,
      1721423157059428600,
      1721423157063428600
    ],
    "Data1": [
      0.5375196810086651,
      0.9610414132027387,
      0.0018270982367867283,
      0.3682829898479403,
      0.1601324861510789,
      0.19179781722154354,
      0.930889384682748,
      0.49418687417064033,
      0.7972837143268192,
      0.10455857994855733
    ]
  }
}

What I want is the following is the following influxdb line protocol output

foobar,Sensor1 Data1=0.5375196810086651 1721423157027428600
foobar,Sensor1 Data1=0.9610414132027387 1721423157031428600
foobar,Sensor1 Data1=0.0018270982367867283 1721423157035428600
.
.
.

You get the point

Here is my config

[[inputs.mqtt_consumer]]
   servers = ["tcp://127.0.0.1:1883"]
   topics = ["sensordata"]
   data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
        measurement_name = "Sensor1"
        timestamp_path = "Sensor1.Time.0"
        timestamp_format = "unix_ns"

        [[inputs.mqtt_consumer.json_v2.field]]
            path = "Sensor1.Data1.0"
            type = "float"
            rename = "Data1"

[[inputs.mqtt_consumer.json_v2]]
        measurement_name = "Sensor1"
        timestamp_path = "Sensor1.Time.1"
        timestamp_format = "unix_ns"

        [[inputs.mqtt_consumer.json_v2.field]]
            path = "Sensor1.Data1.1"
            type = "float"
            rename = "Data1"

[[inputs.mqtt_consumer.json_v2]]
        measurement_name = "Sensor1"
        timestamp_path = "Sensor1.Time.2"
        timestamp_format = "unix_ns"

        [[inputs.mqtt_consumer.json_v2.field]]
            path = "Sensor1.Data1.2"
            type = "float"
            rename = "Data1"
.
.
.

And this for a total of 10 times.
This produces the result I want, however, i am setting up a config for every single data point.
Is there an easier way to configure Telegraf? I have read through many of the examples in github but i can’t figure out how to do this easier in the config.
Am i doing something wrong?

2 posts - 2 participants