Advertisement
Guest User

Untitled

a guest
Sep 7th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import hassapi as hass
  2. import json
  3. import urllib.request
  4. import datetime as dt
  5.  
  6.  
  7. def calcAQI(Cp, Ih, Il, BPh, BPl):
  8.     """
  9.    The calcs AQI using the varibles passed in. I'm not sure what they are exactly.
  10.    this function is from purpleair's docs. converted from JS to python.
  11.    
  12.    """
  13.     a = (Ih - Il)
  14.     b = (BPh - BPl)
  15.     c = (Cp - BPl)
  16.     return round((a / b) * c + Il)
  17.  
  18. def aqiFromPM(pm):
  19.     """
  20.    Pass in the pm 2.5 value and returns AQI.
  21.  
  22.    this function is from purpleair's docs. converted from JS to python.
  23.    JS call : var AQI = aqiFromPM(pm25value);
  24.    
  25.  
  26.     Good                              0 - 50         0.0 - 15.0         0.0 – 12.0
  27.    Moderate                         51 - 100           >15.0 - 40        12.1 – 35.4
  28.    Unhealthy for Sensitive Groups  101 – 150     >40 – 65          35.5 – 55.4
  29.    Unhealthy                       151 – 200         > 65 – 150       55.5 – 150.4
  30.    Very Unhealthy                  201 – 300 > 150 – 250     150.5 – 250.4
  31.    Hazardous                       301 – 400         > 250 – 350     250.5 – 350.4
  32.    Hazardous                       401 – 500         > 350 – 500     350.5 – 500
  33.    """
  34.  
  35.     # this is error checking. convert to python?    
  36.     # if (isNaN(pm)) return "-"; // isNaN means "is number"
  37.     # if (pm == undefined) return "-";
  38.     # if (pm < 0) return pm;
  39.     # if (pm > 1000) return "-";
  40.  
  41.     if (pm > 350.5):
  42.         return calcAQI(pm, 500, 401, 500, 350.5)
  43.     elif (pm > 250.5):
  44.         return calcAQI(pm, 400, 301, 350.4, 250.5)
  45.     elif (pm > 150.5):
  46.         return calcAQI(pm, 300, 201, 250.4, 150.5)
  47.     elif (pm > 55.5):
  48.         return calcAQI(pm, 200, 151, 150.4, 55.5)
  49.     elif (pm > 35.5):
  50.         return calcAQI(pm, 150, 101, 55.4, 35.5)
  51.     elif (pm > 12.1):
  52.         return calcAQI(pm, 100, 51, 35.4, 12.1)
  53.     elif (pm >= 0):
  54.         return calcAQI(pm, 50, 0, 12, 0)
  55.     else:
  56.         return undefined
  57.    
  58. class AirQuality(hass.Hass):
  59.     def initialize(self):
  60.         #self.get_set_purpleair(self)
  61.  
  62.         starttime = dt.datetime.now() + dt.timedelta(seconds=5)        
  63.         self.run_every(self.get_set_purpleair, starttime, 300)
  64.         self.listen_state(self.sensorNodePmChange, "sensor.sensornode_airquality_pm_2_5")
  65.  
  66.  
  67.  
  68.  
  69.     def sensorNodePmChange(self, entity, attribute, old, new, kwargs):
  70.         # pm2.5 value changed. Update AQI.
  71.         if new == "unavailable":
  72.             return
  73.            
  74.         self.set_state(
  75.             "sensor.inside_air_aqi",
  76.             state = aqiFromPM(float(new)),
  77.             attributes={
  78.                 "unit_of_measurement": "AQI",
  79.                
  80.             },
  81.         )        
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement