History Stats count state every minute

Hi there!

As a long time Home Assistant user and forum reader I finally came to the point where I couldn’t find a solution by my own or in the forum :slight_smile:

I have a binary_sensor which unregulary change states. Sometimes it stays on or off for hours. Now I want to count these “on” states every minute and tried the history_stats counter:

- platform: history_stats
  name: "Basement Watermeter Liters Minute"
  entity_id: binary_sensor.basement_watermeter_pulse
  state: "on"
  type: count
  start: "{{now().replace(second=0)}}"
  end: "{{now()}}"

Now if the state is “on” and a new minute begins, it stays on which is perfectly fine.
But the counter takes this also as a new “on” and increases to 1. So if my “on” states are liters of water and I don’t use any water the whole night, it counts 1 liter for every minute which is wrong of course.

Actually the counter works as expected but then its also useless somehow. At least in this case. What I miss is a counter of state changes.

Or maybe I missed something and it is already possible?
The only other solution which I can think of, is an automation which monitors the state of the binary_sensor and updates a counter.

Is there anything better and/or simpler?

thx and greetings
d.k