Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown card templates using stricter rendering than developer-tools #20862

Open
3 of 4 tasks
erkr opened this issue May 24, 2024 · 8 comments
Open
3 of 4 tasks

Markdown card templates using stricter rendering than developer-tools #20862

erkr opened this issue May 24, 2024 · 8 comments

Comments

@erkr
Copy link

erkr commented May 24, 2024

Checklist

  • I have updated to the latest available Home Assistant version.
  • I have cleared the cache of my browser.
  • I have tried a different browser to see if it is related to my browser.
  • I have tried reproducing the issue in safe mode to rule out problems with unsupported custom resources.

Describe the issue you are experiencing

I have a simple template that filters on the 'device_class' attribute of an entity. That template works fine in e.g dev tools or as an template sensor, but fails in the markdown card for entities that don't have a 'device_class'.

Describe the behavior you expected

Template works the same as in Dev Tools or in the template integration. I also verified a number of other cards that support templates, and there it works fine. So it seems a markdown card specific issue.

Steps to reproduce the issue

  1. Created this test template entity without a device_class:
- binary_sensor:
  - name: test_source
    state: 'on'
  1. Created this markdown card:
type: markdown
content: >-
      {% set cnt = expand('binary_sensor.test_source') | rejectattr('attributes.device_class','eq','connectivity') | list | count   -%}
      {{ cnt > 0 }}
  1. Results in this error:
    image

What version of Home Assistant Core has the issue?

Core 2024.5.4

What was the last working version of Home Assistant Core?

No response

In which browser are you experiencing the issue with?

Chrome, Safari

Which operating system are you using to run this browser?

Win11 aan iOS 17

State of relevant entities

No response

Problem-relevant frontend configuration

No response

Javascript errors shown in your browser console/inspector

No response

Additional information

No response

@karwosts
Copy link
Contributor

Your template seems fine to me in a markdown card:

image

@erkr
Copy link
Author

erkr commented May 24, 2024

Your template seems fine to me in a markdown card:

Thanks for verifying, but it only fails for entities where the device class is missing.

@karwosts
Copy link
Contributor

karwosts commented May 24, 2024

Yes I see what you mean. It seems markdown card subscribes its template with an additional argument strict: true, and developer tools does not.

Related: #17824

@karwosts
Copy link
Contributor

So it seems that strict mode for devtools was replaced with an option report_errors, but report_errors seems to catch less things than strict mode.

Perhaps that should be reported as a core bug, that report_errors should be able to report everything that strict mode would catch. I assume that was its intention but I don't know.

@karwosts karwosts changed the title Markdown card template failure Markdown card templates using stricter rendering than developer-tools May 24, 2024
@erkr
Copy link
Author

erkr commented May 24, 2024

@karwosts
Nice it seems your identifiers the source of this issue.
But one remark. Rejectattr and selectattr should not raise errors when those attributes don't exist (to my knowledge)?! They will simply not be selected or rejected (like dev tools and template sensors). That is why I raised the issue here.

@ildar170975
Copy link
Contributor

Rejectattr and selectattr should not raise errors when those attributes don't exist (to my knowledge)?! They will simply not be selected or rejected (like dev tools and template sensors).

My first experiments with selectattr / rejectattr were on 2021 - and on that time I learned that I must check for an attribute existence before using selectattr/rejectattr.

@erkr
Copy link
Author

erkr commented May 25, 2024

My first experiments with selectattr / rejectattr were on 2021 - and on that time I learned that I must check for an attribute existence before using selectattr/rejectattr.

I agree it is not very robust, But How to do that in filter expressions?! When I lookup a value with state_attr(), I have to check, but in a filter I want to include/exclude entities that match.
I checked several places where I can execute templates (dev tools, template sensors, state switch card etc) and the same filtering works for all, except the markdown card.

Update: @ildar170975 thanks for your hint. This combo of filters works in the markdown as well:

selectattr('attributes.device_class', 'defined') |selectattr('attributes.device_class','eq','connectivity')

It complicates my full template, as I also want the entity's that don't have a device_class attribute (so need to merge two lists). But it's a workaround. Still hope the issue gets solved though.

@erkr
Copy link
Author

erkr commented May 25, 2024

Other simple example:

{% set entities = states | rejectattr('attributes.device_class','ne','connectivity')| map(attribute='entity_id') | list    -%}
{{ entities }}

auto entities card works fine:
image
Markdown card with the same template fails:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants