mqtt2prometheus
mqtt2prometheus copied to clipboard
Allow overriding the device id regex per metric
This allows the regex used to extract the device id to be overriden per metric. This required a little reorganizing of where the device id is parsed from the topic (and therefore how it's passed between functions). The default parsing remains the same.
This is useful (in my installation) with the availability topic created by Zigbee2MQTT. It creates topics like, zigbee2mqtt/thermostat/availability
for availability. The main topic, zigbee2mqtt/thermostat
, has the main set of metrics (and correctly parses using the default device id regex) but then zigbee2mqtt/thermostat/availability
has {"state":"online"}
. With this change, I can add a config that looks like this,
- prom_name: availability
mqtt_name: state
help: Device availability (from Zigbee2MQTT)
type: gauge
device_id_regex: "zigbee2mqtt/(?P<deviceid>.*)/availability"
string_value_mapping:
# A map of string to metric value.
map:
online: 1
offline: 0
and then get a bunch of metrics that look like this,
availability{sensor="living_room",topic="zigbee2mqtt/living_room/availability"} 1 1672873723990
availability{sensor="bedroom",topic="zigbee2mqtt/bedroom/availability"} 1 1672873723964
availability{sensor="thermostat",topic="zigbee2mqtt/thermostat/availability"} 1 1672873723964
Hey @hikhvar just wanted to bump this and see what you thought. I'm very happy to discuss more if you think there's a better way to implement this or any other comments you have. I should have created an issue so we could have discussed it beforehand, but I was in the code and on a roll getting my setup working so I figured I'd just make the changed.
No worries, thanks for taking a look!