Sure thing. No guarantees or promises here, itās all just a lash-up but I hope it makes sense to you. You will obviously need to adapt it to your own environment with IP addresses of your own purifier and MQTT broker. Youāll also need the Python packages py-air-control
and paho-mqtt
package (install these with pip3).
Below is the script (pfypub.py) that obtains the status, saves it to the database and publishes to MQTT. It runs every 10 minutes, invoked by a cron entry:
## Air quality from the Philips machine every 10 minutes
*/10 * * * * /usr/bin/python3 /home/pi/bin/pfypub.py
#!/usr/bin/env python3
# Obtain latest air quality data from Philips purifier
# Store in a little database table, and publish to MQTT
from datetime import datetime
from pyairctrl.coap_client import CoAPAirClient
import sqlite3
AQDB = '/home/pi/purifier/aq.db'
# MQTT broker
import paho.mqtt.client as mqtt
import json
BROKER = "172.28.16.24"
PORT = 1883
TOPIC = "air/bedroom"
# IP of the air filter
FILTER = "172.28.16.95"
class AirFilter:
def __init__(self, host, port=5683, debug=False):
self._client = CoAPAirClient(host, port, debug)
self.status = self._client.get_status(debug)
def get_quality(self):
if self.status:
return self.status["iaql"]
def get_name(self):
if self.status:
return self.status["name"]
def get_fanspeed(self):
if self.status:
return self.status["om"]
def get_mode(self):
if self.status:
return self.status["mode"]
def get_pm25(self):
if self.status:
return self.status["pm25"]
def get_power(self):
if self.status:
return self.status["pwr"]
def main():
timestamp = int(datetime.now().timestamp())
bedroom = AirFilter(FILTER)
name = bedroom.get_name()
power = bedroom.get_power()
fanspeed = bedroom.get_fanspeed()
quality = bedroom.get_quality()
pm25 = bedroom.get_pm25()
mode = bedroom.get_mode()
dbc = sqlite3.connect(AQDB)
csr = dbc.cursor()
sql = 'INSERT INTO airquality (Timestamp, Filtername, Powerstatus, Fanspeed, AirQualityIndex, PM25, Mode ) VALUES( {}, "{}", {}, {}, {}, {}, "{}" )'.format(timestamp, name, power, fanspeed, quality, pm25, mode)
#print(sql)
count = csr.execute(sql)
dbc.commit()
csr.close()
dbc.close()
adic = {"timestamp" : timestamp,
"name" : name,
"power" : power,
"fan" : fanspeed,
"quality" : quality,
"pm25" : pm25,
"mode" : mode}
ajson = json.dumps(adic)
#print(ajson)
client = mqtt.Client()
client.connect(BROKER, PORT)
client.publish(TOPIC, ajson)
client.disconnect()
if __name__ == '__main__':
main()
The database is optional, I created it in sqlite3 with one table as follows:
CREATE TABLE airquality (
Timestamp integer NOT NULL,
Filtername text,
Powerstatus integer,
Fanspeed integer,
AirQualityIndex integer,
PM25 integer,
Mode text,
PRIMARY KEY (Timestamp)
);
The HA sensor definitions are these:
# Air quality, obtained from Philips air purifier
- platform: mqtt
state_topic: "air/bedroom"
name: "Fan speed"
unit_of_measurement: "Index"
value_template: '{{ value_json.fan }}'
- platform: mqtt
state_topic: "air/bedroom"
name: "Air quality index"
unit_of_measurement: "Index"
value_template: '{{ value_json.quality }}'
- platform: mqtt
state_topic: "air/bedroom"
name: "PM25"
unit_of_measurement: "Index"
value_template: '{{ value_json.pm25 }}'
The dashboard card is this:
entities:
- entity: sensor.air_quality_index
- entity: sensor.pm25
hours_to_show: 24
refresh_interval: 600
title: Air quality
type: history-graph
Good luck!