Hi,
as winter with smog days is coming (at least to the Central Europe), I have tried to parse the public air pollution data provided in json by chmi.cz (see http://portal.chmi.cz/files/portal/docs/uoco/web_generator/actual_hour_data_CZ.html, licence CC BY-NC-ND 3.0).
In the template code you can select the right station and its measurement. The sensor’s data shows the value, sensor’s icon shows the level (1 best, usually up to 5-6 worst), each location has also separate value with overall station’s status - one can create other dependent rules based on its value (e.g. limit ventilation if the outside air is bad etc.). Enjoy
- platform: rest
name: air_quality
# store json parts into attributes
json_attributes:
- Note
- States
- Legend
resource: http://portal.chmi.cz/files/portal/docs/uoco/web_generator/aqindex_cze.json
scan_interval: 0:30:00
# some value must be present
value_template: '{{ value_json["Actualized"] }}'
- platform: template
sensors:
air_overall:
friendly_name: "Air Quality at XYZ"
unit_of_measurement: '*'
icon_template: >-
{%- for state in states.sensor.air_quality.attributes.States if state.Code == 'CZ' -%}
{%- for region in state.Regions if region.Code =='XX' -%}
{%- for station in region.Stations if station.Code == 'XXXXX' -%}
{% set state = float(station.Ix)|round(0) %}
{% if state >0 and state < 7 %}
mdi:numeric-{{ state }}-box
{% else %}
mdi:minus-circle
{% endif %}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
value_template: >-
{%- for state in states.sensor.air_quality.attributes.States if state.Code == 'CZ' -%}
{%- for region in state.Regions if region.Code =='XX' -%}
{%- for station in region.Stations if station.Code == 'XXXXX' -%}
{{ float(station.Ix)|round(0) }}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
entity_id: sensor.air_quality
air_pm10_1h:
friendly_name: "PM₁₀ 1h"
entity_id: sensor.air_quality
unit_of_measurement: µg/m³
icon_template: >-
{%- for state in states.sensor.air_quality.attributes.States if state.Code == 'CZ' -%}
{%- for region in state.Regions if region.Code =='XX' -%}
{%- for station in region.Stations if station.Code == 'XXXXX' -%}
{%- for component in station.Components if (component.Code == 'PM10' and component.Int == '1h') -%}
{% set state = float(component.Ix)|round(0) %}
{% if state >0 and state < 7 %}
mdi:numeric-{{ state }}-box
{% else %}
mdi:minus-circle
{% endif %}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
value_template: >-
{%- for state in states.sensor.air_quality.attributes.States if state.Code == 'CZ' -%}
{%- for region in state.Regions if region.Code =='XX' -%}
{%- for station in region.Stations if station.Code == 'XXXXX' -%}
{%- for component in station.Components if (component.Code == 'PM10' and component.Int == '1h') -%}
{% if component.Val is defined %}
{{ float(component.Val)|round(1) }}
{% else %}
n/a
{% endif %}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}