I’m using an AIR-1 device, but that’s not really what’s important. What I want is to know when the air quality in my house is within certain ranges. I’m basing that off of 3 sensors and their relative ranges. I have a “good” range, a “moderate” range, a “bad” range, and a “very bad” range for all 3 sensors. The overall air quality is based on whichever one of these is worst. I want to announce on my Alexa device (that part’s easy) when the overall quality increases or decreases, but only if it’s stayed increased or decreased for more than, say, 5 minutes. If it spikes for 2 minutes, it’s really annoying to get an alert both when it goes up and when it goes back down again, especially if it’s something of an inherently brief duration like a cleaning spray.
I had an automation based on whether it had changed within the last 5 minutes, but that still announced that the air quality was good when it went down after a brief spike.
I have successfully set up a realtime sensor that tells me the overall quality based on which of them is worse. I think maybe I need two sensors, one that is my realtime “overall quality” and one that only updates if the first one stayed constant for 5 minutes.
So here is the code I have so far. The first sensor works, but the second sensor (the trigger-based one) is not working. It just keeps saying “Unknown” even when I restart HA and therefore the state of my realtime sensor went from “Unavailable” to “Good”. (I have also checked under Developer Tools > Template to make sure that the code of states('sensor.air1_overall_quality_rt') == "Good"
is evaluating to “true”.)
- sensor:
- name: Air-1 Overall Quality Realtime
unique_id: air1_overall_quality_rt
state: |-
{% if states('sensor.apollo_air_1_bac868_sen55_voc')|float < 200 and
states('sensor.apollo_air_1_bac868_pm_25m_weight_concentration')|float < 35.4 and
states('sensor.apollo_air_1_bac868_pm_10m_weight_concentration')|float < 55 %}
Good
{% elif states('sensor.apollo_air_1_bac868_sen55_voc')|float < 300 and
states('sensor.apollo_air_1_bac868_pm_25m_weight_concentration')|float < 35.4 and
states('sensor.apollo_air_1_bac868_pm_10m_weight_concentration')|float < 155 %}
Moderate
{% elif states('sensor.apollo_air_1_bac868_sen55_voc')|float < 400 and
states('sensor.apollo_air_1_bac868_pm_25m_weight_concentration')|float < 150.4 and
states('sensor.apollo_air_1_bac868_pm_10m_weight_concentration')|float < 355 %}
Bad
{% else %}
Very Bad
{% endif %}
- trigger:
- platform: template
id: Good
value_template: |-
{{ states('sensor.air1_overall_quality_rt') == "Good" }}
for: "00:05:00"
- platform: template
id: Moderate
value_template: |-
{{ states('sensor.air1_overall_quality_rt') == "Moderate" }}
for: "00:05:00"
- platform: template
id: Bad
value_template: |-
{{ states('sensor.air1_overall_quality_rt') == "Bad" }}
for: "00:05:00"
- platform: template
id: Very Bad
value_template: |-
{{ states('sensor.air1_overall_quality_rt') == "Very Bad" }}
for: "00:05:00"
sensor:
- name: Air-1 Overall Quality Metric
unique_id: air1_overall_quality
state: "{{ trigger.id }}"
What am I doing wrong?