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