Journal

Predictive AQI Control: LSTM Forecasting to Pre-activate Air Purifiers Before PM2.5 Spikes

Predictive AQI Control: ใช้ LSTM Forecast ควบคุมเครื่องฟอกอากาศล่วงหน้าก่อน PM2.5 พุ่ง

May 12, 2026 · 1 min read
Predictive AQI Control: LSTM Forecasting to Pre-activate Air Purifiers Before PM2.5 Spikes

The Limitation of Reactive Control

Most smart air purifier systems use threshold-based control: when indoor PM2.5 exceeds 25 μg/m³, the purifier switches on. But waiting for pollution to enter before filtering means residents still absorb the peak exposure. Predictive control activates the system before PM2.5 reaches hazardous levels, dramatically reducing exposure.

Input Data for LSTM AQI Forecasting

Historical AQI: hourly PM2.5 from IQAir API (nearest monitoring station) + Thailand Pollution Control Department (PCD) data — 2 years of history.

Meteorological data: wind speed and direction, temperature, relative humidity, atmospheric pressure from the Open-Meteo API (free).

Seasonal features: month, day of week, hour of day. Bangkok AQI follows a clear pattern: peaks at 07:00–09:00 and 17:00–20:00 during the haze season (November–February).

Lag features: PM2.5 readings 1, 2, 4, 6, 12, and 24 hours prior — the most important features.

LSTM Model Architecture

Sequence length: 24 timesteps (24 hours). Features: PM2.5, temperature, humidity, wind speed, wind direction (cos/sin), hour (sin/cos), month (sin/cos) = 9 features. Model: 2-layer LSTM (64 units / 32 units) + Dropout 0.2 + Dense output (forecasts for 1h, 2h, 3h, 4h ahead). Training split: 18 months training, 3 months validation, 3 months test. Performance: MAE ~8–15 μg/m³ for 1–2h forecasts; RMSE rises with longer horizons.

Deployment on Raspberry Pi 5

Train the model on a cloud instance or PC, then export as TensorFlow Lite (.tflite) or ONNX. Run inference on Pi 5 every 30 minutes, or triggered when AQI changes by >10 μg/m³. A Python script updates the Home Assistant sensor (sensor.aqi_forecast_1h) via REST API.

Predictive Automation Logic in Home Assistant

When sensor.aqi_forecast_1h > 50 (Moderate): turn all air purifiers to high speed, reduce ERV flow to minimum (less outdoor air import), close smart windows if fitted, send LINE alert notifying that high AQI is forecast in 1 hour and the system has been prepared.

When sensor.aqi_forecast_1h < 30 (Good): set air purifiers to auto/low mode, increase ERV flow to normal for maximum fresh air.

Bangkok AQI Seasonal Patterns

Fine dust season (November–March): PM2.5 AQI regularly exceeds 100 for multi-day stretches — the period when the LSTM needs the most data and has the most variability. Retrain the model annually with new data.

Monsoon season (May–October): rain washes out PM2.5 — AQI stays low, LSTM accuracy is higher, purifiers run less.

Predictive vs Reactive Control Results

Simulation using Bangkok dataset: reactive control (25 μg/m³ threshold) absorbs 100% of cumulative PM2.5 during AQI spikes. Predictive LSTM control (1h ahead) reduces cumulative PM2.5 exposure by 40–60% by activating the system 30–60 minutes before the spike.

Questions & answers

How is LSTM better than Random Forest or XGBoost for AQI forecasting?
AQI is a time series with high temporal dependency — the current hour’s value depends on previous hours. LSTM is designed for sequence data and captures temporal patterns natively. XGBoost/Random Forest require manual lag feature engineering and do not capture long-term dependencies as well.
Is the Open-Meteo API free for home use?
Open-Meteo (open-meteo.com) is free for non-commercial use with no request limits. It provides hourly weather forecasts and historical data for any coordinates worldwide, including Bangkok.
Can an LSTM forecast really run on Raspberry Pi 5 without a GPU?
Yes — a TFLite quantised (INT8) LSTM model with 64+32 units runs inference on Pi 5 CPU in <1 second. No GPU is needed for inference (only for training). Train on a PC or cloud instance, export TFLite, then deploy on Pi.
How many years of historical data are needed to train an accurate LSTM?
Two or more years are recommended to cover at least two full fine dust seasons and two monsoon seasons. With less than one year of data, seasonal prediction accuracy drops significantly.

Related reading