Telegraf + MQTT + influxdb: Modifying input of mqtt

Viewed 2682

Let us assume I have various MQTT clients who send data within some topic, for instance for temperature sensors tele/temp/%devicename%/SENSOR in a JSON Format, such as

{"Time":"2020-03-24T20:17:04","DS18S20":{"Temperature":22.8},"TempUnit":"C"}}

My basic telegraf.conf looks as following

# Influxdb Output
[[outputs.influxdb]]
  database = "telegraf"

# sensors
[[inputs.mqtt_consumer]]
name_override = "sensor"
topics = ["tele/temp/+/SENSOR"]
data_format = "json"

My problem is now that I fail to do basic operations on that json data.

  1. I do not want to save the host and the topic. How can I drop fields?
  2. The topic contains the %devicename%. How can I add it as tag?
  3. I cannot use json_query, since the %devicename% is there and there is only one field.
  4. How can I rename the field %devicename%_Temperature to "temperature"?

In general, I would like to have an easy way to keep only the measurement of a multitude of sensors in the following format

timestamp           |   temperature |    device 

2020-03-24T20:17:04 |         22.8  |   DS18S20

Thanks a lot!

1 Answers

If you don't want to save the topic in InfluxDB set the following as part of your inputs_mqtt_consumer:

topic_tag=""

Adding the topic as a tag name can be done using processors.regex

[[processors.regex]]
[[processors.regex.tags]]
key = "topic"
pattern = ".*/(.*)/.*"
replacement = "${1}"
result_key = "devicename"

In this case the 2nd section of the topic would become the devicename.

Related