hi,
using the air quality integration, I used this automation for starters, with a hardcoded from: Good
in the trigger platform to create a warning:
- alias: Aqi Alert
id: 'Aqi Alert'
trigger:
- platform: state
entity_id: sensor.chinese_air_pollution_level
from: 'Good'
- platform: state
entity_id: sensor.us_air_pollution_level
from: 'Good'
condition: []
action:
- service: notify.notify
data_template:
title: >
{{as_timestamp(now())|timestamp_custom('%X')}} : Aqi Alert!
message: >
{{trigger.to_state.name}} deteriorated from 'Good' to {{trigger.to_state.state}}
Check: https://www.airvisual.com/netherlands/north-brabant/huijbergen/huijbergen-vennekenstraat for more info
Of course that doesn’t suffice since there are a few more quality levels defined see https://www.home-assistant.io/integrations/waqi/#configuration-variables
So I made a more versatile automation based on any state change, with a templated from_ and to_state:
- alias: Aqi Alert
id: 'Aqi Alert'
trigger:
- platform: state
entity_id: sensor.chinese_air_pollution_level
- platform: state
entity_id: sensor.us_air_pollution_level
condition: []
action:
service: notify.notify
data_template:
title: >
{{as_timestamp(now())|timestamp_custom('%X')}} : Aqi Alert!
message: >
{{trigger.to_state.name}} changed from {{trigger.from_state.state}} to {{trigger.to_state.state}}
{% set url = 'https://www.airvisual.com/netherlands/north-brabant/huijbergen/huijbergen-vennekenstraatl' %}
Check [your local Air quality station]({{url}}) for more info
What I’d like however, is to only create a warning when deteriorating… how can I write the template so it takes the available conditions into account?
['Good','Moderate','Unhealthy for Sensitive Groups','Unhealthy','Very unhealthy','Hazardous']
should I eg create a mapper like
{% set mapper = {'Good':'5',
'Moderate':'4',
'Unhealthy for Sensitive Groups':'3',
'Unhealthy':'2',
'Very unhealthy':'1',
'Hazardous':'0' } %}
and then compare the
mapper[trigger.to_state.state] < mapper[trigger.from_state.state]
?
or, maybe even better use both improved and deteriorated, using a template like this:
action:
service: notify.notify
data_template:
title: >
{{as_timestamp(now())|timestamp_custom('%X')}} : Aqi Alert!
message: >
{% set mapper = {'Good':'5','Moderate':'4','Unhealthy for Sensitive Groups':'3',
'Unhealthy':'2','Very unhealthy':'1','Hazardous':'0' } %}
{{trigger.to_state.name}} {{'improved'
if mapper[trigger.to_state.state] > mapper[trigger.from_state.state]
else 'deteriorated'}} from {{trigger.from_state.state}} to {{trigger.to_state.state}}
{% set url = 'https://www.airvisual.com/netherlands/north-brabant/huijbergen/huijbergen-vennekenstraatl' %}
Check [your local Air quality station]({{url}}) for more info
might also need an added safety catch for the mapper?
please have a look if this could be the way to go? thanks!
@petro would you be so kind to check with me here? Not really sure if this is working now, seem not to get too many warnings anymore