Binary_sensor template always evaluates to off

I’m trying to generate a boolean value that is used to determine when a graph is displayed in a dashboard. The idea is that when the sensor.air_quality_index is above an input_number.aqi_graph_display_threshold, this would make the AQI graph appear in the dashboard.

In my templates.yaml file, I have a few sensors and binary_sensors that work well. But this one just can’t. I thought it might be due to the values changing but the template not re-evaluating, so I added the sensors as triggers, but that didn’t help. I though it might be due to an int not comparing to a float, so I tried forcing them both to float and int, but neither helped.

In today’s case, sensor.air_quality_index is 32, and input_number.aqi_graph_display_threshold is 12. So I would expect aqi_graph_display_threshold_met to evaluate to true. But it’s always false.

This one has me stumped. I’d appreciate any clues.

- trigger:
    - platform: numeric_state
      entity_id:
        - input_number.aqi_graph_display_threshold
        - sensor.air_quality_index
      above: 10
  binary_sensor:
    - name: aqi_graph_display_threshold_met
      state: "{{ (states('sensor.air_quality_index') | float(0)) > (states('input_number.aqi_graph_display_threshold') | float(0)) }}"

I’m guessing it’s because the values are ALREADY above 10. It will only re-evaluate when one of them goes from below to above 10.

Why are you using a trigger? Seems unnecessary versus having it just update everytime the AQI changes (so like a normal template sensor).

You don’t need a triggered template sensor for this. Template sensors update whenever an entity used in the template updates. So all you need is this:

- binary_sensor:
    - name: aqi_graph_display_threshold_met
      state: "{{ states('sensor.air_quality_index') | float(0) > states('input_number.aqi_graph_display_threshold') | float(0) }}"
      availability: "{{ has_value('sensor.air_quality_index') and has_value('input_number.aqi_graph_display_threshold') }}"

The availability template will prevent odd states if one or both of the entities are unavailable.

After making these changes look in developer tools states and check the value of binary_sensor.aqi_graph_display_threshold_met.

If it is off or unavailable then check the entity_ids of the sensors used in your template.

1 Like

Wow that totally worked (fixed a couple parenthesis issues). Thank you! I spent 2 days on it. I never tried the availability option.