Gas meter tracking with counting in Home Assistant instead of on ESP32 side

Hi there,

I am currently tracking my gas consumption with an ESP32 and the setup described here: GitHub - svenwal/gaszaehler-home-assistant-esphome: Adding automated reading of your gas consumption to your Home Assistent setup with minimal hardware setup

I have currently the issue, that when the ESP32 is restarted, that the state of the gas count from before the restart is not restored from flash although I set it in the configuration. The state after a reboot is always set back to the initial value I set in the config.

This is annoying as my statistics in Home Assistant are showing wrong results after a reboot with a negativ amount of gas consumption. As far as I understand the configuration, the ESP32 always sends the total value of the gas meter and Home Assistant consumes this.

Would it be better, if the ESP32 only sends a pulse and the counting is done in Home Assistant? If this would be done via MQTT, it would be also work when there are networking issues as counting counting continues when everything is back.

Does anybody know if there is another setup based on this logic? I did not find one so far. Or do I miss something why such a setup wouldn’t be better?

Thanks in advance for your answer

Hi,
I have exactly the same issue and I bet many others also :wink:
Isn’t there any solution out there?
I would be very grateful, if someone has an idea.

Tranks and best regards,
Andreas

EDIT: Read one answer below before bothering with this stuff, Ben solved my stuff with his answer. If you still want an answer to the initial question, read on.

Hi

i posted a similar request today (the topic just before this one) and i might have a solution for you, solving my problem, too.

My gas meter sensor is a D1 mini (ESP6288) running Tasmota and i was using roughly this approach. Since i had the same problems like you, i altered the thing like this:

Tasmota Configuration: (try something similar in ESPhome probably)

So instead of configuring D7 as a counter like suggested in the link above i set it up as Switch. Also i set “SwitchMode 13” and “CounterDebounce 500” on Tasmota console. As a result Tasmota sends only “ON” statements via MQTT every time the reed sensor is activated, no “OFF” or other statements.
EDIT: “SwitchRetain 1” might be a good idea, too.

HA configuration:

I made a new helper counter (using UI) named gas_counter, no min/max, starting value 0, youcan use a different starting value of course.

The counter is increased by an automation fetching MQTT data:

automation:
  - id: '1701343698001' # change
    alias: Increment_Gas_Counter
    description: ''
    trigger:  
    - platform: mqtt
      topic: stat/tasmota_10EBDF/POWER # change to appropriate topic
      payload: "ON" #change to appropriate payload value
      condition: []
    action:
    - service: counter.increment
      data: {}
      target:
        entity_id: counter.gas_counter
    mode: single

To get a suitable entity for the energy dashboard i use this template:

template:
  - sensor:
    - name: gas_verbrauch
      state: | 
        {{(states('counter.gas_counter') | float(0) * 0.01)}}
      state_class: total
      device_class: gas
      unit_of_measurement: m³

Still testing it, but seems to work fine for now.

Do you have it defined as “total_increasing”? If so, HA should ignore negative adjustments and only consider when it’s increased. That’s how my utility values are setup and I’ve never had an issue when they reset after a power-cycle.