Help for air purifier automation

Do you care about time, or number of measurements? Time is easy, with the for: parameter in the numeric state trigger.

If it must be the latter, you will find some difficulties when a new measurement comes in the same as the last — that won’t, by default, fire any triggers.

See the State Object docs: you might need to create a template sensor helper that mirrors the last_reported property of the meter.

{{ states.sensor.YOUR_METER.last_reported }}

Then build a “value cascade” sensor like this that triggers from your new template sensor and records the current air quality measurement with n “slots”.

Next, create a template binary sensor helper that checks if slots of your cascade sensor are above k (here, for k=5, with the 999 to filter out anything that can’t be converted to float, like the last_updated attribute):

{{ states.sensor.YOUR_CASCADE.attributes.values()|map('float',default=999)|select('<=',5)|list|count == 0 }}

Trigger your automation from that binary sensor.

If you want further help, please provide the entity IDs for your sensor and switch, as well as values for k, n, x and i.