Hi there!
I’m trying to setup a telegraf mqtt_consumer config. My MQTT Server get’s lots of simple valued topics that I would like to log in my influxDB.
I have topics of various data types: int, float & enum (=string?).
Do I need to handle those in 3 different [[inputs.mqtt_consumer]]
instances (with data_format = "value"
& data_type = "*"
where *
= integer, float & string?
Or is there a (reasonable) way to get it done with a single MQTT-client over a single connection to the MQTT-server?
To make my desire to handle multiple data_types in a single MQTT-client easier to understand, see my current config of two clients where most lines are annoying applications of the copy-paste-pattern:
[[inputs.mqtt_consumer]]
servers = ["tcp://127.0.0.1:1883"]
username = "*****"
password = "*****"
topics = [
"valetudo/+/BatteryStateAttribute/level",
"valetudo/+/WifiConfigurationCapability/signal",
]
topic_tag = ""
data_format = "value"
data_type = "integer"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "valetudo/+/+/+"
measurement = "_/measurement/_/_"
tags = "_/_/category/variable"
[[processors.regex]]
order = 1
[[processors.regex.tags]]
key = "category"
pattern = "(.*)(StateAttribute)|(ConfigurationCapability)"
replacement = "${1}"
[[processors.template]]
order = 2
tag = "joined"
template = '{{ .Tag "category" }}.{{ .Tag "variable" }}'
[[processors.pivot]]
order = 3
tag_key = "joined"
value_key = "value"
tagexclude = ["category","variable"]
[[inputs.mqtt_consumer]]
servers = ["tcp://127.0.0.1:1883"]
username = "*****"
password = "*****"
topics = [
"valetudo/+/BatteryStateAttribute/status",
"valetudo/+/StatusStateAttribute/status",
"valetudo/+/StatusStateAttribute/detail",
"valetudo/+/StatusStateAttribute/error",
]
topic_tag = ""
data_format = "value"
data_type = "string"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "valetudo/+/+/+"
measurement = "_/measurement/_/_"
tags = "_/_/category/variable"
[[processors.regex]]
[[processors.regex.tags]]
order = 1
key = "category"
pattern = "(.*)StateAttribute"
replacement = "${1}"
[[processors.template]]
order = 2
tag = "joined"
template = '{{ .Tag "category" }}.{{ .Tag "variable" }}'
[[processors.pivot]]
order = 3
tag_key = "joined"
value_key = "value"
tagexclude = ["category","variable"]
UPDATE: okey, so I think maybe I’d be able to only use a single mqtt_consumer
with data_type = "string"
and use [[processors.parser]]
to parse integers, floats & booleans out of their string versions afterwards.
UPDATE2: by now I’ve learned that by using stupid config file formatting I’ve deluded myself into believing those [[processors.*]]
blocks to only apply to exactly their immediately preceding [[inputs.*]]
block. That was a false believe.
My current state of the relevant Telegraf config segment is:
[[inputs.mqtt_consumer]]
servers = ["tcp://127.0.0.1:1883"]
username = "*****"
password = "*****"
topics = [
"valetudo/+/BatteryStateAttribute/level",
"valetudo/+/BatteryStateAttribute/status",
"valetudo/+/StatusStateAttribute/status",
"valetudo/+/StatusStateAttribute/detail",
"valetudo/+/StatusStateAttribute/error",
"valetudo/+/WifiConfigurationCapability/signal",
]
topic_tag = ""
data_format = "value"
data_type = "string"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "valetudo/+/+/+"
measurement = "_/measurement/_/_"
tags = "_/_/category/variable"
[[processors.regex]]
[[processors.regex.tags]]
namepass = ["roborock", "dreame"]
order = 1
key = "category"
pattern = "(.*)(StateAttribute)|(ConfigurationCapability)"
replacement = "${1}"
[[processors.template]]
namepass = ["roborock", "dreame"]
order = 2
tag = "joined"
template = '{{ .Tag "category" }}.{{ .Tag "variable" }}'
[[processors.pivot]]
namepass = ["roborock", "dreame"]
order = 3
tag_key = "joined"
value_key = "value"
tagexclude = ["category","variable"]
[[processors.parser]]
namepass = ["roborock", "dreame"]
order = 4
parse_fields = ["Battery.level", "Wifi.signal"]
drop_original = false
merge = "override"
data_format = "value"
data_type = "integer"
The problem with this config is, that the [[processors.parser]]
writes the parsed values into a new measurement called parser
instead of the source measurement where I want them and I would have expected them with an option merge = "override"
set…