Sensor de calidad del aire conectado a luftdaten.info ,Parte IV – Integrar en HA

Buenas , en estas líneas vamos a conectar nuestro sensor de calidad del aire a nuestro Home Assistant , lo primero crearemos un sensor para saber si esta conectado a nuestra red mediante un sensor ping.

- platform: ping 
  host: 192.168.1.26
  name: ping_luftdaten
  count: 5
  scan_interval: 300

Los datos los podemos extraer de varias formas , una de ellas es mediante el fichero data.json , para ello usaremos la integración local_luftdaten mediante HACS o directamente de la pagina https://github.com/lichtteil/local_luftdaten , también crearemos un sensor para traducir de hectopascales a milibares.

- platform: local_luftdaten
  host: 192.168.1.26
  name: estacion_luftdaten
  monitored_conditions:
    - SDS_P1
    - SDS_P2
    - BME280_temperature
    - BME280_humidity
    - BME280_pressure
    - signal  

- platform: template
  sensors:
    estacion_luftdaten_presion:
      friendly_name: 'Presión'
      value_template: >
        {{(states('sensor.estacion_luftdaten_pressure') | float / 100) | round(1)}}
      unit_of_measurement: 'mBar'

Crearemos un par de sensores para que nos indiquen la calidad del aire medido.

- platform: template
  sensors:
    # Air Quality Index = (PM10 mean over 1 day / 50) * 100
    air_quality_pm10:
      friendly_name: 'Air Quality PM 10'
      value_template: >-
        {%if ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) <= 33 %}Muy buena
        {% elif ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) <= 66 | float > 33 %}Buena
        {% elif ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) <= 99 | float > 66 %}Regular
        {% elif ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) <= 149 | float > 99 %}Mala
        {% elif ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) <= 200 | float > 149 %}Muy mala
        {% elif ((states.sensor.home_assistant_pm_10_concentration_mean.state | float) * 2.0) > 200 %}Peligrosa
        {%- endif %}
      entity_id: sensor.home_assistant_pm_10_concentration_mean
    # Air Quality Index = (PM2.5 mean over 1 day / 25) * 100
    air_quality_pm25:
      friendly_name: 'Air Quality PM 2.5'
      value_template: >-
        {%if ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) <= 33 %}Muy buena
        {% elif ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) <= 66 | float > 33 %}Buena
        {% elif ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) <= 99 | float > 66 %}Regular
        {% elif ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) <= 149 | float > 99 %}Mala
        {% elif ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) <= 200 | float > 149 %}Muy mala
        {% elif ((states.sensor.home_assistant_pm_2_5_concentration_mean.state | float) * 4.0) > 200 %}Peligrosa
        {%- endif %}
      entity_id: sensor.home_assistant_pm_2_5_concentration_mean  

Creamos un sensor que nos indicara la versión del software del sensor

- platform: rest
  name: Software Version
  entity_namespace: luftdaten
  resource: http://192.168.1.26/data.json
  method: GET
  force_update: true
  scan_interval: 600
  value_template: '{{ value_json["software_version"] }}'     

Ya habíamos visto como integrarlo en influxdb , ahora es el momento de poder extraer los datos que previamente vamos guardando en influxdb y trabajar con ellos y realizar las lectura promedio.

- platform: influxdb
  username: !secret influxdb_username
  password: !secret influxdb_password
  entity_namespace: !secret influxdb_database
  queries:
    - name: PM 10 Concentration
      unit_of_measurement: 'µg/m³'
      value_template: '{{ value | round(1) }}'
      group_function: last
      database: !secret influxdb_database
      measurement: feinstaub
      field: SDS_P1
      where: 'time > now() - 1h'
    - name: PM 10 Concentration Mean
      unit_of_measurement: 'µg/m³'
      value_template: '{{ value | round(1) }}'
      group_function: mean
      database: !secret influxdb_database
      measurement: feinstaub
      field: SDS_P1
      where: 'time > now() - 24h'
    - name: PM 2.5 Concentration
      unit_of_measurement: 'µg/m³'
      value_template: '{{ value | round(1) }}'
      group_function: last
      database: !secret influxdb_database
      measurement: feinstaub
      field: SDS_P2
      where: 'time > now() - 1h'
    - name: PM 2.5 Concentration Mean
      unit_of_measurement: 'µg/m³'
      value_template: '{{ value | round(1) }}'
      group_function: mean
      database: !secret influxdb_database
      measurement: feinstaub
      field: SDS_P2
      where: 'time > now() - 24h'
    - name: Outside Temperature
      unit_of_measurement: '°C'
      value_template: '{{ value | round(1) }}'
      group_function: last
      database: !secret influxdb_database
      measurement: feinstaub
      field: BME280_temperature
      where: 'time > now() - 1h'
    - name: Outside Humidity
      unit_of_measurement: '%'
      value_template: '{{ value | round(1) }}'
      group_function: last
      database: !secret influxdb_database
      measurement: feinstaub
      field: BME280_humidity
      where: 'time > now() - 1h'
    - name: Outside Pressure
      unit_of_measurement: 'mBar'
      value_template: '{{ value | round(1) }}'
      group_function: last
      database: !secret influxdb_database
      measurement: feinstaub
      field: BME280_pressure
      where: 'time > now() - 1h'

Le damos los valores en customize.yaml

sensor.estacion_luftdaten_pm2_5:
  friendly_name: Particulas < 2,5 micras
  icon: mdi:cloud-outline
  unit_of_measurement: 'µg/m³'
sensor.home_assistant_pm_2_5_concentration_mean:
  friendly_name: Promedio particulas < 2,5 micras
  icon: mdi:cloud-outline
  unit_of_measurement: 'µg/m³'
sensor.estacion_luftdaten_pm10:
  friendly_name: Particulas < 10 micras
  icon: mdi:cloud-outline  
  unit_of_measurement: 'µg/m³'
sensor.home_assistant_pm_10_concentration_mean:
  friendly_name: Promedio particulas < 10 micras
  icon: mdi:cloud-outline
  unit_of_measurement: 'µg/m³'
sensor.estacion_luftdaten_temperature:
  friendly_name: Temperatura
sensor.estacion_luftdaten_humidity:
  friendly_name: Humedad
sensor.estacion_luftdaten_pressure:
  friendly_name: Presión atm.
sensor.estacion_luftdaten_presion:
  friendly_name: Presión atm.
  icon: mdi:gauge
sensor.air_quality_pm25:
  friendly_name: Calidad aire PM 2.5
  icon: mdi:alpha-q-circle-outline
sensor.air_quality_pm10:
  friendly_name: Calidad aire 10
  icon: mdi:alpha-q-circle-outline
sensor.luftdaten_software_version:
  friendly_name: Versión software
  icon: mdi:animation-outline

Ya solo nos queda mostrarlo en nuestro panel

- type: entities
  title: Sensor calidad del aire
  show_name: true
  show_state: false
  show_header_toggle: false
  columns: 3     
  entities: 
    - sensor.estacion_luftdaten_pm2_5
    - sensor.home_assistant_pm_2_5_concentration_mean            
    - sensor.estacion_luftdaten_pm10
    - sensor.home_assistant_pm_10_concentration_mean            
    - sensor.estacion_luftdaten_temperature    
    - sensor.estacion_luftdaten_humidity
    - sensor.estacion_luftdaten_presion
    - sensor.air_quality_pm25
    - sensor.air_quality_pm10            
    - sensor.luftdaten_software_version 

Crearemos un par de automatizaciones para que nos indique los cambios a través de Telegram

- id: air_quality_pm10_changed
  alias: "Air Quality PM10 Changed"
  trigger:
    - platform: state
      entity_id: sensor.air_quality_pm10
  action:
    - service: notify.notif_telegram_bot
      data_template:
        message: "\U00002601\U00002601 Calidad del aire PM10 ha cambiado a : *{{ trigger.to_state.state }} (era : *{{ trigger.from_state.state }}*) . Calidad del aire PM2.5 es :*{{ states.binary_sensor.air_quality_pm25.state }}* \U00002601\U00002601"
          
          
- id: air_quality_pm25_changed
  alias: "Air Quality PM2.5 Changed"
  trigger:
    - platform: state
      entity_id: sensor.air_quality_pm25
  action:
    - service: notify.notif_telegram_bot
      data_template:
        message: "\U00002601\U00002601 Calidad del aire PM2.5 ha cambiado a : *{{ trigger.to_state.state }} (era : *{{ trigger.from_state.state }}*) . Calidad del aire PM10 es :*{{ states.binary_sensor.air_quality_pm10.state }}* \U00002601\U00002601"