Having trouble with trigger-based sensors

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?

Your Trigger-based Template Sensor references sensor.air1_overall_quality_rt.

I think your Template Sensor’s entity_id is sensor.air1_overall_quality_realtime. You can check by going to Developer Tools > States and finding it there.

That’s normal if its triggers are referencing a non-existent entity_id.

It will only report a nominal value after it has been triggered.

Nope, it’s sensor.air1_overall_quality_rt.

Did you at some point in time rename their entity_id option?

I’m asking because, when supplied name and unique_id, Home Assistant automatically uses the value of name to create the sensor’s entity_id, not the value of unique_id.

In other words it will create a slug from “Air-1 Overall Quality Realtime” which will look like air_1_overall_quality_realtime.

Alternately, did the sensor’s name originally end with rt and you changed it after the sensor had been created?

If you did none of those things, and the sensor’s entity_id was derived automatically from its unique_id, then your instance of Home Assistant has behaved in a way that doesn’t correspond to known naming rules.


At any rate, a Trigger-based Template Sensor initially reports unknown until it’s triggered.

You may wish to try this version. It uses a State Trigger to detect when the sensor’s state changes to any value that remains unchanged for at least 5 minutes. Then it reports that value.

- trigger:
    - platform: state
      entity_id: sensor.air1_overall_quality_rt
      to:
      for:
        minutes: 5
  sensor:
    - name: Air-1 Overall Quality Metric
      unique_id: air1_overall_quality
      state: "{{ trigger.to_state.state }}"

Keep in mind that neither your version or mine will report anything until the sensor’s state changes and then remains in the changed state for at least 5 minutes. For example, your air quality sensor is currently reporting Good. It won’t serve to trigger your Template Trigger or my State Trigger until it changes to another value (and maintains it for at least 5 minutes).

Ah. Yes. I renamed it based on the unique_id. I thought it was supposed to use that, and I wondered why it kept messing up, so I “fixed” it.

Does it count as a state change when I reboot the system and it was briefly “Unavailable”?

The code you gave me is working! It seems like rebooting isn’t causing a state change on the main sensor away from “Good” (it used to earlier in my development, but I’m not sure what happened to change that). I’m fine with that. Thanks for helping!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.