kpetremann / mqtt-exporter

Simple generic MQTT Prometheus exporter for IoT working out of the box
https://hub.docker.com/r/kpetrem/mqtt-exporter
MIT License
103 stars 29 forks source link

Support esphome format #49

Closed zoic21 closed 1 year ago

zoic21 commented 1 year ago

Hello,

I have lot of esphome device and I want to transfert data from esphome to prometheus, I try mqtt-exporter but format of mqtt message of esphome is not supported, example of topic : esp-weather-outdoor/sensor/temperature/state => 8.1

It's like <prefix>/<name>/sensor/temperature (shelly) but with state at the end.

Is it possible to handle that ?

Thank in advance.

kpetremann commented 1 year ago

hello @zoic21,

Sure, I can try.

I am not familiar with esphome: is the prefix always the same? has it a default value?

I will also check the documentation.

kpetremann commented 1 year ago

note: I cannot simply use "/state" suffix in the topic to identify esphome message. This would cause collision with other zigbee2mqtt messages, using state as a type of value (for an ON/OFF button for instance).

zoic21 commented 1 year ago

Hello,

Yes it's always the same. Maybe to prevent issue with zigbee2mqtt you can add an env variable that indicates root topic of zigbee2mqtt?

Thank in advance

kpetremann commented 1 year ago

I agree, this was exactly my intent :)

I just needed to know if there was a default value. According esphome documentation, the default seems to be the node name. If the varenv is empty, it will not enter the esphome "parsing" function.

I'll push a commit for you to test tomorrow.

kpetremann commented 1 year ago

Hello @zoic21,

Please let me know if the code in the branch esphome works for you.

zoic21 commented 1 year ago

Hello,

Thank it's work but how can I specify multiple esphome root topic. In Esphome each device create a topic in root of mqtt. For example I have 6 devices, each device have a "root" topic, ex : esp-weather-indoor/esp-weather-outdoor/....

kpetremann commented 1 year ago

this was my original question "is the prefix always the same?" :)

two options here:

The second solution would be compatible with the first one.

WDYT?

zoic21 commented 1 year ago

Hello,

Sorry I don't undertand "is the prefix always the same?", I think suffix... Sorry

For me second option is better, we can choose the preffix so list will be perfect. But if you can also handle esp-* it will be better.

kpetremann commented 1 year ago

Hello, no problem :)

I've updated the branch.

The varenv changed: ESPHOME_TOPIC_PREFIXES This is now a list: prefixes are separated by a comma.

For each prefix in the list, mqtt-exporter will check if the topic is starting by the prefix string.

For your example:

 esp-weather-outdoor/sensor/temperature/state => 8.1
 esp-weather-indoor/sensor/temperature/state => 20.0

you can either set: ESPHOME_TOPIC_PREFIXES="esp-" or: ESPHOME_TOPIC_PREFIXES="esp-weather-outdoor/,esp-weather-indoor/" or: ESPHOME_TOPIC_PREFIXES="esp-weather-outdoor,esp-weather-indoor" etc...

Please let me know if it works for you

zoic21 commented 1 year ago

Hello,

Thank a lot !!!! it's perfectly works !!!! With that I can have all my mqtt data in grafana.

Thank a lot.

kpetremann commented 1 year ago

You are welcome :)

I'll merge that to the master branch later and it will be in the next release.

kpetremann commented 1 year ago

FYI it has been merged and officially released in the new v1.1.0.

Enjoy 🙂