Count only if another entity was on for x seconds in the last y seconds

Hi!
I am monitoring the actual power usage of my coffee grinder with a Threshold sensor (when coffee grinder uses more than 150W → treshold sensor = ON). Please don’t ask why I am counting this :joy: I use this to count how many coffees I have made…

So, now I have a “little problem”: sometimes I stop grinding the coffee because I have to distribute the grinded coffee in the portafilter. To count not every stop as a single grinding (and therefore as a single coffee making), I have implemented an automation, to count only then when the state of the treshold sensor is “On” for at least 5 seconds.
Sometimes it happens, that i stop exactly on the point where the sensor either doesn’t count any of those “2” grindings (because the single grindings are < 5 seconds) or the sensor counts 2 grindings as single ones.
Is there any way, to set up an automation that runs in the following way:
if the grinder is on for x seconds (x will be defined) in the last y seconds (y will also be defined), then increase the counter by 1?

Many thanks in advance
Andreas

“If the grinder is on for 5 seconds in the last 10 seconds (or any value greater than 5)” will always be true if it was on for 5 seconds.

What is the shortest amount of time you could have between grinding “sessions” that you’d like to count as two separate events?

As soon as your threshold sensor turns true you can count that as +1. Then, don’t allow your automation trigger again until some amount of time (2 minutes?) elapses.

In your automation, use a condition like:

condition:
  - condition: template
    value_template: "{{ this.attributes.last_triggered + timedelta(minutes=2) < now() }}"

Yeah and how to I implement this? :smiley:

I pointed out that you described an operating test that would always evaluate to true. A test that always reports true is an unnecessary test; remove it from your requirements.

You may wish to consider exploring the possibility of using the Time Throttle filter. It may be useful for counting the first detection of grinding and then ignoring any subsequent short pauses and restarts within a given time window.