OK, the attached code implements a new sensor called pulse_meter. This has a very similar setup to pulse_counter, with two key differences:
- It measures the time between the pulses and outputs a measurement for every pulse rather than a measurement every minute. This gives much better resolution.
- It has an additional “total” sensor that outputs the total number of pulses (this is the same as the current development branch of
pulse_counter).
These two together allow you to get the best possible temporal resolution (a measurement for every pulse) and accurate totals (as we can really count the pulses rather than trying to integrate the frequency).
Here’s an example config for my electricity meter that outputs 1000 pulses per kWh:
sensor:
- platform: pulse_meter
name: "Electricity Meter"
pin: D5
unit_of_measurement: "kW"
internal_filter: 5us
accuracy_decimals: 3
timeout: 5min
total:
name: "Electricity Meter Total Count"
filters:
- multiply: 0.06
The setup is much the same as pulse_counter with a couple of changes:
- You can’t specify rising or falling edges, we always look for rising edges (you can invert the pin if you want falling edges for some reason). In particular we can’t count both rising and falling, that doesn’t make sense for this sensor.
- I’ve added a
timeoutvalue, this defaults to 5 mins. If we don’t see a pulse for this amount of time we assume 0 pulses/min. If we don’t do this then we get “stuck” on the previous measured value if the pulses stop (because we normally update the value on each pulse).
Here is some output showing pulse_meter vs pulse_counter:
You can see that the new sensor (blue) tracks the usage much more quickly than the pulse counter (red). In particular it shows spikes that the pulse counter will miss, and doesn’t hunt up and down (see the period from 14:39 to 15:09 as a good example). It does however track the old sensor pretty well which is reassuring.
You can download the new sensor here:
Unpack it into your esphome directory (this will be in the config directory for hassio).
