Trigger - If light turns on more than 5 times in 10 minutes

Would anyone know how to code the following trigger in an automation:

If light turns on more than 5 times in 10 minutes then do some action.

I’ve run into situations where a light will repeatedly turn on and off and I’ll have no idea until I physically discover it’s doing that. For example a light tied to a malfunctioning motion sensor that keeps detecting motion/no motion. Another example an outside camera floodlight that keeps detecting motion when the winds kick up. Any assistance is appreciated.

You could use a history stats count sensor for the last 10 minutes:

sensor:
  - platform: history_stats
    name: Lamp on count
    entity_id: light.my_lamp
    state: "on"
    type: count
    end: "{{ now() }}"
    duration: 
      minutes: 10

automation

triggers:
  - trigger: numeric_state
    entity_id: sensor.lamp_on_count
    above: 4

Thanks tom_l, exactly what I needed, tested and works great! I tried a couple of approaches on my own but never would’ve thought of a History Stats sensor. This community is certainly helpful for continued learning. Thanks for the steer in the right direction, much appreciated.

1 Like