Gas usage through energy dashboard

I’m trying to track my gas usage and already and am already quite far down the rabbit hole. The detection is done with a Wemos D1 mini and a reed switch module attached to my meter. In ESPHome I have the following config.

sensor:
  - platform: pulse_counter
    pin: D7
    count_mode:
      falling_edge: INCREMENT
      rising_edge: DISABLE
    internal_filter: 100ms
    unit_of_measurement: 'm³'
    name: 'Gas Meter'
    filters:
      - multiply: 0.01

Which gives me a perfect representation of minute to minute gas usage in m³

image

However to get the energy dashboard to see the gas usage I had to create a new template sensor to add the state_class and device_class

      gas_meter_m3:
        unit_of_measurement: "m³"
        device_class: "gas"
        value_template: "{{ states.sensor.gas_meter.state | float}}"
        attribute_templates:
            state_class: total_increasing

Now here I already got a feeling that it wouldn’t work correctly because this isn’t an increasing sensor but basically a copy of the original sensor with the “correct” attributes.

This was confirmed by the energy dashboard. If you see the first full hour of tracking (9:00) we get an output of 0.64 m³

But, if I summarize the individual values from an sqlite export I get 0.86 m³
I can’t figure out what’s going on in the background but I suspect that the energy dashboard expects a constantly increasing state. And I’m also not sure how to solve it.

Options :

  • Add total to the pulse_counter in ESPHome but then I lose the total after a restart. I see that pulse_meter allows to set the total but I don’t see this option for pulse_counter

  • Make some kind of constantly increasing sensor in home assistant, but I don’t know how. Edit : The following template seems to work to create an increasing sensor. Let’s see what the dashboard shows

  - trigger:
    - platform: state
      entity_id:
        - sensor.gas_meter
    sensor:
      - name: gas_meter_m3
        unit_of_measurement: "m³"
        device_class: "gas"
        state_class: total_increasing
        state: "{{ states.sensor.gas_meter.state | default(0) | float + states.sensor.gas_meter_m3.state | float }}"

Only problem is that it seems it’s not persistent when I reload template entities.
image

  • play with utility_meters config but I don’t know how this relates/interacts with the energy management dashboard.

Any help to get me further would be much appreciated.

PS : In the mean time I think I found out how the energy management dashboard work. I found the short term stats in the DB and I see that every 5 minutes it adds the current state, so it seems the 4 minutes in between are not counted.

Have a look at this integration