If motion is detected 3 times in 5 minutes then

Hi I currently have two motion sensors in my ensuite, one near the doorway and one in the shower. Which turn the lights on and the extra fan.

I want to automate a state that is if the shower detects motion 3 times in 5 minutes than my fan relay stays on for 20 minutes. Otherwise it goes off after 5 minutes.

What’s the best way to do this in a automation?

Use a history stats sensor on count and a five minute time setting