# Development of an Automatic Low-Cost Air Quality Control System: A Radon Application
## 2 Materials and Methods
### The Prediction Algorithm  
The piece that links the monitoring devices' readings and the control devices' state providing the automatic air quality control is the air quality prediction algorithm. We designed the algorithm with the following requisites:  
1. Its inputs should be the current measures of a device, and its outputs should be the commands (on/off) sent to the control device associated with the monitoring device or no command. \(\circ\) It could hold an internal state, but it should be minimal to fulfill requisite 3. 2. The relationship between inputs and outputs should be based on a prediction for the air quality measure, not on the current air quality. \(\circ\) The air quality measure can be based on a subset of the variables a monitoring device can sense. 3. The algorithm should be simple enough to run on an edge device. \(\circ\) It should be fast. \(\circ\) It should require low resources: low memory and low processing power. Figure 3: The air quality monitoring devices. 4. It should have the ability to have different behaviors by changing some parameters. The implementation of the algorithm fulfilled the requisites in the following manner:
5. Requisite 1: the input of the algorithm is the current radon concentration of a specific monitoring device, the time when the reading was done, and the parameters (see requisite four implementation). The algorithm's output is the command to send to the associated control device ('on' or 'off') or no action ('NULL'). \(\circ\) The algorithm has a minimal internal state of two values: the time when the device sent the previous readings to the server and the previous radon slope. 6. Requisite 2: the prediction algorithm uses the radon concentration as the measure of the air quality. We chose the radon concentration as the air quality measure because high radon concentrations cause health problems [10; 11; 12; 13]. Radon has a notable incidence in Spain, especially in Galicia, where we carried out this work, caused by its strongly linked to granite geology, the primary source of radon emissions [45; 46]. \(\circ\) We carried out some initial tests that showed that the radon concentration values showed temporary spikes. The prediction algorithm uses the radon concentration slope to interpolate the radon value, so brief spikes in the radon concentration give a wrong prediction. The prediction algorithm mitigates this effect by using the exponentially weighted moving average (EWMA) of the radon concentration slope (the difference between the current and previous radon concentration over the time difference of the readings). The algorithm uses the radon concentration slope EWMA to predict the future radon concentration by interpolation, using the time window specified. \(\circ\) If the algorithm predicts that the radon concentration will be above the configured threshold, it outputs an 'on' command. The server sends it, via MQTT, to the control device associated with the current monitoring device. It also sends the corresponding \"high radon concentration\" alert via MQTT, email, and Telegram, if configured. \(\circ\) If the airflow system is on and the actual radon concentration is below the threshold, the algorithm outputs the command for stopping the airflow system, and the server sends it to the associated control device. It also sends the corresponding \"back to normal\" alert via MQTT, email, and Telegram, if configured. 7. Requisite 3: each time a monitoring device sends a new reading to the server, the prediction algorithm runs. The algorithm restores its state for the current monitoring device (two values as explained in the requisite one implementation). Then the algorithm sets some constants, calculates the current radon concentration slope, calculates the EWMA using the current reading and the previous state, and outputs a command if needed. \(\circ\) Although the algorithm runs every time a new reading is sent to the server, it is fast, and it only outputs a command if it predicts a high radon concentration or the airflow system needs to be turned off.