InfluxDB / Telegraf / MQTT - Can't read data

Hello All,
I send following JSON via MQTT:
‘{“TimeStamp”:1658602981983,“WorkCenter”:“PLIATDST12”,“PlantCode”:“2345”,“LineName”:“PLT01”,“MachineName”:“CTR01”,“SensorName”:“UW2_PR_TotRMSVibVelo_1”,“AnalogValue”:232.98875427246095}’

With Topic: ‘PLT01/CTR01/UW2_PR_TotRMSVibVelo_1’

I can’t read those data on Telegraf in InfluxDB.
Below snippet from Telegraf configuration.

[[inputs.mqtt_consumer]]
servers = [“tcp://192.168.1.169:1886”]

topics = [
“PLT01/CTR01/UW2_PR_TotRMSVibVelo_1”
]

data_format = “json”
tag_keys = [“WorkCenter” , “PlantCode” , “LineName” , “MachineName” , “SensorName”]

Telegraf Dubug shows some error:
[inputs.mqtt_consumer] Error in plugin: invalid character ‘\x00’ after top-level value

Who can help me? I spent lot of time trying to solve it and reading docu.

Regards
Tom,

Hi @Tom999 - Welcome to the community forum.

Try Google search:
telegraf error in plugin invalid character ' ' after top-level value

This post talks about the same error message and this post is about the same setup (MQTT/Telegraf/InfluxDB). Also This Forum Post is about the same subject. All of these say something isn’t right (e.g., a bad format or unexpected character) in the JSON.

Hello
the problem is ‘\x00’ at the end of JSON payload. This is added on PLC level, where JSON message is created. I can’t manipulate it.
New idea would be to use JSON version 2 with parsing.

I tried this:
[[inputs.mqtt_consumer.json_v2]]
timestamp_path = “TimeStamp”
timestamp_format = “unix”

[[inputs.mqtt_consumer.topic_parsing]]
topic = “+/+/+”
tags = “WorkCenter/LineName/SensorName”

[[inputs.mqtt_consumer.json_v2.field]]
path = "AnalogValue"
type = "float"

[[inputs.file.json_v2.tag]]
path = "WorkCenter"

[[inputs.file.json_v2.tag]]
path = "PlantCode"

[[inputs.file.json_v2.tag]]
path = "LineName"

[[inputs.file.json_v2.tag]]
path = "MachineName"

[[inputs.file.json_v2.tag]]
path = "SensorName"

But now I get message “Invalid JSON provided, unable to parse”
How to configure Telegraf config file to use JSON ver 2 ?

No clear documentation is available.
I think my issue is very simple but there is no informative example how to handle it.

I have topic: ‘PLT01/CTR01/UW2_PR_TotRMSVibVelo_1’

And JSON payload:
‘{“TimeStamp”:1658602981983,“WorkCenter”:“PLIATDST12”,“PlantCode”:“2345”,“LineName”:“PLT01”,“MachineName”:“CTR01”,“SensorName”:“UW2_PR_TotRMSVibVelo_1”,“AnalogValue”:232.98875427246095}’ - last char is NULL.

Tom.

Hi @Tom999 -
Detailed mqtt_consumer documentation can be found here.

There are some problems with this code.

  1. Do not mix [[inputs.mqtt_consumer]] and [[inputs.file]]. All lower-level plug-in statements must also start with [[inputs.mqtt . . .]].
  2. Lower-level statements must use proper indentation (see this recent post.)
  3. Need to specify data_format = "json_v2" at the beginning.
  4. First parse the topic to unwrap the payload, then parse the JSON. It is reversed in your code which probably caused the error message.