Setup pulse meter or pulse counter

pulse_counter is better for dashboard, because it gives you the correct energy.
This is not the case for pulse_meter as this one assumes further energy consumption after turning the device off - for a specified time. And this assumed consumption, which is not real, gives you an error on the energy.
You don’t have this problem witch pulse_counter.

1 Like

Thanks, then I go with pulse_counter for the heating pump. This can just be on/off.

Did not work

Get no pulse:

[16:45:50][D][sensor:090]: ‘Power’: Sending state 0.00000 kW with 2 decimals of accuracy
[16:45:50][D][pulse_counter:150]: ‘Power’: Total : 0 pulses
[16:45:50][D][sensor:090]: ‘Energy’: Sending state 0.000 kWh with 3 decimals of accuracy

This is my code:

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

sensor:
  - platform: pulse_counter
    pin: GPIO5 #D5
    name: 'Power'
    id: power
    unit_of_measurement: 'kW'
    state_class: measurement
    device_class: power
    filters:
      - multiply: 0.6 # (60 minutes per hour / 100 pulses per kWh)
    total:
      name: 'Energy'
      id: energy
      unit_of_measurement: 'kWh'
      state_class: total_increasing
      device_class: energy
      accuracy_decimals: 3
      filters:
        - multiply: 0.01

Pinout:

and

What could I try?

I tried a new D1 mini board with pin D4 and impulses now seem to be coming in :slight_smile:
The values or the conversion into kwh seem to be wrong (around factor 10 to much).
I reset the recorder again and pulse counter again. Then I have tomorrow the right values.

In the meantime maybe a question if I have 100 pulses per kwh.

Aren’t the values ​​0.6 and 0.01 double converted now?
@thusassistint can you explain the value(s)?

I understand 0.01:
1 / 100 pulses = 0.01
1 / 1000 pulses = 0.001

But is 0.06 not right got both?

Initially I thought that as well: I initially assumed that the total sensor would use the output from the pulse-counter sensor as input.
But that appears not to be the case: the total sensor uses the same input as the pulse_counter sensor, so it counts the raw pulses and not the converted output.

By default the pulse_counter measures the power using pulses/minute.
Your meter gives 100 pulses per kWh.
Let’s assume that the pulse_counter measures 1 pulse / minute.
Then this is 1/100 kWh / minute in your case.
And that is 1/100 * 60 kWh / hour = 0.6 kWh / h = 0.6 kW.
So the multiply factor for the pulse_counter should be 0.6 in your case to measure power in kW with 100 pulses per kWh.

The total sensor by default just counts the total number of accumulated pulses.
So if the total amount of pulses for instance is 10000 this is 10000/100 = 100 kWh in your case.
So you have to use a multiply factor of 0.01 for the total sensor to convert the total number of pulses to kWh in your case.

Does this make sense?

1 Like

Ok interesting, but I see my on meter I have 0,2 kwh and the sensor has 2480 kwh. So something went wrong. Let’s try 1000 pulses per kwh. On the meter I see 100 per kwh, but on the documentation it’s 1000 :open_mouth:.

So next try I put: 0.06 and 0.001

@thusassistint and @der-optimist thanks a bunch, it works. Also the Energy Dashboard works with daily values.

One more question:
What is the best way to build a daily consumption sensor? Can this be done directly in the ESP?

I use utility meter for this:

Never tried to do this in ESP

That’s clear, but what do I use for the source? The total increase sensor?

Yes that is the question :smiley: but I just try it with a “normal” util meter

Hey,
I didn’t read all responses but as I had a related issue some time ago…

My use case was a bit more complicated and posed a bit of a challenge.
It is my personal believe that the esphome implementations pulse_meter as well as pulse_counter are flawed, i.e. limited in functionality and very badly documented. Someone should unify them into one consistent pulse measurement component.

Anyhow, I ended up building what I needed by myself, which is quite easy. @MelleD maybe consider that:

1 Like

The pulse counter looks good so far ( ok since 2 two days :slight_smile: ). Let’s see what it looks like after a month.

I think I also need a daily util meter like in your post. I’ll try that.

Works quite well so far and the values ​​are accurate.

However, I still have the problem that as soon as an ESP home update comes in and I install it, the total sensor always jumps to 0 after the update. Can this be prevented?

What I did with my water meter is adding a global variable in which the total value is stored on shutdown and at an interval, and which value is restored at startup.
Something like this:

esphome:
  .....
  on_boot:
    then:
      - pulse_counter.set_total_pulses:
          id: power
          value: !lambda 'return id(global_total);'
  on_shutdown:
    then:
      - globals.set:
          id: global_total
          value: !lambda 'return id(total_increasing).state;'

interval:
  - interval: 60s
    then:
      - globals.set:
          id: global_total
          value: !lambda 'return id(total_increasing).state;'

globals:
  - id: global_total
    type: int
    restore_value: yes

I added the interval to ensure that the latest value is stored in the case of an inadvertent reboot, like at an power outage.

1 Like

Not myself but a friend of mine did something similar via Home Assistant. Advantages:

  1. No memory wear
  2. Ability to set/reset/adjust the absolute value through an input option in Home Assistant
  3. Home Assistant mainly in control (that’s in many cases beneficial)

I don’t think this is such a big issue because ESPHome is only really writing a value to flash when the previous value is indeed changed.

With my method it is still possible to adjust the value via an input option in HA if required.
I do this as well with my water meter to correct for small deviations with the real water meter.

I prefer to keep the sensor output processing local on the ESP as much as possible, so that this is not disturbed by a (short or long) outage of Home Assistant.
So when HA is offline and comes back online again the sensor values from the ESPs are still accurate.

@thusassistint thanks for sharing.

Looks like a bit of a workaround. Pity that there is no out of the box solution. Maybe raise up a feature request or at least a contribution in the documentation.

Does your solution also work with the Energy dashboard?
If I manually after startup set the pulse_counter.set_total_pulses. I have to delete the wrong statistic for the dashboard

Yes it does, I am using my ESPHome sensor sensor.water_total as input for the Water consumption in the Energy Dashboard.

1 Like

Ok I will try it thanks

@thusassistint Hey again,

I did try you code, but after a upgrade the ESP Controller with a new firmeware. I got wrong values.
I saw two issue:
The state was: 10241.504
After upgrade the counter got the value 10241 pulses and then multiply with 0.001 so the global state was 10.241

  1. First problem the decimal places were gone
  2. Second i think i have to multiply with 1000 but not sure during on_shutdown or on_boot and how

Do you have an idea? Thank you

Hi,
Reading this and many other topics i try to get my water meter running that has installed a proximity meter. When it turns the light on the proximity meter on every time the wheel passes. Every passage should be 1 liter of water but. The code is easy and simple and you find it on many places over the web ( i used pieter brinkmans code but there are many others) I also tried pulse meter and pulse counter. But every time the led goes red the log shows 80-120 pulses (fluctuating a little) instead of 1. I use a D1 mini and connected it to D4 - GPIO2. Questions i have might it be possible that the time the red light is on(so the pulse is on) it measures the time it’s on and shows in a way so many pulses ? or did i not use the correct pin on the D1 mini or something .