MQTT consumer multiple types topic_parser

Hello,
i’m trying to setup a mqtt consumer telegraf node. My situation is the same as #10712, for example:I have the following topics

ACME/controller0/v1/air-conditioner/temp (e.g. 23.59  - float ) 
ACME/controller0/v1/air-conditioner/rpm  (e.g. 2000   - int   ) 
ACME/controller0/v1/air-conditioner/mode (e.g. MANUAL - string)

I need to consume the mqtt data to an influxdb node. To do so i have written the following config:

[...] 

[[inputs.mqtt_consumer]] 
  servers = ["tcp://acme.mqtt.org:8883"] 
 
 topics = [
    "ACME/#", 
  ] 
  
  data_format = "value" 
  data_type = "string" 

  [[inputs.mqtt_consumer.topic_parsing]] 
    topic = "ACME/+/+/+/temp" 
    measurement = "agency/_/_/_/_" 
    tags = "_/controller_id/controller_version/contoller_type/_" 
    fields = "_/_/_/_/temperature" 
    [inputs.mqtt_consumer.topic_parsing.types] 
      temperature = "float" 
    
  # Other topic parsers to differentiate the types, same as above but with a different type 
  [[inputs.mqtt_consumer.topic_parsing]] 
    topic = "ACME/+/+/+/rpm"

[ ... ]

I’ve got 2 problems:

  1. Standard mqtt_consumer LPs get generated even though the topic_parser is active, e.g.
mqtt_consumer,host=this-host,topic=ACME/controller0/v1/air-conditioner/temp value="23.59" 1651224634909093887
  1. The plugin is unable to convert from string to (e.g.) float , giving
[inputs.mqtt_consumer] Error in plugin: unable to convert field 'temp' to type float: strconv.ParseFloat: parsing "temp": invalid syntax

What am i doing wrong? Is this the best-practice for dealing with multiple types of mqtt values?Thanks in advance

Hi @drdee,
Welcome to the community :slight_smile:. Hopefully, I can help answer some of your questions:

  1. So let’s start with the measurement name. I think that is as simple as changing:
measurement = "agency/_/_/_/_" 

to

measurement = "measurement/_/_/_/_" 
  1. For your second issue I think this might be a little more tricky and I could be wrong. Essentially on each topic you are collecting from you are assuming the payload is of type string with the following line:
  data_format = "value" 
  data_type = "string" 

I belive the [inputs.mqtt_consumer.topic_parsing.types] Only takes into account changing the value for the following situation:

 topic = "telegraf/one/cpu/23"
 fields = "_/_/_/test"
    [inputs.mqtt_consumer.topic_parsing.types]
      test = "int"

In English → i want to change the topic level “23” to a field and change its type to float.
In your case the value is coming as part of the payload this wouldn’t work. I would advise splitting your topics into multiple MQTT consumers based on there value type returned. i.e. one consumer that deals with all string types and one that deals with one float.