Telegraf: renaming measurements when sending mqtt metrics to influxdb

I am collecting a variety of metrics via mqtt and using telegraf to send those to an influxdb instance. Ideally, I would like them all to end up in the same database but with different measurement names. The default telegraf.conf dumps everything into the mqtt_consumer measurements, which gets messy quickly as each sensor may have its own schema.

I can use things like tagpass and tagdrop (or namepass and namedrop, etc) to select between different [[outputs.influxdb]] sections, but I haven’t been able to figure out how, in these output sections, to influence the target measurement name.

It looks like maybe I could approach this from the other direction, and have multiple [[inputs.mqtt]] sections using the name_override parameter. What I really want is templated measurements names: that is, measurement names that are built from tags gathered by telegraf. Something like name_override = sensor_${sensor_type}. Is anything like that possible?

What data format are you using with the mqtt_consumer? If you can use the data_format = "influx" you can specify the measurement name: sensor_foo value=42. If this data format is not possible, you will need to use multiple inputs and naming the measurement based on the topic.

Can I name measurements “based on the topic”? I mean, can I use the topic to set the measurement name somehow? After a little research, I think what I am asking about is this currently open feature request, so I guess the answer is “no”.

I understand that I can do something like…

[[inputs.mqtt]]
  topics = ["sensor/dht/+"]
  name_override = "sensor_dht"

[[inputs.mqtt]]
  topics = ["sensor/weather/+"]
  name_override = "sensor_weather"

…which is what I am doing now. I was just looking for a way to simplify the configuration.

I really like your approach with adressing measurement based on the mqtt topic, i use this myself. However, i am not using Telegraf (so far).
Istead i am using a python script based on the influxdb-python library.

My my mqtt message typically looks like:
topic: “weather/station/met_sample”,
the last item “met_sample” is translated to measurement name.

payload: {“tst” : 1511196991, “airTemperature” : 12.2, “airPressure” : 1012.2}
The payload is json formatted, value of “tst” is used as timestamp (epoch in seconds).
“AirTemperature” and “airPressure” are fields in the measurement “met_sample”

It would be nice if Telegraf could handle this in an efficient way, but unfortunately, i have no experience in using Telegraf.

You could use a config like this:

[[inputs.mqtt]]
  topics = ["weather/station/met_sample"]
  name_override = "met_sample"
  data_format = "json"

Fine, thanks.
I should maybe start using Telegraf but can i use my timestamp “tst” as the influxDb timestamp?

That will actually be a problem currently, we do have an issue open for this that you could keep an eye on.