Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HomeAssistant Luftdaten REST API - Air Quality, PM2.5, PM10, Temp and Humidity (SDS011 and DHT22)
sensor:
- platform: rest
name: luftdaten
resource: http://192.168.1.149/data.json
scan_interval: 100
json_attributes:
- sensordatavalues
value_template: '{{ value_json.age }}'
- platform: template
sensors:
luftdaten_pm10:
friendly_name: 'Balcony Air Quality PM10'
unit_of_measurement: 'µg/m³'
value_template: '{{ states.sensor.luftdaten.attributes["sensordatavalues"][0]["value"] }}'
luftdaten_pm25:
friendly_name: 'Balcony Air Quality PM2.5'
unit_of_measurement: 'µg/m³'
value_template: '{{ states.sensor.luftdaten.attributes["sensordatavalues"][1]["value"] }}'
luftdaten_temperature:
friendly_name: 'Balcony Temperature'
device_class: temperature
unit_of_measurement: '°C'
value_template: '{{ states.sensor.luftdaten.attributes["sensordatavalues"][2]["value"] }}'
luftdaten_humidity:
friendly_name: 'Balcony Humidity'
device_class: humidity
unit_of_measurement: '%'
value_template: '{{ states.sensor.luftdaten.attributes["sensordatavalues"][3]["value"] }}'
luftdaten_aqi:
friendly_name: 'Balcony Air Quality'
unit_of_measurement: 'AQI'
value_template: >
{% macro calcAQI(Cp, Ih, Il, BPh, BPl) -%}
{{ (((Ih - Il)/(BPh - BPl)) * (Cp - BPl) + Il)|round }}
{%- endmacro %}
{% if (states('sensor.luftdaten_pm25')|float) > 1000 %}
invalid
{% elif (states('sensor.luftdaten_pm25')|float) > 350.5 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 500.0, 401.0, 500.0, 350.5) }}
{% elif (states('sensor.luftdaten_pm25')|float) > 250.5 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 400.0, 301.0, 350.4, 250.5) }}
{% elif (states('sensor.luftdaten_pm25')|float) > 150.5 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 300.0, 201.0, 250.4, 150.5) }}
{% elif (states('sensor.luftdaten_pm25')|float) > 55.5 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 200.0, 151.0, 150.4, 55.5) }}
{% elif (states('sensor.luftdaten_pm25')|float) > 35.5 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 150.0, 101.0, 55.4, 35.5) }}
{% elif (states('sensor.luftdaten_pm25')|float) > 12.1 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 100.0, 51.0, 35.4, 12.1) }}
{% elif (states('sensor.luftdaten_pm25')|float) >= 0.0 %}
{{ calcAQI((states('sensor.luftdaten_pm25')|float), 50.0, 0.0, 12.0, 0.0) }}
{% else %}
invalid
{% endif %}
luftdaten_aqi_human:
friendly_name: 'Air Quality AQI Description'
value_template: >
{% if (states('sensor.luftdaten_aqi')|float) >= 401.0 %}
Hazardous
{% elif (states('sensor.luftdaten_aqi')|float) >= 301.0 %}
Hazardous
{% elif (states('sensor.luftdaten_aqi')|float) >= 201.0 %}
Very Unhealthy
{% elif (states('sensor.luftdaten_aqi')|float) >= 151.0 %}
Unhealthy
{% elif (states('sensor.luftdaten_aqi')|float) >= 101.0 %}
Unhealthy for Sensitive Groups
{% elif (states('sensor.luftdaten_aqi')|float) >= 51.0 %}
Moderate
{% elif (states('sensor.luftdaten_aqi')|float) >= 0.0 %}
Good
{% else %}
undefined
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment