Pulse counter to current usage

Hi,

I collect pulse from several sources (Kwh pulse counters and watermeter)
See below image, channel 0-4 are the pulses counted every 10 seconds for 5 different sources and channel 5-9 are the total pulses counted for the same fie sources.

What I would like to do is convert the counter into (near) realtime watt meters or water flow meter in case of the water based pulses.

What should be the best way to do this?

Many thanks in advance…

So you want to extract power (W) from your energy meter (Wh) for example?

If so, you need to differentiate the sensor with respect to time.

Unfortunately there is no differentiation sensor.

The statistics sensor has a change_rate attribute which is essentially the same (e.g. dWh/dt = W). You might be able to use that.

Hi,

Can you be a little bit more specific?

1 KW = 10.000 pulses. How do I diffentiate this?

Thansk,
Rien

Use the change_rate attribute from this:

I think you mean: 1 kWh = 10.000 pulses?
So 1 pulse = 0.0001 kWh.
And you can turn you pulse-counter into a kWh-counter by doing: kWh-count = pulse-count x 0.0001
Use a template sensor for that.

And what would I use for the change rate ?

But this would not give the realtime usage, or am I mistaken ?

Screenshot including default statistics sensor, see below.

I still don’t see how these statistics sensors would help me :frowning:

Correct.

If you want that, you can use this: https://www.home-assistant.io/integrations/derivative/
That will give you “change in pulses per time-unit” and you will need to translate this to for instance W: W = kWh/(1000*time-unit in hours).

time-unit in hours means: time-unit of 10s = (1/360) hour

That has been around since 0.105 and I still missed it:

:man_facepalming:

Also you might want to check your math here:

1 ÷ 10 = 0.1, not 0.0001

For the third and final time, look at the change_rate attribute of the statistics sensor, that is your “instantaneous power”. You can display the attribute directly in Lovelace or use a template sensor to break it out from the statistics sensor.

Or as Bouwe pointed out use the derivative sensor that (sorry) does actually exist.

RienduPre is apparently Dutch, so am I, we use the point as thousands-separator :slight_smile:

1 Like

Hi,

Sorry for my ignorance, but I’m still confused.
As stated in my first post, I want to calculate the current (realtime) usage in Watts (not KwH’s)
I have 2 types of sensors; pulses per 10 seconds and tot pulses. The update time for both is 10 seconds.
For starters; which of the 2 type do I have to use for this ?
See the screenshot of what I see now.

And this is my sensor part of the configuration .yaml of my test environment:

sensor:
  - platform: s0pcm
    device: '/dev/tty.usbmodemFD131'
    channels: 
      - 0
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6
      - 7
      - 8
      - 9
#  - platform: cyble
  - platform: statistics
    entity_id: sensor.s0pcm_channel_0
  - platform: statistics
    entity_id: sensor.s0pcm_channel_1
  - platform: statistics
    entity_id: sensor.s0pcm_channel_2
  - platform: statistics
    entity_id: sensor.s0pcm_channel_3
  - platform: statistics
    entity_id: sensor.s0pcm_channel_4
  - platform: statistics
    entity_id: sensor.s0pcm_channel_5
  - platform: statistics
    entity_id: sensor.s0pcm_channel_6
  - platform: statistics
    entity_id: sensor.s0pcm_channel_7
  - platform: statistics
    entity_id: sensor.s0pcm_channel_8
  - platform: statistics
    entity_id: sensor.s0pcm_channel_9

  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_0
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_1
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_2
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_3
  - platform: derivative
    unit: Liter
    unit_time: min
    source: sensor.s0pcm_channel_4
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_5
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_6
    unit: Watt
    unit_time: min
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_7
  - platform: derivative
    unit: Watt
    unit_time: min
    source: sensor.s0pcm_channel_8
  - platform: derivative
    unit: Liter
    unit_time: min
    source: sensor.s0pcm_channel_9

Please tell me what I precisely have to do, to see realtime power usage.
Many thanks in advance

Best regards,
Rien

I have. Three times.

OK, But I can’t find any good documentation of the change_rate attribute, so please just tell me how to use it. same goes for the derivative sensor. I am using it, but it does not give the desired results.

I just need some more help than just tell me to use it, I like to know how to use it.

Again many thanks in advance.

As I said:

I think what RienduPre is missing is that HA internally keeps track of the time when the pulses occur, so the derivative sensor (what delivers the same info as the change_rate attribute of the statistics sensor, as mentioned above), can show number of pulses/time. Multiply with 10000 (or divide, I’m not paying attention here), and you should have Wh*time, which is power (W).

Only caveat: does HA keep track of time in ms, and what is the jitter (lack of real-time) of your signal path?
Which hardware is doing the counting, and how did you interface it to your HA hardware?

To be able to turn pulses in to Watt, you need to define your measurement interval, say 10 second.
Then you need to count pulses per 10 seconds.

10000 pulses = 1 kWh --> 10 pulses = 1 Wh --> 1 pulse = 0.1 Wh
Suppose you measure 8 pulses in 10 secs, that translates to:
8 * 0.1 = 0.8 Wh
Now divide by time: the 10 secs interval:
0.8 * 360 (10 second = (1/360)h) = 288W.

So the formula is: sensor-value * 0.1 * (time-interval in secs/3600)

The derivative-sensor should give you the amount of pulses per time-interval.
You can use a 2nd template-sensor to translate the sensor-value to Watt.
See here: https://www.home-assistant.io/integrations/template/
Scroll down to the example called CHANGE THE UNIT OF MEASUREMENT.

Hi guys,

I’ve been trying out many settings to get an accurate reading which updates near realtime.
I use the statistics sensor’s change_rate and a template sensor. see below:

- platform: statistics
    entity_id: sensor.s0pcm_channel_6
    precision: 3
    max_age:
      minutes: 15

- platform: template
    sensors:
      zonnepaneel_production:
        friendly_name: "Zonnepaneel productie"
        value_template: "{{ state_attr('sensor.stats_6','change_rate') |float * 0.1 * 3600 | round(0)}} Watt"

So I’m nearly happy with the result except for the rounding of the value.
See example below
I used "round(0) in the template definition, but this does not work. Is this a bug or am I doing something wrong?

You are rounding the number 3600 (order of operations error). Put some brackets around it:

value_template: "{{ ( state_attr('sensor.stats_6','change_rate') |float * 0.1 * 3600 ) | round(0)}} Watt"