Templated value with device_class filter not displaying in frontend

I am trying to display a card that shows the amount of open windows in specific areas. For that I have created the following code (areas currently not being filtered for testing purposes):

{% set open =
  states.binary_sensor
  | selectattr('attributes.device_class', 'eq', 'opening')
  | selectattr('state', 'eq', 'on')
  | list |length
 %}
{{ open }}

Within the developer tools this works great, resulting in 2, because 2 windows are currently open.

But when putting this code into a markdown card nothing is displayed. If, however, I remove the line filtering for device_class, then a value is displayed.

However, that value is of course not correct, because it then shows all binary_sensors that are on.

Does anybody know what is happening here? I am super confused as it works in dev tools but not in cards…

Select for objects that have the attribute defined first:

{% set open =
  states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'opening')
  | selectattr('state', 'eq', 'on')
  | list | length
 %}
{{ open }}
1 Like

You’re probably running into this issue Markdown card templates using stricter rendering than developer-tools · Issue #20862 · home-assistant/frontend · GitHub

1 Like

Oh that’s beautiful - thank you so much @Didgeridrew and @karwosts , this works like a charm! Couldn’t find this anywhere, but glad I understand this now :slight_smile: