Impulse Counter for Water-Meter

Hi :slight_smile:

I want to use an ESP8266 for reading an Watermeter with an impulse Sensor.
For each 1 L of water, the sensor will send one pulse…

So far, I already have my configuration…

esphome:
  name: watermeter
  friendly_name: WaterMeter

esp8266:
  board: d1_mini
  restore_from_flash: true
  early_pin_init: false

# Enable Home Assistant API
api:
  encryption:
    key: *********

sensor:
  - platform: pulse_counter
    pin: GPIO13
    update_interval : 1s
    name: "water pulse meter cold"
    id: water_pulse_meter_cold
    device_class: water
    state_class: total_increasing
    total:
      unit_of_measurement: 'l'
      name: 'Water Meter Kaltwasser'
      filters:
        - multiply: 1  #(1 pulse per liter)
    
binary_sensor:
  - platform: gpio
    name: pulse_counter_eg_input
    id: meter_eg_reading
    internal: true
    pin:
      number: GPIO13
    on_press:
      then:
        - light.turn_on: meter_eg_reading_light
    on_release:
      then:
        - light.turn_off: meter_eg_reading_light

light:
  - platform: status_led
    id: internal_led_light
    name: "Internal LED Light"
    pin:
      number: GPIO2       # LED at Wifi module // // Also available on PIN D5
      inverted: true
  
  - platform: status_led
    id: meter_onboard_led
    name: "Meter-Onboard-LED"
    pin:
      number: GPIO14      # Onboard LED "SCK" // Also available on PIN D4
      inverted: false

  - platform: binary
    id: meter_eg_reading_light
    internal: true
    name: "MeterReading-LED"
    output: light_eg_output

output:
  - id: light_eg_output
    platform: gpio
    pin: GPIO0

captive_portal:

It is working well so far - BUT:
I would like to implement the sensor into the energy dashboard, and noticed the following:

  • Whenever the ESP is rebooting, the meter_counter will be reset to zero.
    How could I make the sensor value “persistent” in case the ESP does reboot, or similar?

Or doesn’t it matter if the sensor will be reset to zero?

Heya :slight_smile:

You could try and save the total of the pulse_counter to a global variable, with restore set to yes. You can then set the pulse_counter total on boot.

Haven’t tested this, but something along these lines:

esphome:
  name: watermeter
  friendly_name: WaterMeter
  on_boot:
    priority: 600
    then:
      - pulse_counter.set_total_pulses:
          id: water_pulse_meter_cold
          value: !lambda 'return id(pulse_counter_total);'
  on_shutdown:
    priority: 100
    then:
      - lambda: |-
          id(pulse_counter_total) = id(water_pulse_meter_total).state;

esp8266:
  board: d1_mini
  restore_from_flash: true
  early_pin_init: false

# Enable Home Assistant API
api:
  encryption:
    key: *********

preferences:
  flash_write_interval: 0s      # setting this to 0s means immediate writes to flash when the variable is changed.

globals:
  - id: pulse_counter_total
    type: int
    restore_value: yes
    initial_value: '0'

sensor:
  - platform: pulse_counter
    pin: GPIO13
    update_interval : 1s
    name: "water pulse meter cold"
    id: water_pulse_meter_cold
    device_class: water
    state_class: total_increasing
    total:
      id: water_pulse_meter_total
      unit_of_measurement: 'l'
      name: 'Water Meter Kaltwasser'
      filters:
        - multiply: 1  #(1 pulse per liter)

[...]

Besides that you can set up an interval or time on_time to save the state regularly, say every hour or so.

But, as you have the state_class set to total_increasing, it shouldn’t actually matter :slightly_smiling_face:

1 Like

I use this one

ok, an old version of it.
it also uses this global, but you don’t need to set it on boot as the lambda just returns the value.

thanks a lot :slight_smile:
I will give it a try with this.

I found a similar approach recently - but after testing, I have to say that it was really slow…
The values of the meter didn’t change “directly” after simulating the pulse… sometimes, it took several seconds and multiple pulses until the sensor value was updated in HA…

Therefore, I was not sure how reliable this approach will be in the end - but I will try it.

→ Sure, since I am using “total_increasing” as state class, HA will take this into account and measure the values accordingly… maybe, I will create another sensor / helper within HA to make a persistent counter (to keep track of the total count)