How to count pulse frequency accurately with ESPHome?

My supplier (ActewAGL in Aus) provides in the downloadable quarterly data that has ‘Usage in m³’, ‘Average Heating Value’ and ‘Pressure Factor’.

The value of the ‘Average Heating Value’ and ‘Pressure Factor’ are different for each billing period but are very close, for example for the last to bills I get this:

Average Heating Value: 38.06 and Pressure Factor: 0.9612
Average Heating Value: 37.63 and Pressure Factor: 0.9951

These items multiplied give a ‘Consumption’ in Mj and as I am charged per Mj I have no need to convert to kWh.

My meter has a rotating disk with a white line that I am detecting and that I assumed is the same as the least significant digit of the meter so I am dividing by 1000. I probably need to confirm that.

Gaz

Yep that’s probably all you need to do. In the end you can set the sensor up to report in whatever units are most meaningful (would be kW for me as that’s how gas is billed in the UK, sounds like MJ might be best for you).

Hi, can someone share their wiring?I have this electricity meter which has built-in pulse counter terminal.
Previously I was using raspberry pi with custom made python script and 10k resistor, but that does not seem to work with esp32/esphome neither with pulse_counter nor with pulse_meter.

Thanks !

Is It a relay or semiconductor?
Try swapping the wires in the terminals.

How exactly you have used the resistor?

how do I tell? Tried swapping, did not seem to help. The problem that i managed to tear the wiring going to RPI, which was done 5 years ago so I cannot remember how it was connected to RPI - if it was between ground and pin or +5v and pin …

Try searching from the manual. The pins have + and - symbols so made me wondering.

If it is a relay, point is not to leave input pin floating. So use a resistor to tie it to ground. Then connect the voltage through the relay. Check also that the relay is potential free.

Alternatively you can use pull high resistor to 3.3v and switch ground through your meter to the esp input pin.

Here is some random article with the principal explained.

Most esp boards can tolerate 5v even the logic level is 3.3v. Not sure if raspberry does.

ok, thanks. Still monitoring if it is correct, but seems to have worked. Used GPIO26 with INPUT_PULLDOWN connected to - pin and +5 to a + pin.

Some random spikes 1-2 a day, but else seems to be good.

Try turning off the WiFi power save. I have a fade memory I had similar spikes and that helped.

If someone comes across this thread, I thought I would put an update about what I came to realize regarding the false counts and random spikes - It’s your AC power lines - or at least it was for me.

The issue is that the counter is susceptible to false counts as an effect of other electrical devices kicking in, in your house, and causing some effect on the ESP power supply.

I’ve tested 4 different 5V power supplies, all connected to the same socket in my kitchen, then to another socket also in the kitchen. Checked 3 different ESP-WROOM-32 boards as well. The external reed switch was disconnected, but when my oven, dishwasher pump or kettle was switched on, the counter would increment a few times.

When I switched to a power bank for the 5V, the issue went away completely and after 48h my gas usage was exactly correct down to 0.001m3. Ergo, the power supply is to blame for all the issue I and probably at least some of you are having. This said, it can’t be just a faulty power supply I have since I tested this many, from different brands from 1$ adapters to 60$ 120W laptop grade chargers.

Not sure how it’s happening, how to fix it for AC powered setups, but since I am moving to a solar array for this one, I am not investing more time, which I already did an insane amount to get it resolved.

2 Likes

Hi!

I would like to limit the amount of data that my 2 power meters write to the HA database (and as a result of that, to InfluxDB). This is what I use at the moment:

sensor:
  - platform: pulse_meter
    id: ${eleffekt_sensor_id}
    name: ${eleffekt_sensor_name}
    unit_of_measurement: W
    state_class: measurement
    device_class: power
    accuracy_decimals: 0
    icon: mdi:flash-outline
    pin: D5
    internal_filter: 100ms
    on_value:
      then:
        - light.turn_on:
            id: led_blue
        - delay: 0.2s
        - light.turn_off:
            id: led_blue
    filters:
      # multiply value = (60 / imp value) * 1000
      # - multiply: 60
      - lambda: return x * ((60.0 / ${pulse_rate}) * 1000.0);
    total:
      name: ${energi_sensor_name}
      unit_of_measurement: kWh
      icon: mdi:circle-slice-3
      state_class: total_increasing
      device_class: energy
      accuracy_decimals: 3
      filters:
        # multiply value = 1 / imp value
        # - multiply: 0.001
        - lambda: return x * (1.0 / ${pulse_rate});
        
        # Only report changes every 0.1 kWh
        - delta: 0.1

So I have started by adding the - delta: 0.1 filter to the total sensor (kWh), and this is a good start. But what about the W sensor, can I add a throttle or something similar without affecting the totals? :thinking: I would like to limit to something like once every 5 seconds or so.

Is there something similar to pulse_meter in home-assistant? I have a HVAC that only reports 0.1kWh increments in energy consumption, and power must be derived. However all my attempts using filters, templates, and sensors fail to result in any useful information.

@stevebaxter Dear Steve, your code is cool and works as a charm. This has completely solved a common issue, my compliments to you. Ciao

My ESP32 photoresistor pulse_counter / pulse_meter measurements are all over the place. Adjusting the resistor seems to only adjust the multitude of the error. I get anything between 50MW and 0kW, and I can’t find the accurate position to give correct measurements. What could be the cause? I’m using a wroom-32U board. I’ve tried with two different boards and photoresistor-boards.

Photoresistor board:

Here’s what happened when I changed pulse_counter to pulse_meter at 09:59. Nothing else changed. pulse_meter gives single spikes every now and then:

thanks for the idea. Now, a few months later, you still feel/believe it’s the power supply causing the issue?
Just wondering, as I also see spikes on all my 3 pulse_meters (water, gas and electricity). I’ll attach it to a powerbank too, just to verify.
Anybody else tried the powerbank method in the meantime?

@stevebaxter great improvement on the pulse counter!

Quite a basic setup here: 1000 imp/kWh, comsumption around 6 kWh. This generates a lot of traffic, which is reduced by using the ‘throttle’ parameter. It is, however, quite addictive to see the actual whole house realtime power consumption.

So my question: can the ‘throttle’ parameter use a variable (e.g. a binary sensor value) from the HAOS in such a way that the realtime sending of data is used only whenever needed. Normally the values would be sent e.g. once a minute, in ‘realtime’ mode continuously.

Possibly, but that’s not really part of the sensor, it’s part of what ESPHome can do. One question though, why do you care? We use about 10kWh a day with a similar setup, been running for over a year and no problems with the amount of data.

I mean 6 kW in an hour, so much more per day (and more traffic). Might not be that critical, but noticed the CPU temp to rise around 10 %, and processor use roughly from 2 % → 5 %.

Anyway, using ‘pulse_meter’ is so much more accurate than pulse_counter, enjoying it a lot!

Hi,
i need a very accurate pulse_meter. What do i have to do to remove the conversion in ms in .cpp and just show the raw microseconds for the pulse ?
I would need it for measuring the tick of a mechanical clock. Therefor accuracy has to be in us-range.
I run ESPhome on a Raspi 4
regards, Peter

Hey @stevebaxter,

thanks for writing a better pulse sensor!

I try to measure the grid ac frequency with it.

I wired up an analog voltage converter to a digital pin (on a ESP32) and used a pulse_counter like this:

  - platform: pulse_counter
    pin: GPIO23
    name: "Grid Frequency"
    unit_of_measurement: "Hz"
    accuracy_decimals: 5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: INCREMENT
    #device_class: "frequency"
    state_class: "measurement"
    filters:
      - lambda: return x / 120.0;
    update_interval: 1s

It gave me something around 50 Hz, but with way way too little accuracy due to alignment issues with the update_interval to the grid frequency.

So I gave your new module a shot, but it doesn’t seem to work right for me:

sensor:
  - platform: pulse_meter
    pin: GPIO23
    name: "Pulse Meter"
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 5000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:58][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 5000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:42:59][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3000.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 8571.42871 pulses/min with 2 decimals of accuracy
[11:43:00][D][sensor:125]: 'Pulse Meter': Sending state 3157.89478 pulses/min with 2 decimals of accuracy

I expected it to measure the time between each rising edge, but it seems (just a guess) to measure between each pulse - so depending on the pulse-width (the time the pin is HIGH) you have different results.

What’s particularly strange are the 0 values.

Would be neat if you could help :slight_smile:

I using pulse meter for Anemometer and I am also picking up 0 values and variable results. I am switching to ESP32, better power supply, nothing else running and play around with pull up resistors. Lets see if I can get to the bottom of what is happening