How to count pulse frequency accurately with ESPHome?

Hey at all.
I want to say thank you @stevebaxter too. Really a great tool.

@teddy have you already found a way to enter the actual counter reading and that counting does not start at 0? @stevebaxter is there a possibility there?

@Kasper027 : I solved it in hassio using a template sensor which adds the sensor value from pulse_meter and an input_number which I use to enter/correct my actual counter reading.

1 Like

I now have Esphome running on my Shelly Uni for water pressure and water usage. On the pulse meter page it says that you can use on_… to enter the actual water meter reading for addition.

Unfortunately, then my yaml is not correct. Must after on_… also something be entered?

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

Thanks for the great sensor. The update rate is awesome in comparison to the counter.
I’m experiencing something weird and did start a separate thread for my issue but this looks like the right place to be. I was wondering if anyone has experienced the Pulse Meter Sensor measuring pretty much exactly double that of the Pulse Counter Sensor. I’m using it as a power meter and the pulse counter is displaying power accurately but when I use pulse meter is reads double the power usage.

I have come up with a way to read my gas meter with a reflective photosensor to detect the white mark on an indicator wheel on the meter and so far the readings seem to very stable.

My gas charges are per Mj and the bill has additional info including ‘heating value’ and ‘pressure factor’ and I would like to convert the reading from the pulse meter sensor to Mj instead of kW.

Can someone explain the liters to kW formula in the gas meter example above so I can attempt to modify it for my usage please:

      # Convert litres (one pulse) to kWH/min using:
      #
      # 1.022640 * 39.2 / 3.6 / 1000 = 0.01113541333
      #
      # Then kWH/min to kW by multiplying by 60 = 0.66812

Gaz

I’ve been battling the same problem a few days ago.

Here is what I found out here or here, and confirmed with @stevebaxter , to be what he meant in his formula.

When you have your corrected number of pulses, you do the following:

Multiply by volume correction factor (1.022640)
Multiply by calorific value of the gas (39.3) - may be different in your country/region
Divide by kWh conversion factor (3.6)
Divide by 100 for converting from 1m3 to 0.01m3 (which is one pulse worth of gas for my meter).

If combined as discussed above, you get very close to what my provider is claiming = 11.312 KWh/m3. In my case, the difference is the slightly different calorific value of the gas.

My final sensor layout below. I am still fighting with a slight overestimation, but this is a matter of getting to the internal_filter sweetspot.
Long enough to deal with debounce and short enough to prevent loss of true counts.

sensor:
- platform: pulse_meter
  id: gas_pulse_meter
  pin:
    number: 13
    mode: INPUT_PULLUP
  internal_filter: 2s
  timeout: 30 sec
  unit_of_measurement: 'kW'
  name: 'Incoming gas power'
  device_class: power
  state_class: measurement
  accuracy_decimals: 2
  filters:
      # Convert 0.01 m³ (one pulse) to kWH/min using:
      #
      # 1.022640 * 39.2 / 3.6 / 100 = 0.1113541333
      # My conversion factor = 0.11312
      #
      # Then kWH/min to kW by multiplying by 60 = 6.7872
   - multiply: 6.7872
  total:
    name: "Gas consumption"
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total_increasing
    accuracy_decimals: 2
    icon: mdi:gauge
    filters:
      - multiply: 0.01

What I don’t get is why the original formula divides by 1000 as opposed to 100.
We are going from 1m3 to 0.01m3, which is 1/100th. What am I misunderstanding here?

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?