Having difficulty setting metric name from topic (getting value)- mqtt topic and value parsing

I have a topic called “cube/44080D05613C/voltage” which carries a voltage value (float).

I was able to parse most of it like so:

topics = [
    "cube/+/voltage",
  ]

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "cube/+/voltage"
    measurement = "cube/_/_"
    tags = "_/device/_"

(though now I am getting: Error in plugin: metric parse error: expected tag at 1:5: “5.25” , not sure why)

I am trying to get to a situation where:
• “cube” is the measurement
• there is a device tag which is that second position (44080D05613C in our example)
• the metric is “voltage” (not value) and a float

I am getting thoroughly confused. Closest I got at some point was having a value metric which carried the voltage value and a “voltage” metric that had a string in it.

I think you are just using the wrong keywords, have a look at the docs samples here

This should work

topics = [
    "cube/+/voltage",
  ]

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "cube/+/voltage"
    measurement = "measurement/_/_"
    tags = "_/device/_"

Thank you, @Giovanni_Luisotto

You got me back to where I had gotten to (before I messed things up by removing data_format & data_type by mistake).

The issue with this is that there is one field named value. I would like the field to be named “voltage”.

I tried the following:

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "cube/+/voltage"
    measurement = "measurement/_/_"
    tags = "_/device/_"
    fields = "_/_/test"
    [inputs.mqtt_consumer.topic_parsing.types]
      test = "float"

But I get: 2022-03-10T18:42:35Z E! [inputs.mqtt_consumer] Error in plugin: unable to convert field 'voltage' to type float: strconv.ParseFloat: parsing "voltage": invalid syntax

Looking at this article from an influxdb source I couldn’t find a way to do this either. @samdillard, maybe you know? Maybe something with a pivot?

@ferik this is a known need and here is the Github issue tracking it: Include Field pivoting in MQTT topic parsing · Issue #10712 · influxdata/telegraf · GitHub

For now – as you mentioned – pivot is your best friend! telegraf/plugins/processors/pivot at master · influxdata/telegraf · GitHub

But in this case @samdillard , I am not trying to make a tag a value, I am just trying to name the field “voltage”. The metric that’s sent for cube/device_id/voltage is right, it’s a float, I am not embedding a field in the topic name. I just want to change the field name from value to voltage. How would I do that?

That is what that issue is aimed to do. The pivot processor will do this for you for now.

@samdillard - do you mind helping, I am not sure how to use it:

[[inputs.mqtt_consumer]]
  ## Broker URLs for the MQTT server or cluster.  To connect to multiple
  ## clusters or standalone servers, use a separate plugin instance.
  ##   example: servers = ["tcp://localhost:1883"]
  ##            servers = ["ssl://localhost:1883"]
  ##            servers = ["ws://localhost:1883"]
  servers = ["tcp://192.168.7.229:1883"]

  ## Topics that will be subscribed to.
  topics = [
    "cube/+/voltage",
  ]
  data_format = "value"
  data_type = "float"

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "cube/+/voltage"
    measurement = "measurement/_/_"
    tags = "_/device/_"
    fields = "_/_/test"
    [inputs.mqtt_consumer.topic_parsing.types]
      test = "float"
    [[processors.pivot]]
      ## Tag to use for naming the new field.
      tag_key = "test"
      ## Field to use as the value of the new field.
      value_key = "voltage"

Sure thing. Given the way you’ve configured the consumer, your Tag will be field=voltage and the Field will be value=<float_value>. For pivot, your tag_key will then be “field” and the value_key will be “value”.

The best way to work this out for your future cases is to have Telegraf print out (with the --test flag or using the file output plugin to “stdout”) the data so you know exactly what your fields and tags are named and what values they have.

Hope that helps!