home-assistant / home-assistant.io

:blue_book: Home Assistant User documentation
https://www.home-assistant.io
Other
4.88k stars 7.25k forks source link

Template in Markdown card results in error messages #29325

Open mdeweerd opened 1 year ago

mdeweerd commented 1 year ago

Feedback

With HA2023.9.3 I notice an error as shown below in the log from time to time.

So IMHO the example should be updated to avoid this.

2023-10-13 10:21:12.040 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% for package in states.sensor.seventeentrack_packages_in_transit.attributes.packages %}
>- **{{ package.friendly_name }} ({{ package.tracking_number }}):** {{ package.info_text }}
{% endfor %}) renders=594>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 568, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2198, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'packages'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 694, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 570, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'packages'

URL

https://www.home-assistant.io/integrations/seventeentrack/

Version

2023.10.2

Additional information

No response

euf0ria commented 2 months ago

Here is an updated example that works that you can use instead. Tested on 2024.7.3:

type: markdown
title: Packages in transit
content: >-
  {% for package in state_attr('sensor.17track_in_transit', 'packages') %}
  {{  package.friendly_name }} ({{ package.tracking_number }}): {{ package.info_text }}

  {% endfor %}