Defined 'date' attribute of Waste Collection sensor not available in template

As a new user of HA I hit an issue with the waste collection integration (or at least the sensor attributes related to that).

I have defined a sensor that received the schedule from the waste collection integration:

State attributes of `sensor.upcoming`
types:
  - GFT
  - OPK
  - PMD
upcoming:
  - date: "2025-06-20"
    icon: null
    picture: /local/waste/plastic.png
    type: PMD
  - date: "2025-06-27"
    icon: mdi:leaf
    picture: /local/waste/organic_waste.png
    type: GFT
  - date: "2025-06-30"
    icon: null
    picture: /local/waste/paper.png
    type: OPK
  - date: "2025-07-04"
    icon: null
    picture: /local/waste/plastic.png
    type: PMD
  ...

In a Mushroom Template card the following Jinja2 expression is included:

{% set date = as_datetime(state_attr('sensor.upcoming','upcoming')[0].date) %}
Over {{ (date.date() - now.date()).days }} dagen

to show info about the next wast collection.
However the logfile shows the following error:

ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set date = as_datetime(state_attr('sensor.upcoming','upcoming')[0].date) %}
Over {{ (date.date() - now.date()).days }} dagen) renders=70>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 644, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2969, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 399, in call
    if not __self.is_safe_callable(__obj):
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 3446, in is_safe_callable
    ) or super().is_safe_callable(obj)
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 265, in is_safe_callable
    getattr(obj, "unsafe_callable", False) or getattr(obj, "alters_data", False)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'function object' has no attribute 'date'

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 761, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        variables, strict=strict, log_fn=log_fn, **kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 646, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'function object' has no attribute 'date'

What am I missing here?

Not sure the source of the code, but start with this as an example

{% set date = as_datetime(state_attr('sun.sun','next_dawn'))%}
{{ date - now() }}

Use Developer Tools to look at your sensor attributes in the States tab and verify your code in the Template tab

It’s complaining about the now.date(): you’re trying to look up the date attribute of the now function but there isn’t one.

That code should be modified to replace now.date() with now().date() — which runs the date() instance method built into the datetime object that is returned by the now() function:

2 Likes

Grr, rookie mistake. Thanks!