Question: Pulse meter

Guys, I’m struggling with two things to understand the pulse meter sensor:

1:
Does the optional total counter sensor only sum up all pulses for as long as the ESP is not being rebooted? On other words: Is the total counter being set back to zero when the ESP gets rebooted?

2:
The following example from the desciption website linked above does confuse me:

on_...:
  then:
    - pulse_meter.set_total_pulses:
        id: pulse_meter_id
        value: 12345

Is it required to set the

on_...:
  then:

part? If so, what value(s) does “on_...” accept?

You can store (and restore it on boot) it in the ESP itself in a global variable
The current value can be set from home assistant calling the service (e.g. from dev tools if you don’t want to automate it somehow)

esp8266:
  restore_from_flash: true

api:
  services:
    - service: heat_meter_counter_set_value
      variables:
        heat_meter_counter_value_new: int
      then:
        - pulse_meter.set_total_pulses:
            id: heat_meter
            value: !lambda 'return heat_meter_counter_value_new;'

esphome:
  on_boot:
    - pulse_meter.set_total_pulses:
        id: heat_meter
        value: !lambda "return id(heat_meter_counter_value);"

preferences:
  flash_write_interval: 0s   # only do this if the values does not change very often otherwise flash could wear out..

globals:
  - type: int
    id: heat_meter_counter_value
    restore_value: yes
1 Like

But be aware that pulse_meter is currently unreliable:

The fix is merged but probably will only be in 2023.1 or later.
In the mean time you can use the fixed version by including this:

external_components:
  - source: github://pr#4199
    components: [ pulse_meter ]

i had a simular issue and wan’t to store all measured values, not wait for 1 or more minutes to backup the value to the memory.
i developed a board with FRAM so i can write on every update to the fram. this external fram module doesn’t wear out as fast as an ESP8266.

I guess that would be a bit of overkill considering that you’d lose the pulses during reboot/update either way.
Just set it to a minute or so and use on_shutdown to store the current value.
The only difference would be that you might lose a few pulses if you have a sudden power loss…
If you are worried about wear on flash you could also just store it in HA and restore it from there during boot.

PR 4199 works fine, PR 4231 seams also be ok.

Thanks a bunch @k8n
So does this mean that the global variable is required to make the absolute counter values persistent over system ESP reboots, did I get this right?
I mean, in the end it does not really matter because HA does store its own history based on the pulses it gets from esphome, this makes it independent from the total counts of the ESP, right?

This is my sensor’s config, btw.:

# Water meter
# 1 pulse = 1 circle = 1 liter
sensor:
  - platform: pulse_meter
    pin: GPIO33
    name: "livewaterpulse"
    id: livewaterpulse
    unit_of_measurement: "L/min"
    accuracy_decimals: 1
    filters:
      - debounce: 300ms
    total:
      name: "totalwaterpulse"
      id: totalwaterpulse
      unit_of_measurement: "m³"
      device_class: water
      state_class: total_increasing
      accuracy_decimals: 3
      filters:
        - debounce: 300ms
        - multiply: 0.001

So does this mean that the global variable is required to make the absolute counter values persistent over system ESP reboots, did I get this right?

To my understanding, yes. It starts at 0 after reboot and you just have to restore the value from somewhere, either flash or network (HA) or some external memory like ageurtse does.