Blueprint for binary sensor that is on when a timestamp sensor is soon

I am trying to use the new template entity blueprint feature that came with 2024.11. The goal is to have a binary sensor that is on when a timestamp sensor is soon – with a blueprint input for how much soon is.

I came up with the following blueprint:

blueprint:
  name: Create a binary sensor based on the temporal distance to a timestamp sensor
  description: bla
  domain: template
  input:
    reference_entity:
      name: Timestamp sensor to use
      description: The sensor which needs to be observed
      selector:
        entity:
          domain: sensor
          device_class: timestamp
    distance:
      name: What distance to use
      description: blubb
      selector:
        duration:
variables:
  reference_entity: !input reference_entity
  dt: '{{ states("sensor.restabfallsammlung")|as_datetime }}'
  delta: "{{ timedelta(days=distance.days, hours=distance.hours, minutes=distance.minutes) }}"
binary_sensor:
  state: >-
    {% set dist = dt - now() %}
    {% if dist < delta %}
      on
    {% else %}
      off
    {% endif %}
  availability: "{{ states(reference_entity) not in ('unknown', 'unavailable') }}"

and I use it like this:

- unique_id: F384C743-330B-44BB-AA37-8547320F0D13
  name: Anzeige Restabfallsammlung
  use_blueprint:
    path: dev/in_the_next.yaml
    input:
      reference_entity: sensor.restabfallsammlung
      distance:
        days: 10
        minutes: 0
        hours: 0

But this does not work, and I cannot figure out why. The log says:

Logger: homeassistant.components.binary_sensor
Quelle: helpers/entity_platform.py:599
Integration: Binärsensor (Dokumentation, Probleme)
Erstmals aufgetreten: 08:12:38 (8 Vorkommnisse)
Zuletzt protokolliert: 08:23:58

Error adding entity binary_sensor.anzeige_restabfallsammlung for domain binary_sensor with platform template
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 633, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported type for timedelta minutes component: LoggingUndefined

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 599, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 918, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1367, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/template/binary_sensor.py", line 271, in async_added_to_hass
    await super().async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 578, in async_added_to_hass
    async_at_start(self.hass, self._async_template_startup)
  File "/usr/src/homeassistant/homeassistant/helpers/start.py", line 71, in async_at_start
    return _async_at_core_state(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/start.py", line 36, in _async_at_core_state
    hass.async_run_hass_job(at_start_job, hass)
  File "/usr/src/homeassistant/homeassistant/core.py", line 949, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 491, in _async_template_startup
    **self._render_variables(),
      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 347, in _render_variables
    return self._run_variables.async_render(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 254, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 635, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported type for timedelta minutes component: LoggingUndefined

To me, this seems to indicate two problems:

  1. Something with the minutes component of the timedelta object. I replicated the exact same template in the template playground, but there it works fine.

  2. Some logging component seems to be missing. I think this is not fixable on my end, and could be a bug in the core.

Any ideas?

I figured it out. This blueprint seems to work.

blueprint:
  name: Create a binary sensor based on the temporal distance to a timestamp sensor
  description: bla
  domain: template
  input:
    reference_entity:
      name: Timestamp sensor to use
      description: The sensor which needs to be observed
      selector:
        entity:
          domain: sensor
          device_class: timestamp
    temporal_distance:
      name: What distance to use
      description: blubb
      selector:
        duration:
variables:
  temporal_distance: !input temporal_distance
  reference_entity: !input reference_entity
binary_sensor:
  state: >-
    {% set dt = as_datetime(states(reference_entity)) %}
    {% set delta = timedelta(hours=temporal_distance.hours, days=temporal_distance.days) %}
    {% set dist = dt - now() %}
    {% if dist < delta %}
      on
    {% else %}
      off
    {% endif %}
  availability: "{{ states(reference_entity) not in ('unknown', 'unavailable') }}"

The tricky thing was that there does not seem to be a way to force these blueprints to reload. What I did in the end was renaming the blueprint all the time, and then reloading all the templates. In case anyone stumbles upon the same problem …