mhendriks / P1-Dongel-ESP32

P1-Dongel-ESP32
MIT License
27 stars 13 forks source link

MQTT - parsing values by a prometheus exporter #2

Closed djieno closed 1 year ago

djieno commented 1 year ago

First off: what a great piece of hardware and code! I received it yesterday and everything worked great.

I mainly want to use the p1 telegram data -> mqtt -> prometheus exporter. I've setup eclise (eclipse-mosquitto:2) and ghcr.io/hikhvar/mqtt2prometheus:latest.

The problem: parsing the topics by the exporter requires message/payload to be in the (json) form:

{"temperature":23.20,"humidity":51.60, "computed": {"heat_index":22.92} } eg. the example of the exporter of hikvar (used golang mqtt parsing)

results in: image So this is the desired outcome of the exporter. It finds a 'temperature' key responding to the value '23.2'.

If the exporter parses the topic coming from the pi-dongle-esp32 it will have only the value and not the key. Therefore it's not able to parse it since it doesn't find the (missing) key but only the value. Below another example/usecase:

image

Seeing https://github.com/mhendriks/P1-Dongel-ESP32/blob/master/MQTT.ino#L227 also confirms what I've noticed. Would It be possible to have the whole P1/telegram output enriched with keys in Json message? So every cycle of P1 data is send to mqtt as one json payload to one topic (eg P1/dongle-esp32/electricity). This way all telegram data is kept as a complete message and it's easily to parse keys, values.

It didn't find any best practices for mqtt payloads but it seems useful already in my case: I need to set the type of value for 'energy_delivered_tariff1' to counter to be able to do (in promql) energy_delivered_tariff1[24] and see the additional usage since 24hr.

Perhaps an idea to make a toggle in the interface mqtt message in (one) json and have switched off by default but selectable.