[Automation] Trigger when binary sensor changes often

Is there a relatively simple way to trigger my automation if a binary sensor changes often (for example 30 times in 15 minutes)?
I have 2 security cameras with motion detection installed and have motion sensors for them.
The problem is that very often spiders make cobwebs in front of them which causes my cameras to permanently record at night if there is any wind.
What I want to do is to send a notification to my phone that I have to clear the cams if the motion is dectected so often like in the screenshot.

If someone can help me here, I would also need a way to only trigger this only once (per day) so I won’t wake up with hundreds notifications because for the whole night it has changed 30 times in 15 minutes.
I would do it with a helper switch and condition testing but maybe there is a better way?

I think you can use the count type of the History Stats sensor.

Create a Statistics Sensor that counts the number of times your binary_sensor change to on in the past 15 minutes.

  - platform: statistics
    name: Recent Detections
    entity_id: binary_sensor.your_sensor_name
    state_characteristic: count_on
      minutes: 15
    sampling_size: 100

Create an automation that uses a Numeric State Trigger to detect when the Statistics Sensor’s value exceeds 30 detections. The automation uses a Template Condition (shown in shorthand notation) that allow the notification to occur only if the the last time the automation triggered was yesterday (in other words, it only allows the action to be executed once per day).

alias: Notify excessive recent detections
  - platform: numeric_state
    entity_id: sensor.recent_detections
    above: 30
  - "{{ this.attributes.last_triggered | default(today_at() - timedelta(minutes=1)) < today_at() }}"
  - service: notify.your_device
      message: More than 30 detections in the past 15 minutes.

You will need to replace binary_sensor.your_sensor_name with the entity_id of your binary_sensor and replace notify.your_device with whatever notification service you intend to use.

1 Like

Thank you so much.
Sorry for the late response, I have just seen while being on the HA site that I have new notifications. I tought that it will notify me with an E-Mail if someone answers, but it seems like that is not the case.

I have created the automation and hopefully it will work. :slight_smile: