mqtt_exporter
mqtt_exporter copied to clipboard
Regex for value replacement is not parsed correctly by YAML reader
I'm trying to extract values from JSON content (coming from a Shelly HT sensor) via value regex with the following config:
- name: "shelly_battery_voltage"
help: "Shelly Battery Voltage"
type: "gauge"
topic: "shellies/+/info"
label_configs:
- source_labels: ["__value__"]
regex: ".*voltage.:([0-9.]*)"
target_label: "__value__"
replacement: '\1'
action: 'replace'
The JSON content looks like this:
{"tmp":{"value":24.00,"units":"C","tC":24.00,"tF":75.20,"is_valid":true},"hum":{"value":70.0,"is_valid":true},"bat":{"value":94,"voltage":2.93},"act_reasons":["sensor"],"connect_retries":0}
When I try this with a Python RegEx tester it works fine. However, the MQTT_Exporter gives the following error in the log (line 55 is the "regex" line): yamlreader.yamlreader.YamlReaderError: YAML Error: while scanning a double-quoted scalar in "conf/mqtt_exporter.yaml", line 55, column 16 found unknown escape character '.' in "conf/mqtt_exporter.yaml", line 55, column 34
How can I specify this regex correctly? I tried any type of quotes or escaping, none works. Or is there another regex variant to avoid this error?
Hmm, this is odd. YAML linter doesn't complain about escaping, regex101.com's Python implementation works as well.
Does the same problem occur without the quotes?
- name: shelly_battery_voltage
help: Shelly Battery Voltage
type: gauge
topic: shellies/+/info
label_configs:
- source_labels: ["__value__"]
regex: .*voltage.:([0-9.]*)
target_label: __value__
replacement: \1
action: replace
I have far more "fancy" regex in my setup. But I use single quotes. And all examples in the repo use single quotes too. AFAIK unquoted strings won't work with braces as they can note an array. Double quoted string allow escaping, but I was not aware that '.' dot is an escape char, but it seems to be.
Hmm, this is odd. YAML linter doesn't complain about escaping, regex101.com's Python implementation works as well.
Exactly, I tested the regex as well with such services and it worked fine.
Does the same problem occur without the quotes?
Without the quotes, there is no error when parsing the regex by the YAML interpreter, but it's also not really recognized and so the extraction of the desired value fails.
Is there another recommended way to extract values from JSON? Another kind of regex maybe?
@jce-zz I updated the PyYAML dependency in the latest v.2.0.0 release. Please take a look if this changed something for you.