Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hassapi as hass
- import json
- import urllib.request
- import datetime as dt
- def calcAQI(Cp, Ih, Il, BPh, BPl):
- """
- The calcs AQI using the varibles passed in. I'm not sure what they are exactly.
- this function is from purpleair's docs. converted from JS to python.
- """
- a = (Ih - Il)
- b = (BPh - BPl)
- c = (Cp - BPl)
- return round((a / b) * c + Il)
- def aqiFromPM(pm):
- """
- Pass in the pm 2.5 value and returns AQI.
- this function is from purpleair's docs. converted from JS to python.
- JS call : var AQI = aqiFromPM(pm25value);
- Good 0 - 50 0.0 - 15.0 0.0 – 12.0
- Moderate 51 - 100 >15.0 - 40 12.1 – 35.4
- Unhealthy for Sensitive Groups 101 – 150 >40 – 65 35.5 – 55.4
- Unhealthy 151 – 200 > 65 – 150 55.5 – 150.4
- Very Unhealthy 201 – 300 > 150 – 250 150.5 – 250.4
- Hazardous 301 – 400 > 250 – 350 250.5 – 350.4
- Hazardous 401 – 500 > 350 – 500 350.5 – 500
- """
- # this is error checking. convert to python?
- # if (isNaN(pm)) return "-"; // isNaN means "is number"
- # if (pm == undefined) return "-";
- # if (pm < 0) return pm;
- # if (pm > 1000) return "-";
- if (pm > 350.5):
- return calcAQI(pm, 500, 401, 500, 350.5)
- elif (pm > 250.5):
- return calcAQI(pm, 400, 301, 350.4, 250.5)
- elif (pm > 150.5):
- return calcAQI(pm, 300, 201, 250.4, 150.5)
- elif (pm > 55.5):
- return calcAQI(pm, 200, 151, 150.4, 55.5)
- elif (pm > 35.5):
- return calcAQI(pm, 150, 101, 55.4, 35.5)
- elif (pm > 12.1):
- return calcAQI(pm, 100, 51, 35.4, 12.1)
- elif (pm >= 0):
- return calcAQI(pm, 50, 0, 12, 0)
- else:
- return undefined
- class AirQuality(hass.Hass):
- def initialize(self):
- #self.get_set_purpleair(self)
- starttime = dt.datetime.now() + dt.timedelta(seconds=5)
- self.run_every(self.get_set_purpleair, starttime, 300)
- self.listen_state(self.sensorNodePmChange, "sensor.sensornode_airquality_pm_2_5")
- def sensorNodePmChange(self, entity, attribute, old, new, kwargs):
- # pm2.5 value changed. Update AQI.
- if new == "unavailable":
- return
- self.set_state(
- "sensor.inside_air_aqi",
- state = aqiFromPM(float(new)),
- attributes={
- "unit_of_measurement": "AQI",
- },
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement