Telegraf MQTT Consumer

Hi there,

I am using Telegraf 1.14.0 with enabled MQTT consumer. The configuration as it follows:

# # Read metrics from MQTT topic(s)
[[inputs.mqtt_consumer]]
#   ## MQTT broker URLs to be used. The format should be scheme://host:port,
#   ## schema can be tcp, ssl, or ws.
   servers = [
     "tcp://192.168.1.109:1883",
     "tcp://192.168.1.27:1883",
   ]

  topics = [
    "nexus433/#",
    "openweather/web/temperature",
    "speedtest/network/state",
  ]
#
#   ## Data format to consume.
#   ## Each data format has its own unique set of configuration options, read
#   ## more about them here:
#   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
   data_format = "json"

I have two MQTT brokers that are running on a different raspberry pi. The payload that is coming from from the first (192.168.1.109) MQTT is

{ “temperature”: 29.3, “humidity”: 41, “battery”: “100”, “quality”: 66 }

and from the second (192.168.1.27) is

{ “temperature”: 23.4, “humidity”: 53, “battery”: “100”, “quality”: 100, “id”: 113, “channel”: 1 }

So the payload is fairly similar with the exception of two extra values in the second case. When I start telegraf I ma getting the following error:

2020-05-27T16:02:53Z E! [inputs.mqtt_consumer] Error in plugin: invalid character ‘o’ looking for beginning of value

and the data from the second broker is never pushed to InfluxDB whereas the data from the first one is getting saved into InfluxDB with no problem. I was trying to use only second MQTT broker but the error is still the same.

The both hosts have the same version of mosquitto MQTT. I run out of ideas what is wrong and where problem could be.

After a bit of debugging it turned out that the error is caused by the online message that gets sent by MQTT and because it’s not in json format parser fails to parse it.

However, what is more interesting is that after this error MQTT consumer keeps getting messages but only from the first server in the list of servers defined in the config. Messages from the second server in the list are never received ¯_(ツ)_/¯ If I swap the order of servers in the config that I will still be getting messages only from the first server.
I will run two telegraf instances with different configs and it solves the problem for me for now.

This looks like a documentation issue. When you specify multiple servers to this plugin it allows you to connect to a MQTT Cluster.

Since you just want to read from two different brokers then you will need to use two instances of the plugin. This will allow you to run everything from a single Telegraf instance:

[[inputs.mqtt_consumer]]
	servers = [
		"tcp://192.168.1.27:1883",
	]

	topics = [
		"nexus433/#",
		"openweather/web/temperature",
		"speedtest/network/state",
	]
	data_format = "json"

[[inputs.mqtt_consumer]]
	servers = [
		"tcp://192.168.1.109:1883",
	]

	topics = [
		"nexus433/#",
		"openweather/web/temperature",
		"speedtest/network/state",
	]
	data_format = "json"

Great! Thanks. That solved the problem for me!

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.