Pulse counter - Total counter

I am trying to get “Total pulses counter” - additional sensor with value that would add pulses reported every 60s

I tried what @OttoWinter suggested in one of issues on github.
Problem is I’m getting ‘unknown value’ in Hass.io all the time
Could anyone help me solve this?
Any help/ideas apreciated

Here is my code:

sensor:
  - platform: pulse_counter
    pin: D5
    unit_of_measurement: 'W'
    name: 'Pobór prądu'
    id: pulse
    filters:
      - multiply: 9.375
  - platform: template
    name: "Zużycie prądu całkowite"
    unit_of_measurement: 'kWh'
    lambda: |-
      static float total_value = 0.0;
      total_value += id(pulse).state / 6400;
      return total_value;

What are your logs telling you?

I had exactly the same problem when I was trying to get a pulse counter working a few weeks ago and it was maddening. I spent tons of time down in the C++ code trying to debug it there, only to get “unavailable” in my Hass.io instance for my template sensor and the expected value for the sensor itself. I could return the count, the total, and other numbers, but as soon as I tried to perform a math operation on the state, it would turn into “unavailable”. I’m trying to remember exactly what the problem was and I’m failing so far.

One thing that I discovered is that the underlying C++ code only maintains a count of pulses since the last time the raw count was read, resetting it each time. I thought that it was keeping a running counter, so when I tried to add additional reads and prints in the lambda function, I would get values that made no sense. I think that may have been related the weird Hass behavior.

There is nothing in the logs … Or I’m looking in wrong places:
ESPHome node logs show only information about pulse readings (30 pulses/min) and what is sent to Hass.io (280W) … But when for test I changed lambda to just adding 1 on every cycle, it worked - showed up in Hass.io, but wasn’t visible in logs.
In Hass.io logs I don’t see any references to this sensor.
I kept it for the night and it wasn’t updated once…

Take a look at this https://esphome.io/components/sensor/total_daily_energy.html# and this https://esphome.io/cookbook/power_meter.html - do they help?

1 Like

Looks like this is working :+1:
Thank you very much.
It can be also achieved on Hassio side (integration sensor component) but I needed a counter on ESP side in case there is no connection to Hassio

@MrUkleja. How did you got it to working on the ESP side?

Hi,

I am using total daily energy counter that was suggested above - https://esphome.io/components/sensor/total_daily_energy.html#

Can this work with water L instead of energy W?
I’m trying to get a daily water total in litres from a pulse counter and ESPhome but not having much success so far.

change unit_of_measurement: to L/Hr may be

the pulse sensor will always give you the counts in a period of time, e.g. liters per hour, during the last hour, so if you want to have the liters ever since you’ve opened the faucet you have to integrate the sensor, unfortunately the integration sensor is buggy on 1.13 but you can use the template filter as is there on the first post in order to integrate it.

Yes or you can leave the integration to home assistant. Not everything needs to be done on the esp.

Did you manage to count liters of water?

1 Like

i am alo looking for a way to do this with water

There are threads detailing how to measure water flow.