Jinja template works in dev tools template, not in markdown card

Hello,

I am trying to show the count of open windows, and have this jinja snippet in my dev tools template editor which works just fine:

{{ states.binary_sensor
    | selectattr("attributes.device_class", "eq", "window")
    | selectattr("state", "eq", "on")
    | list
    | count
}} Open Windows

But when I paste the same in a markdown card, the card is always blank and the background flickers in and out. If I remove the device_class line, then it works, showing me all “on” binary_sensors, but I am only trying to show “on” windows.
Does anyone know what I’m doing wrong?

Thanks!

HA Core version: 2024.12.5
Frontend version: 20241127.8

There’s nothing wrong with that template, clear your cache and refresh the page. That may clear up issues.

Try adding “ selectattr(“attributes.device_class”, “defined”)” before.

3 Likes

I don’t quite understand the reason but markdown card uses a stricter template engine than developer tools, and it throws errors on things developer tools allows.

1 Like

FWIW, templates used in Template entities are also subjected to more stringent rules. In other words, the Template Editor can be more lax than anywhere else a template is processed.

What the OP encountered is the classic example of the disparity. When asked to select entities with a particular device_class value, the Template Editor doesn’t complain if it encounters entities without the attribute.

It’s more forgiving and simply excludes entities lacking a device_class attribute and continues processing the template.

Outside of the Template Editor, the system is not forgiving; it fails under the same circumstances.


Another disparity was the Template Editor supported the use of expand as either a filter or a function (which is how expand is documented to work). In contrast, the system only supported it as a function.

I reported it as an Issue over 2 years ago (expand fails as a filter when used in a Template Sensor). No replies. I held the Issue open for a year and then I gave up. The Issue eventually closed itself as ‘stale’. At some later date, the disparity was gone but I don’t know which PR fixed it.


EDIT 1

I overlooked to mention that years ago, there was no disparity and a template designed in the Template Editor worked exactly the same when used elsewhere.

My best guess is that it occurred in late 2021. Here’s an example in early 2021 where I suggested a Template Sensor (user tagged it as the Solution) that did not first check if device_class is defined.

At some point later, it became necessary to include the test (but not in the Template Editor). That disparity has been present ever since.

EDIT 2

According to petro, this PR is responsible for it (it appears to have been included in the May 2021 release).

1 Like

Thanks lldar, that did indeed fix it for me!
Is there a way/place I could see some kind of error or warning when the template fails? I checked the developer console in my browser but had no idea that device_class could sometimes be missing.

Thanks for this context. I would certainly hope that the two systems coalesce again sooner than later, especially when things fail silently and there’s no way for n00bs like me to understand what is going wrong.