Pulse Counter is miscounting compared to Binary Sensor

I used an ESP and a TCRT5000 to build a counter for my Ferraris electricity meter. Unfortunately, the pulse counter always miscounts and the results are incorrect. I have created a binary sensor on the same PIN and the two give different results. Here is a log:

[13:58:37][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:58:37][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:58:42][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:58:42][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:58:48][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:58:48][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:58:53][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:58:53][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:58:58][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:58:58][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:04][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:04][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:09][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:09][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:14][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:14][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:20][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:20][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:25][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:25][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:30][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:30][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:33][D][pulse_counter:174]: 'powermeter_pulse': Retrieved counter: 9.00 pulses/min
[13:59:33][D][sensor:094]: 'powermeter_pulse': Sending state 9.00000 pulses/min with 2 decimals of accuracy
[13:59:33][D][pulse_counter:180]: 'powermeter_pulse': Total : 27 pulses
[13:59:33][W][component:237]: Component pulse_counter.sensor took a long time for an operation (77 ms).
[13:59:33][W][component:238]: Components should block for at most 30 ms.
[13:59:36][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:36][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:41][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:41][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:46][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:46][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:52][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:52][D][binary_sensor:036]: 'Pulse': Sending state OFF
[13:59:57][D][binary_sensor:036]: 'Pulse': Sending state ON
[13:59:57][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:02][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:02][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:07][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:08][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:13][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:13][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:18][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:18][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:24][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:24][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:29][D][binary_sensor:036]: 'Pulse': Sending state ON
[14:00:29][D][binary_sensor:036]: 'Pulse': Sending state OFF
[14:00:33][D][pulse_counter:174]: 'powermeter_pulse': Retrieved counter: 10.00 pulses/min
[14:00:33][D][sensor:094]: 'powermeter_pulse': Sending state 10.00000 pulses/min with 2 decimals of accuracy
[14:00:33][D][pulse_counter:180]: 'powermeter_pulse': Total : 37 pulses
[14:00:33][W][component:237]: Component pulse_counter.sensor took a long time for an operation (79 ms).
[14:00:33][W][component:238]: Components should block for at most 30 ms.

In the first minute, the Binary Sensor Pulse 11x switches from ON to OFF and back again. The pulse_counter counts 9x. In the second minute there are 11 changes. The pulse_counter counts 10. How can this be?

I’m afraid it has something to do with the internal filter. However, I have huge numbers for the pulse counter when I remove the filter.

Here is my config:

binary_sensor:
  - platform: gpio
    name: "Pulse"
    pin: 
      number: GPIO15
      allow_other_uses: true

sensor:
  - platform: pulse_counter
    pin: 
      number: GPIO15
      allow_other_uses: true
    id: powermeter_pulse
    internal_filter: 20ms
    use_pcnt: false
    update_interval: 60s
    count_mode: 
      falling_edge: INCREMENT
      rising_edge: DISABLE
    total:
      id: powermeter_total
  - platform: copy
    source_id: powermeter_pulse
    unit_of_measurement: 'W'
    device_class: power
    state_class: measurement
    name: 'Leistung'
    accuracy_decimals: 0
    filters:
      - multiply: 800  # (60s/75 pulses per kWh * 1000 = W)
  - platform: copy
    source_id: powermeter_total
    unit_of_measurement: 'kWh'
    device_class: energy
    state_class: total_increasing
    name: 'Energie (Total)'
    accuracy_decimals: 3
    filters:
      - multiply: 0.01333333 # (1/75 pulses per kWh)

  # Used for Testing and Validation Purposes
  - platform: copy
    source_id: powermeter_pulse
    name: "Pulse-Counter Raw"
  - platform: copy
    source_id: powermeter_total
    name: "Pulse-Counter Total Raw"
  - platform: uptime
    name: Uptime

As far as I know there is a known issue with the pulse meter for about a year now; pulse_meter seems to have broken with 2023.8.0 · Issue #4807 · esphome/issues · GitHub

A PR with the fix is open since mid-may. In the issue you can find how to use the PR code on your device. Make sure to leave a comment in that thread if this PR fixes (or not) the issue you’re observing.

Ah, thank you very much. The problem does indeed look similar. However, the pull request only seems to fix the problem for pulse_meter, but not for pulse_counter.

I have now observed a section of about 2 hours. While I was able to observe 291 flips with the binary_sensor, the pulse_counter counted 310. That’s quite a difference. The pulse_counter count can be manipulated by adjusting the internal_filter - then I get different values here. I don’t have to configure anything for the binary_sensor and the value seems more plausible than that of the pulse_counter.

Does the binary_sensor use any default values for filters? Does anyone have any idea how I can get it to count the same as the flips on the binary_sensor? Or can I somehow use the binary_sensor as input in the pulse_counter?

If you read the documentation it explains pulse counter and pulse meter. It also explains the difference between how each one counts because they do it differently.

You really just need to calibrate one of the methods with your physical sensor. What are you hoping to accomplish by synchronizing pulse_meter with a binary_sensor which are counting 2 different values and shouldn’t match “counts” to begin with.

My config does not use a pulse_meter - I compare a pulse_counter with a binary_sensor. I only inserted the binary_sensor for experimental purposes, as the pulse_counter gave me values that seemed incorrect to me.

The aim is to be able to count how often the TCRT5000 switches. The binary_sensor then switches once to ON and directly back to OFF. The pulse_counter should count one pulse.

The interesting thing in my case is that the flips between On/Off correspond to reality with the binary_sensor and are apparently correct - while the pulse_counter deviates.

I had assumed that a pulse_counter could be configured to count these flips, but I couldn’t find any references to different counting methods in the documentation. Can you explain them?