How to use ESPHome "pulse_meter" sensor?

I have one of these old power measurement devices installed in my house:


and want to integrate that into mit HA. There are tons of tutorials out there, but they all seem different in how to configure the service.
What i did is to build a sensor using an reflective sensor shield and mount it that way to get a LOW pulse everytime the inked part of the turning wheel passes that by.

Mounting that was pretty fiddly, and i think it would be much easier and get better results if i did not took a “binary sensor”, where the digital pulse is made of an OpAmp comparator, but to feed the analog voltage into an A/D port of the ESP, but finally i got good looking LOW pulse every turn. The wheel turns 75 times per kWh power used. The sensor is connected to pin “D5”

Now, how to configure the ESP8266 D1 Mini (already adopted) to deal with that data and provide good dashboards?

Should i use pulse_meter or pulse_sensor oder something else? How to setup the params to get a total power used per time period (i’d like to have 1 day, 1 month and total since start of service)

Everything i tried ends with a messy value or no value…

My last setup looks like this:

sensor:
  - platform: pulse_meter
    unit_of_measurement: 'kW'
    #device_class: 'power'
    #state_class: 'measurement'
    name: 'Stromzähler'
    pin: GPIO5 # D1
    filters:
      - multiply: 800 # (60s / 75 pulses per kWh) * 1000 W
      - filter_out: nan
    internal_filter: 250ms
    accuracy_decimals: 3
    total:
      name: "Total"
      unit_of_measurement: 'kWh'
      accuracy_decimals: 3
      filters:
        - multiply: 0.001

It reports:

INFO Disconnected from ESPHome API for esphome-web-da8c49.local
WARNING Disconnected from API
INFO Successfully connected to esphome-web-da8c49.local
[20:19:33][D][sensor:126]: 'Stromzähler': Sending state 128.36627 kW with 3 decimals of accuracy
[20:19:33][D][sensor:126]: 'Total': Sending state 0.00200 kWh with 3 decimals of accuracy
[20:24:34][D][pulse_meter:061]: No pulse detected for 300s, assuming 0 pulses/min
[20:24:34][D][sensor:126]: 'Stromzähler': Sending state 0.00000 kW with 3 decimals of accuracy

I really don’t get how this pulse_meter should work…

In the end i’d like to have these graphs and counters:

  1. A graph showing the power usage in kWh h in a one minute resolution during the day (24 hours)
  2. A graph showing the total energy consumed per day (365 days)
  3. A graph showing the total energy consumed per month (respect the calender)

To get the graph 1.) i need to map from pulses/minute coming from the energy meter wheel sensor, into kWh/minute.

The docs tells that “pulse_meter” sensor values are seen as “pulses per minute”. That would mean, one minute is the minimal resolution of the software sensor, right?
So if my electrical sensor would give 1 pulse in a minute, it would put the value 1 into the graph for this minute, if there where 2 pulses it would put value 2 and so on?

Now, the plate on my power meter tells me that if the wheel turned 75 times, electrical work of 1 kWh (1000 Wh) has been consumed, no matter how long it took to turn 75 times. So it generates 75 electrical pulses per kWh.

I built something for the same power meter type, using the approach documented in Tracking gas usage with ESPHome, Home Assistant, and TCRT5000 | Savjee.be (same sensor and code logic, just for the power meter instead of the gas meter).
I tried the pulse_meter as well, but I never got it to run reliably. The new approach works good, for 2 months now.

My first issue was that the reflective sensor (TCRT5000 line tracer shield) could not clearly distinguish between the wheel and it’s colored marking. This shield works by sending constant IR light through one LED and detects it using the IR light-sensor, so it’s basically an analog value coming out of it. To get a digital pulse, there is a LM339 OpAmp used as comparator. Through a potentiometer you set the level of detected IR light to be seen as logical “0” or “1”. So it’s really cheap:
b
The problem with this sensor is, that it emitts IR light in an 90° angle around the LED, which get’s reflected by the glass of the power meter. To focus it on the wheel-marking, somebody in the web told a trick to remove the plastic housing and put small pipes (e.g. shrink tubes) onto the sending and receiving LED. This way it works much better and more sensitive!

I watched the signal coming from the sensor using an DSO, and it looks good so far:


The rising and falling edge is a bit unstable of couse, it needs about 2,5ms to stabilize on both:

I tried a lot and found that neither “pulse_meter” nor “pulse_counter” give me good results, so i tried “binary_sensor” with an internal ID storage.

globals:
  - id: total_pulses
    type: int
    restore_value: false
    initial_value: '784427'  # initial value, read from power meter and divided by 0.075

binary_sensor:
  - platform: gpio
    name: "Power meter wheel marking pulse"
    id: internal_pulse_counter
    pin:
      number: GPIO5 # "D1"
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 500ms
    on_press:
      then:
        - lambda: id(total_pulses) += 1;

sensor:
  - platform: template
    name: "Total power consumed"
    device_class: power
    unit_of_measurement: "kWh"
    state_class: "total_increasing"
    icon: "mdi:power"
    accuracy_decimals: 0
    lambda: |-
      return id(total_pulses) * 0.075;

I used a high threshold of 500ms because the wheels turns slow and i like to absolutely avoid wrong pulses.