How do I create an alert if a threshold X breached 3 times within the past 15 minutes, alert?

Currently using an Apollo AIR-1 with Home Assistant, which is an awesome air quality monitor; however, I am not sure if there is a bug in HA, the sensor or something else, every so often, values for Humidity for example will drop to near-zero for a single read. Then, I get an email alert for a false alarm.

Questions:

  1. Is there a parameter to deal with these false positives built into HA?
  2. If not, how would one develop a rule such that if the threshold is breached, for example (humidity < 35%) at least 3 times in the last 15 minutes? If I were able to develop a rule like this, I would stop getting emails about humidity being misreported as shown below:

The same problem happens where temperature will jump ~10F for a single reading and then go back to normal, but I keep getting alerts for these false positives:

Top = Humidity
Bottom = Temperature

What I’d do, please note that it is to help, not tested

template:
  binary_sensor:
    - name: "Humidity below 35%"
      state: "{{ states('sensor.something_humidity') | float(200) < 35 }}"

Then use the history_stats sensor

sensor:
  - platform: history_stats
    name: humidity_below_35_count
    entity_id: binary_sensor.humidity_below_35
    state: "on"
    type: count
    duration: "00:15:00"
    end: "{{ now() }}"
    unique_id: 0ed50e19-d811-444c-a6f6-d48db4b753f7

Then you’ll have something that tells you how many times it was below 35% in the last 15 minutes, to add as condition in your automation.

EDIT: Maybe change to time instead of count if you want to test it against the 15 minutes.

The simple fix is to add a for: duration to your trigger in your automation, so for example the humidity has to drop below 35% and remain below 35% for 2 minutes.

You could also create an entity that mimics the value of the humidity sensor but filters out the spikes:

  • Check out the filter integration for built-in filter options.
  • Create your own logic and implement it with a trigger-based template sensor. I would do something like: If new state < 70% of previous state, use previous state instead. Else use new state.