Catch Error in Wait Template

I like to use this Wait Template:

{{ (states.light.k_fenster.attributes.brightness | int) == 1 }}

Wenn the light is on, it works fine and gives a true. But when light is off i got a error message:

UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘brightness’

How do i prevent this message in wait template?

{% if "brightness" in states.light.k_fenster.attributes %}
  {{ (states.light.k_fenster.attributes.brightness | int) == 1 }}
{% else %}
  false
{% endif %}

or even:

{{ state_attr('light.k_fenster','brightness') == 1 }}

An attribute can be a number, so no need for the |int, and state_attr() protects against non-existent attributes. See the warning box at the end of this section: Templating - Home Assistant

1 Like

Great! Thank you, it works.

I´m sorry, i’m new and I still don’t understand some of the basics.

No need to apologise, we’re all here to learn. You can try out your templates in the template tool, which is how I confirmed the second bit of my answer using a suitable entity on my system:

1 Like