Telegraf beginner: write periodic state of several topics into influxdb

Hi experts,

I have several devices sending values to my mqtt broker every now and then. I would like to write a snapshot of the current values to influx every lets say 5 minutes.

Example mqtt stream:

mytopic/dev1/val1 = 1
mytopic/dev1/val2 = 2
mytopic/dev2/val1 = 3
mytopic/dev2/val2 = 4
mytopic/dev1/val1 = 5
mytopic/dev1/val2 = 6
mytopic/dev2/val1 = 7
mytopic/dev2/val2 = 8

→ now 5 minute interval is over, timestamp is 1692893506

this is what should be written into influx:

mytopic,dev=dev1 val1=5 val2=6 1692893506
mytopic,dev=dev2 val1=7 val2=8 1692893506

I fiddeled around a lot and searched for similar problems and solutions. Did not make it yet. Either it is not trivial or I am dumb. Or a bit of both :sweat_smile:

Any help is appreciated!

Best regards,
Keith

Hi @sauvant

The only usage experience that I have with Telegraf is to do regular metric scraping and sending to InfluxDB (see my example from last year). In your application, you want to summarize the data, then send it. I am guessing that there is a way to do this using Telegraf, but my first thoughts were to do one of the following approaches:

  1. Send all the data points into InfluxDB and do your aggregation every 5 minutes using the last() function (something like |> aggregateWindow(every: 5m, fn: last, createEmpty: false) for each of your topics (dev1 and dev1).
  2. Use Node-RED instead of Telegraf to do the summary (periodic state), then send that chunk of data to InfluxDB. Not sure if you have used Node-RED, but it’s pretty easy to get started with, it plays VERY nicely with MQTT and InfluxDB, and their forum is top notch for support.
1 Like

Cool, thanks for your reply. I expected that to be a piece of cake with Telegraf - astonishing! I will give Node Red a try :+1:

Sorry for being late to the party but an alternative is to use the “final” aggregator plugin and set the period accordingly…

1 Like