Suppor needed: Mqtt Topic parsing using telegraf

This is the topic result getting from Mqtt broker, need to insert data in influxdb " id and message " with time stamp , pls guide how to parse this json to influx

{
“state”: {
“time”: 1366360.3416,
“data”: {
“alarms”: {
“PS1016”: {
“path”: 1,
“axis_code”: 0,
“axis”: “”,
“number”: 1016,
“message”: “EOB NOT FOUND”,
“type_code”: 3,
“type”: “PS”,
“id”: “PS1016”,
“is_triggered”: true
}
}
}
}
}

Hi,

Neither ID or message are numeric values. It is unusual to create metrics without a value that can be graphs or measured over time.

This would get you all the fields:

[agent]
  debug = true
  omit_hostname = true

[[inputs.file]]
  files = ["metrics.json"]
  data_format = "xpath_json"
  xpath_allow_empty_selection = true
  xpath_native_types = true

  [[inputs.file.xpath]]
    metric_selection = "//state/data/alarms/PS1016"
    field_selection = "*"

[[outputs.file]]

What is that timestamp?

Hi, thanks for supporting, “PS1016” value will be dynamic based on “alarms” from Mqtt topic message, This code “PS1016” and message will change based on “alarms”. i m using Mqtt _ consumer plugin. how to store the id & message value from the dynamic Mqtt topic in influx db

Message one :
{
“state”: {
“time”: 1366360.3416,
“data”: {
“alarms”: {
“PS1016”: {
“path”: 1,
“axis_code”: 0,
“axis”: “”,
“number”: 1016,
“message”: “EOB NOT FOUND”,
“type_code”: 3,
“type”: “PS”,
“id”: “PS1016”,
“is_triggered”: true
}
}
}
}
}

Message two:

{
“state”: {
“time”: 1366360.3416,
“data”: {
“alarms”: {
“ER45”: {
“path”: 1,
“axis_code”: 2,
“axis”: “”,
“number”: 45,
“message”: “G code missing”,
“type_code”: 2,
“type”: “ER”,
“id”: “ER45”,
“is_triggered”: true
}
}
}
}
}

Think through this… you need a wild card! So how about trying:

metric_selection = “//state/data/alarms/*”

I ve tried below, after adding wild card, only the second one is updated in influxdb, first one is missing, pls find the json:
{
“state”: {
“time”: 266680.5801,
“data”: {
“alarms”: {
“Is1016”: {
“path”: 1,
“axis_code”: 5,
“axis”: “6”,
“number”: 1016,
“message”: “EOB NOT FOUND”,
“type_code”: 3,
“type”: “IS”,
“id”: “IS1016”,
“is_triggered”: true
},
“js1016”: {
“path”: 1,
“axis_code”: 5,
“axis”: “6”,
“number”: 116,
“message”: “EOB FOUND”,
“type_code”: 3,
“type”: “S”,
“id”: “I016”,
“is_triggered”: true
}
}
}

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

data_format = "xpath_json"
  xpath_allow_empty_selection = true
  xpath_native_types = true

		
[[inputs.mqtt_consumer.xpath]]
  metric_selection = "//state/data/alarms/*"
  metric_name = "string('alarms')"
  timestamp = "/timestamp"
  timestamp_format = "2006-01-02T15:04:05Z"
  field_selection = "*"

You need to set some additional tags to differentiate one from the other, like type or ID. Depends on what you need to filter on.

Either defined as xpath tags or using the converter processor to convert a field to a tag:

[[processors.converter]]
  [processors.converter.fields]
    tag = ["id"]