I am publishing data to MQTT topics with the Mosquitto broker, and am trying to pipe the data points through to my InfluxDB database. Currently I am trying to do this using the mqtt_consumer input plugin for Telegraf.
A simplified version of my telegraf.conf file looks like this:
# Global Agent Configuration
[agent]
interval = "1m"
# Input Plugins
[[inputs.mqtt_consumer]]
name_prefix = "sensors_"
servers = ["localhost:1883"]
qos = 0
connection_timeout = "30s"
topics = [
"/inside/kitchen/temperature"
]
persistent_session = false
data_format = "value"
data_type = "float"
# Output Plugins
[[outputs.influxdb]]
database = "telegraf"
urls = [ "http://localhost:8086" ]
Now, I can manually publish a data point using MQTT via the following:
~ $ mosquitto_pub -d -t /inside/kitchen/temperature -m 23.5
where I have used the MQTT topic /inside/kitchen/temperature and a value of 23.5.
Examining my InfluxDB database, I see the following data points:
name: sensors_mqtt_consumer
time topic value
---- ----- -----
2020-06-27T20:08:50 /inside/kitchen/temperature 23.5
2020-06-27T20:08:40 /inside/kitchen/temperature 23.5
Is there any way that I can use the MQTT topic name description to properly allocate Influx tags? For example, I would like something like:
name: temperature
time location room value
---- ----- ----- -----
2020-06-27T20:08:50 inside kitchen 23.5
2020-06-27T20:08:40 inside kitchen 23.5
Is there a way to do this with the InfluxDB/Mosquitto/Telegraf configurations? Later I will be adding more topics (locations, etc) and measurements (humidity, voltage, etc), so the solution should allow for this.
(I know that this can be done by by choosing data_format = "influx"
as described here, where Telegraf interprets the message as InfluxDB line protocol and passes it through directly. However, then I would have to publish to the topic in this way:
mosquitto_pub -d -t /inside/kitchen/temperature -m "temperature,location=inside,room=kitchen value=23.5"
where you can see that most of the information has been input twice, even though it already existed. The same can be said for the data_format="json" option. What I need is more of a mapping).