Telegraf - MQTT Multiple Topic

Hi,

I am getting data from two topics from an MQTT broker. In one topic the data is temperature data and in the second topic, the data is vibration data. As per the following configuration, the data from both topic will be added with the measurement as point_data.
How can I add different measurement ‘temperature’ for topic “temperature/simulated/0” and measurement ‘vibration’ for topic "“vibrartion/simulated/0”?

 [[inputs.mqtt_consumer]]
    servers = ["tcp://$HOST_IP:1883"]
    topics = [
     "temperature/simulated/0",
     "vibrartion/simulated/0",
     ]
    name_override = "point_data"
    data_format = "json"

The easiest way is to simply make a second [[inputs.mqtt_consumer]] entry with the same URL and the other topic. You will end up with 2 connections to the MQTT broker, but I know it works because I do it all the time. :slight_smile:

dg

1 Like

Hi in single connection can we do it?. m trying , but shows path not found for two topics, can u suggest me how to do

I suggest you show us the actual configuration you are trying to use, and quote
the exact error message you are getting. Otherwise we’re just guessing…

Antony.

Hi, this is the configuration, m trying

[[inputs.mqtt_consumer]]
servers = [“tcp://127.0.0.1:1883”]
topics = [“fanuc/mill/machine/1”,“fanuc/turn/state/1”]
qos = 0
connection_timeout = “30s”
data_format = “json_v2”

[[inputs.mqtt_consumer.topic_parsing]]
    topic = "fanuc/mill/machine/1"  		
	[[inputs.mqtt_consumer.json_v2]]
	    measurement_name = "Machine2"
        [[inputs.mqtt_consumer.json_v2.tag]]
            path = "observation.machine"
        [[inputs.mqtt_consumer.json_v2.field]]
            path = "state.data.axes"	
	        rename = "No of axes "
			type = "int"


[[inputs.mqtt_consumer.topic_parsing]]
    topic = "fanuc/mill/state/1" 
	[[inputs.mqtt_consumer.json_v2]]
	    measurement_name = "Machine4"
        [[inputs.mqtt_consumer.json_v2.tag]]
            path = "observation.machine"
        [[inputs.mqtt_consumer.json_v2.field]]
            path = "state.data.timers.cutting_min"	
	        rename = "N"
			type = "int"

This is the error:
2023-12-24T12:13:10Z E! [inputs.mqtt_consumer] Error in plugin: the path “state.data.timers.cutting_min” doesn’t exist
2023-12-24T12:13:35Z E! [inputs.mqtt_consumer] Error in plugin: the path “state.data.timers.cutting_min” doesn’t exist

When i m trying with first topic, works fine, after adding the second topic show error, & no data is added
This is the topic 2 json data:

{
“observation”: {
“time”: 1703308893842,
“machine”: “turn”,
“name”: “state”,
“marker”: [
{
“type”: “path”,
“number”: 1
}
]
},
“state”: {
“time”: 79272.1764,
“data”: {
“mode”: “MANUAL_DATA_INPUT”,
“execution”: “READY”,
“aut”: 0,
“run”: 0,
“motion”: 0,
“mstb”: 0,
“emergency”: 0,
“alarm”: 0,
“timers”: {
“poweron_min”: 1955,
“operating_min”: 68,
“cutting_min”: 7
},
“override”: {
“feed”: 30,
“rapid”: 0,
“spindle”: 70
},
“modal”: {
“m1”: 0,
“m2”: 0,
“m3”: 0,
“t”: 0
}
}
}
}

Well, firstly those topics are completely different from what you showed in your
first post.

Secondly, can you either show some raw data from MQTT, or subscribe from a
command-line client and show us samples of the data including those topic
names?

Antony.

I do not see any instance of “state.data.axes” in that JSON, and yet you are
not getting an error from telegraf about this being missing?

Antony.

Topic one: fanuc/mill/machine/1

{“observation”:{“time”:1703308437307,“machine”:“mill”,“name”:“machine”,“marker”:[{“type”:“path”,“number”:20}]},“state”:{“time”:1003.7007,“data”:{“focas_support”:[“0”,“i”,“F”,“M”],“loader_control”:false,“i_series”:true,“compound_machining”:false,“transfer_line”:false,“model”:“MODEL F”,“model_code”:34,“max_axis”:32,“cnc_type”:“Series 0i”,“cnc_type_code”:“0”,“mt_type”:“Machining center”,“mt_type_code”:“M”,“series”:“----”,“version”:“----”,“axes”:20}}}

Topic two: fanuc/mill/state/1

{“observation”:{“time”:1703308893842,“machine”:“turn”,“name”:“state”,“marker”:[{“type”:“path”,“number”:1}]},“state”:{“time”:79272.1764,“data”:{“mode”:“MANUAL_DATA_INPUT”,“execution”:“READY”,“aut”:0,“run”:0,“motion”:0,“mstb”:0,“emergency”:0,“alarm”:0,“timers”:{“poweron_min”:1955,“operating_min”:68,“cutting_min”:7},“override”:{“feed”:30,“rapid”:0,“spindle”:70},“modal”:{“m1”:0,“m2”:0,“m3”:0,“t”:0}}}}

There are two topics and two json data, need to parse in single mqtt connection, two measurements, i ve tried with two mqtt connections with single topics, it works.
if combine two, not working

So, your data contains:

Topic one: fanuc/mill/machine/1

Topic two: fanuc/mill/state/1

Whereas you are telling telegraf to subscribe to:

topics = [“fanuc/mill/machine/1”,“fanuc/turn/state/1”]

Antony.

yes, right, i tried two connections below, it is working, but i want to do single connections

# Read metrics from MQTT topic(s)

[[inputs.mqtt_consumer]]
servers = [“tcp://127.0.0.1:1883”]
topics = [“fanuc/mill/machine/1”]
qos = 0
connection_timeout = “30s”
data_format = “json_v2”

Machine_Info:

[[inputs.mqtt_consumer.topic_parsing]]
    topic = "fanuc/mill/machine/1"  		
	[[inputs.mqtt_consumer.json_v2]]
	    measurement_name = "Machine2"
        [[inputs.mqtt_consumer.json_v2.tag]]
            path = "observation.machine"
        [[inputs.mqtt_consumer.json_v2.field]]
            path = "state.data.axes"	
	        rename = "No of axes "
			type = "int"

[[inputs.mqtt_consumer]]
servers = [“tcp://127.0.0.1:1883”]
topics = [“fanuc/mill/state/1”]
qos = 0
connection_timeout = “30s”
data_format = “json_v2”

Machine_Info:

[[inputs.mqtt_consumer.topic_parsing]]
    topic = "fanuc/mill/state/1"  # all topics with SENSOR
	
	[[inputs.mqtt_consumer.json_v2]]
	    measurement_name = "Machine4"
        [[inputs.mqtt_consumer.json_v2.tag]]
            path = "observation.machine"
        [[inputs.mqtt_consumer.json_v2.field]]
            path = "state.data.timers.cutting_min"	
	        rename = "N"
			type = "int"

Log:
2023-12-24T12:52:46Z I! [inputs.mqtt_consumer] Connected [tcp://127.0.0.1:1883]
2023-12-24T12:52:46Z I! [inputs.mqtt_consumer] Connected [tcp://127.0.0.1:1883]