Notification for binary state change X amount of times within Y minutes

New HA user and really wracking my brain trying to figure this one out.

I’m trying to trigger a notification if a device’s binary state changes to ON X amount of times within Y minutes. I intend to hardcode these X and Y values depending on the device at hand. Any ideas on how I can do this?

Here’s some rough pseudo code of how I was thinking I could implement, but I’m having trouble translating this over to HA. But maybe there’s a simpler way?

When device state changes to ON, start a timer for Y minutes
	timesOn = 1
	while timer is < Y minutes
		if device state changes to on:
			timesOn += 1
			if timesOn = X, then
				trigger action

Have you looked at History Stats - Home Assistant
I’ve not used it myself but should allow you to create a count sensor to see how many times your state was on?

1 Like

What I can think of:

More than a tad cumbersome, I admit :wink:

1 Like

Thanks! That does seem reasonable but yeah, definitely a bit cumbersome.