Advertisement
madkatz

VOC AQI Calculation

Mar 8th, 2025
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. {# pollutant: VOC #}
  2.  
  3. {% set pollutant = states('sensor.apollo_air_1_2d2ee4_sen55_voc') | float %}
  4. {% set aqi = namespace(value=0) %}
  5.  
  6. {# Set Breakpoints for pollutant #}
  7. {% set good_low = 0.0 | float %}
  8. {% set good_high = 79 | float %}
  9. {% set moderate_low = 80 | float %}
  10. {% set moderate_high = 119 | float %}
  11. {% set sensitive_low = 120 | float %}
  12. {% set sensitive_high = 199 | float %}
  13. {% set unhealthy_low = 200 | float %}
  14. {% set unhealthy_high = 299 | float %}
  15. {% set very_low = 300 | float %}
  16. {% set very_high = 399 | float %}
  17. {% set hazard_low = 400 | float %}
  18. {% set hazard_high = 599 | float %}
  19.  
  20.   {# Calculate AQI #}
  21.   {% if pollutant <= good_high %}
  22.     {% set aqi = ((pollutant - good_low) * (50-0)/(good_high - good_low)) %}
  23.   {% elif pollutant <= moderate_high %}
  24.     {% set aqi = ((pollutant - moderate_low) * (100-51)/(moderate_high-moderate_low)) + 51 %}
  25.   {% elif pollutant <= sensitive_high %}
  26.     {% set aqi = ((pollutant - sensitive_low) * (150-101)/(sensitive_high - sensitive_low)) + 101 %}
  27.   {% elif pollutant <= unhealthy_high %}
  28.     {% set aqi = ((pollutant - unhealthy_low) * (200-151)/(unhealthy_high - unhealthy_low)) + 151 %}
  29.   {% elif pollutant <= very_high %}
  30.     {% set aqi = ((pollutant - very_low) * (300-201)/(very_high - very_low)) + 201 %}
  31.   {% else %}
  32.     {% set aqi = ((pollutant - hazard_low) * (500-301)/(hazard_high - hazard_low)) + 301 %}
  33.   {% endif %}
  34.  
  35.   {{ aqi | round(0) }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement