ESP8266 Gas- and Water-Meter integration through MQTT

As I had serious trouble integrating my gas-meter and later the water-meter over MQTT I thought I could share how I got it working.

Preamble: I live in germany and my counters are the following

Gas:


Water:
ESP:

You have to take care of the ESP-Installation and adding the Mosquitto-MQTT integration to Homeassistant. This took me some time as a homeassitant beginner but I got it going.
Sadly i can not help you with the ESP side because I got some help there.

On the bottom line I have one ESP8266 board with ESP Easy Mega installed which has the gas sensor (a simple reed contact) and the same for my watermeter.

Both meters have a magnetic contact that counts. One rotation on the water-meter is “1 Litre” and one roation on the gas-meter is “10 Litres” - this is important later on for the template sensor.

There are multiple howtos for the creation of those hardware-sensors online and it would be too much for this quick howto.

First I created the sensor for the counter in Home-Assistant. The ESP is configured to send the values to my Mosquitto-MQTT-Integration and the topics it creates are

homeassistant/gaszaehler/gaszaehlerstand/total
and
homeassistant/gaszaehler/wasserzaehler/total

(I didnt think i would get a water meter. That is why I name it gaszaehler in the beginning and now have the awkward gaszaehler/wasserzaehler combination - but I was too lazy to put that straight)

First I created those counter sensors in the configuration.yaml


mqtt:
  sensor:
    - name: "gas_counter"
      state_topic: "homeassistant/gaszaehler/gaszaehlerstand/total"

    - name: "wasser_counter"
      state_topic: "homeassistant/gaszaehler/wasserzaehler/total"

Next I had to reformat that count with a template sensor so that I can integrate it into my energy-dashboard:

In the template.yaml I added the following:

- sensor:
    - name: "Gas Meter"
      device_class: gas
      state: "{{ states ('sensor.gas_counter') | float * 0.01}}"
      unit_of_measurement: "mÂł"
      icon: "mdi:gas"
      state_class: measurement
      state_class: total_increasing
      attributes:
        last_reset_value_template: "2022-12-23T06:43:36.740703+00:00"
- sensor:
    - name: "Wasser Meter"
      device_class: water
      state: "{{ states ('sensor.wasser_counter') | float * 0.001}}"
      unit_of_measurement: "mÂł"
      icon: "mdi:water"
      state_class: measurement
      state_class: total_increasing
      attributes:
        last_reset_value_template: "2022-12-23T06:43:36.740703+00:00"

These two sensors are then ready to be added to the energy dashboard without throwing errors and with the necessary information homeassistant to know about the statistics etc.

I hope that i can spare someone of you the time invested to try to figure things out with the sensor integration.

2 Likes