Testing Door sensor with if, elif

Hi. Have a sensor that was behaving well before I tried to make it smarter by adding the “elif” portion below. I wanted to add the fail-safe of detecting no result in case there was a sensor malfunction or power outage.

  entity_picture_template: >-
    {% if is_state('switch.garage_door.attributes.sensor_state', 'on') %}
      /local/garage-open.png
    {%-elif is_state('switch.garage_door.attributes.sensor_state', 'off') %}
      /local/garage-closed.png
    {% else %}
      /local/alert-icon.png
    {% endif %}

However, when I use this code - the result returned is “else” - when “elif” should have been the correct direction.

This was how it looked when it behaved correctly…

  entity_picture_template: >-
    {% if is_state('switch.garage_door.attributes.sensor_state', 'on') %}
      /local/garage-open.png
    {% else %}
      /local/garage-closed.png
    {% endif %}

Any ideas what I am doing wrong?

is_state’s first parameter is an entity_id, so you can’t use it the way you’re trying.

However, there is an is_state_attr function you can use, like this:

is_state_attr('switch.garage_door','sensor_state','on')

Having said that, does your switch really have an attribute named sensor_state?

I seriously doubt this “worked”. is_state in this case would have always returned false because there is no entity with an entity_id of ‘switch.garage_door.attributes.sensor_state’.

Thanks for the advice! Indeed it does, It is a Belkin Wemo Maker.

You may be right - I’ll fully test, ie. Open the door and see if it changes. Doh!