home-assistant / frontend

:lollipop: Frontend for Home Assistant
https://demo.home-assistant.io
Other
3.98k stars 2.72k forks source link

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

Open erkr opened 4 months ago

erkr commented 4 months ago

Checklist

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'
  2. Created this markdown card:
    type: markdown
    content: >-
      {% set cnt = expand('binary_sensor.test_source') | rejectattr('attributes.device_class','eq','connectivity') | list | count   -%}
      {{ cnt > 0 }}
  3. 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 commented 4 months ago

Your template seems fine to me in a markdown card:

image

erkr commented 4 months ago

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 commented 4 months ago

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: https://github.com/home-assistant/frontend/pull/17824

karwosts commented 4 months ago

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.

erkr commented 4 months ago

@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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

github-actions[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

erkr commented 1 month ago

Bump