Track a gas meter with ESPHome

I did, at least for my setup.

The issue is that the counter is susceptible to false counts as an effect of other electrical devices kicking in, in your house, and causing some effect on the ESP power supply.

I’ve tested 4 different 5V power supplies, all connected to the same socket in my kitchen, then to another socket also in the kitchen. Checked 3 different ESP-WROOM-32 boards as well. The external reed switch was disconnected, but when my oven, dishwasher pump or kettle was switched on, the counter would increment a few times.

When I switched to a power bank for the 5V, the issue went away completely and after 48h my gas usage was exactly correct down to 0.001m3. Ergo, the power supply is to blame for all the issue I and probably at least some of you are having.

Not sure how, how to fix it for AC powered setups, but since I am moving to a solar array for this one, I am not investing more time, which I already did an insane amount to get it resolved.

Hello!
I don’t know if this is still helpful. I had a similar problem with the inaccurate values. I have now found a solution that is sufficient for my needs. My goal is to track the gas consumption as a whole, i.e. total gas.
My setup:
ESP D1 mini, reed contact with AD converter (like this : Reed Switch).
The solution with pulse_counter and pulse_meter did not work satisfactorily.
My approach with binary_sensor and a template:

binary_sensor:
  - platform: gpio
    id: internal_pulse_counter
    pin: D5
    internal: true
    filters:
      - delayed_on: 100ms
    on_press:
      then:
        - lambda: id(total_pulses) += 1;
sensor:
  - platform: template
    name: "total_gas"
    device_class: gas
    unit_of_measurement: "m³"
    state_class: "total_increasing"
    icon: "mdi:fire"
    accuracy_decimals: 2
    lambda: |-
        return id(total_pulses) * 0.01;
globals:
  - id: total_pulses
    type: int
    initial_value: '0'

3 Likes

@navierstokes could it be that you missed the “sensor” component entry for this?
(as a binary sensor it doies not compile)

1 Like

Exactly, sorry for the confusion. I have corrected this.

This is my yaml, works great for the total amount consumed.
But I’m still struggling with the flow rate

sensor:
  - platform: pulse_meter
    name: "${device_friendly_name} Gasverbrauch"
    id: ${device_entity_id}_gasverbrauch
    unit_of_measurement: 'm³/min'
    internal_filter: 3 min # the flow rate would never get so fast that the pulse meter would get more than 1 pulse in 3 minutes (in my case)
    timeout: 10 min # after 10 min, the pulse meter assumes 0 m³/min
    state_class: measurement
    device_class: gas
    icon: mdi:meter-gas
    #accuracy_decimals: 3
    pin:
      number: $gas_read_pin # GPIO5 on my D1 mini pro
      mode: INPUT_PULLUP # reed contact connects pin to ground
    filters: # I'm still struggling to get the flow rate right 😬
       - lambda: return x * (1.0 / ${gas_imp_value});
    total: # this sensor tracks the total gas consumed in m³
      name: "${device_friendly_name} Gasverbrauch Total"
      id: ${device_entity_id}_gasverbrauch_total
      unit_of_measurement: 'm³'
      icon: mdi:meter-gas-outline
      state_class: total_increasing
      device_class: gas
      accuracy_decimals: 1
      filters: # 1 imp / 0.1 m³ = 10 imp / m³ (in my case); gas_imp_value = 10;
        - lambda: return x * (1.0 / ${gas_imp_value});
      on_value: # I have a led that blinks for every impulse 
        then:
          - script.execute: gas_led_blink

Not working for me.

At the end i adjusted the code again, so it works perfectly for me now, to be honest I don’t know if the flow rate is correct, because it is generally very low with gas.

I’ve added the parameter “inverted” to the pin-configuration and changed how the code keeps track of the impulses.

substitutions:
  # Device informations
  device_friendly_name: "Keller Zähler Gas"
  device_name: "keller-zaehler-gas"
  device_entity_id: "keller_zaehler_gas"
  # Pinout
  gas_read_pin: GPIO5
  # Gas Meter Settings
  gas_imp_value: '10' # impulses / m³ 
  gas_imp_debounce_on: '0.01 s'
  gas_imp_debounce_off: '0.1 s'

globals:
  - id: gas_impulses
    type: int
    restore_value: true # if set to false, the value will be 0 at reboot
    initial_value: '0'

binary_sensor:
  - platform: gpio
    id: gas_impulse
    internal: true
    pin:
      number: $gas_read_pin
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_off: $gas_imp_debounce_off
    on_press:
      then:
        - lambda: id(gas_impulses) += 1;

sensor:
  - platform: pulse_meter
    name: "${device_friendly_name} Gasverbrauch"
    id: ${device_entity_id}_gasverbrauch
    unit_of_measurement: 'm³/min'
    internal_filter: $gas_imp_debounce_on
    timeout: 1 min
    state_class: measurement
    device_class: gas
    icon: mdi:meter-gas
    #accuracy_decimals: 3
    pin:
      number: $gas_read_pin
      mode: INPUT_PULLUP
      inverted: true
    filters: # 1 imp / 0.1 m³ = 10 imp / m³ (in my case); gas_imp_value = 10;
      - lambda: return x * (1.0 / ${gas_imp_value});
  - platform: template
    name: "${device_friendly_name} Gasverbrauch Total"
    id: ${device_entity_id}_gasverbrauch_total
    unit_of_measurement: 'm³'
    icon: mdi:meter-gas-outline
    state_class: total_increasing
    device_class: gas
    accuracy_decimals: 2
    lambda: return id(gas_impulses) * (1.0 / ${gas_imp_value});
    update_interval: 10 s

1 Like

Hallo Callen!

A little help is appreciated :slight_smile: Where can I set the initial total verbrauch as a starting point.
(In grafana I can group verbrauch in Weekly, monthly, year aspect → it’s clear)

Thanks, Stefan

hey,
you can put an global with an initial value.
I did this with my gas meter for 327,63 m³

globals:Preformatted text
  - id: total_pulses
    type: int
    restore_value: false
    initial_value: '32763'
...
  - platform: template
    name: "total_gas_1"
    device_class: gas
    unit_of_measurement: "m³"
    state_class: total_increasing
    icon: "mdi:fire"
    accuracy_decimals: 2
    lambda: |-
        return id(total_pulses) * 0.01;