Template and value_template wrong configuration

Hey guys, I’m trying to set a remaning hours fan filter sensor.

  - platform: template
    sensors:
      filtro_purificatore:  
        value_template: >
            {% if is_state('state.fan.xiaomi_miio_device.filter_life_remaining', '15') %}
              Change filter
            {% else %}
              Filter ok
            {% endif %}

I can see the new sensor in HA, but it is stucked at ‘Filter ok’ even if filter_life_remaining is under 15

  - platform: template
    sensors:
      filtro_purificatore:  
        value_template: >
            {% if is_state_attr('state.fan.xiaomi_miio_device', 'filter_life_remaining', '15') %}
              Change filter
            {% else %}
              Filter ok
            {% endif %}

Though you might be better off with:

  - platform: template
    sensors:
      filtro_purificatore:  
        value_template: >
            {% if state_attr('state.fan.xiaomi_miio_device', 'filter_life_remaining')|int <16 %}
              Change filter
            {% else %}
              Filter ok
            {% endif %}

This way it will tell you to change the filter if the life remaining is 15 or less. In case you miss it when it is 15 and it changes to 14. In your original template this would result in “Filter OK” again.

1 Like

Thank you!

1 Like