I have a Airgradient air quality meter and a cleanairkit air purifier attached to a esphome smartplug.
I want to improve my auomation to automatically turn on the air purifer in a smarter way.
This is the algorithm I would like to implement:
When PM2.5 is above some number k for nconsecutive measurements:
Turn on Air purifier for at least x minutes
After this time, turn off if PM2.5 is below i
Recheck initial condition
Where i<k.
An example:
If PM2.5 is above 5 µg/m³ for 4 consecutive measurements, turn on air purifier for 30 min.
After that time: if PM2.5 is below 4 µg/m³, turn air purifier off.
And this shall repeat, such that for values between 5 and below, the thing shall not turn on from an off state. However, if it once detected “bad air” for a few measurements it shall continue for the x minutes even though the measurements may have improved already. And of course if the air is bad after x min, continue running it.
This is a) to prevent oscilations and b) to account for realtive proximity between air purifier and air quality sensor: The purifier needs to do a lot more work to clean the air of the room than just to make the sensor happy because it is so close.
I know this sounds more complicated but should be faily simple. Can someone help me automate this? I tried poking around in the automation settings with duration, etc but could not really get it to work.
Hello Humserv,
I’m sure that can be done. Also having someone or something writing it for you means the second it misbehaves, you will be struggling back to find someone to fix it for you.
The best way to learn is to just try. Run thru the docs on getting started with the automation editor. If you get stuck, come back with what you have done.
Hi, if the setpoint values are somewhat fixed, try creating a threshold binary sensor. It uses a value and a hysteresis to turn on and off if a value is in a certain range.
Then you have the simplest of automations to turn the purifier on and off based on the binary sensor. It is also easy to put in a delay on the off to give the purifier time to do the work.
If you want the values to be settable in the ui ore easily then consider using a thermostat/hygrostat. They are of course meant for temperature and humidity, but they might be repurposed for something else too.
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):