Template error at startup

I get an error for this template only once at startup:

type: entities
card_mod:
  style: |
    .card-header {
      font-size: 18px;
      margin: -10px 0px;
    }
    ha-card {
      height: 172px !important;
    }
    #states > * {
      margin: -1px 0px !important;
    }
entities:
  - entity: input_boolean.notification_erschutterung_16
    name: Erschütterung
    card_mod:
      style: |
        :host {
          {% if is_state(config.entity, 'on') %}
            --secondary-text-color: red;
            --paper-item-icon-color: red;
            --card-mod-icon: mdi:vibrate;
          {% else %}
            --card-mod-icon: mdi:crop-portrait;
          {% endif %}
        }
    type: custom:template-entity-row
    state: "{% if is_state(config.entity, 'on') %}Erkannt{% else %}Keine{% endif %}"
    secondary: >
      {% set relativeTime = relative_time([
        states.binary_sensor.vibrationssensor_16_erschutterung.last_updated,
        states.binary_sensor.vibrationssensor_16_fallenlassen.last_updated,
        states.binary_sensor.vibrationssensor_16_kippen.last_updated ] | max | default(0, true)) %}
      {% if 'second' in relativeTime %}
        {% set relativeTime = 'einigen Sekunden' %}
      {% endif %} Vor {{ relativeTime | replace("years", "Jahre") |
      replace("days", "Tagen") | replace("hours", "Stunden") |
      replace("minutes", "Minuten") | replace("year", "Jahr") | replace("day",
      "Tag") | replace("hour", "Stunde") | replace("minute", "Minute") }}
  - entity: sensor.vibrationssensor_16_batterie
    name: Batterie
    card_mod:
      style:
        hui-generic-entity-row $: |
          .state {
            color: {{ 'red' if states(config.entity) | int(default=0) <= 10 }};
          }
  - entity: number.vibrationssensor_16_empfindlichkeit
    name: Feinheit
title: Gartenlaube
grid_options:
  columns: 9
  rows: auto

Error:

Logger: homeassistant.helpers.event
Source: helpers/template.py:645
First occurred: 11:01:11 (38 occurrences)
Last logged: 11:01:11

Error while processing template: Template<template=({% set relativeTime = relative_time([ states.binary_sensor.vibrationssensor_16_erschutterung.last_updated, states.binary_sensor.vibrationssensor_16_fallenlassen.last_updated, states.binary_sensor.vibrationssensor_16_kippen.last_updated ] | max | default(0, true)) %} {% if 'second' in relativeTime %} {% set relativeTime = 'einigen Sekunden' %} {% endif %} Vor {{ relativeTime | replace("years", "Jahre") | replace("days", "Tagen") | replace("hours", "Stunden") | replace("minutes", "Minuten") | replace("year", "Jahr") | replace("day", "Tag") | replace("hour", "Stunde") | replace("minute", "Minute") }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2236, in strptime
    return datetime.strptime(string, fmt)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
TypeError: strptime() argument 1 must be str, not LoggingUndefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 643, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2756, 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 1, in top-level template code
  File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 401, in call
    return __context.call(__obj, *args, **kwargs)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2239, in strptime
    raise_no_default("strptime", string)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1932, in raise_no_default
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Template error: strptime got invalid input '' when rendering template '{% set relativeTime = relative_time(strptime(states.sensor.manfred_hohn_last_measurement_timestamp.state,'%Y-%m-%dT%H:%M:%S%z')) %} {% if 'second' in relativeTime %}
  {% set relativeTime = 'einigen Sekunden' %}
{% endif %} {{ states('sensor.manfred_hohn_measurement') }} mg/dl vor {{ relativeTime |  replace("years", "Jahre") | replace("days", "Tagen") |  replace("hours", "Stunden") | replace("minutes", "Minuten") |  replace("year", "Jahr") | replace("day", "Tag") | replace("hour", "Stunde") | replace("minute", "Minute") }}' but no default was specified

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 760, 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 645, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: strptime got invalid input '' when rendering template '{% set relativeTime = relative_time(strptime(states.sensor.manfred_hohn_last_measurement_timestamp.state,'%Y-%m-%dT%H:%M:%S%z')) %} {% if 'second' in relativeTime %}
  {% set relativeTime = 'einigen Sekunden' %}
{% endif %} {{ states('sensor.manfred_hohn_measurement') }} mg/dl vor {{ relativeTime |  replace("years", "Jahre") | replace("days", "Tagen") |  replace("hours", "Stunden") | replace("minutes", "Minuten") |  replace("year", "Jahr") | replace("day", "Tag") | replace("hour", "Stunde") | replace("minute", "Minute") }}' but no default was specified

Any idea what’s wrong?

You’re looking at the wrong template.

this template has the issue

{% set relativeTime = relative_time(strptime(states.sensor.manfred_hohn_last_measurement_timestamp.state,'%Y-%m-%dT%H:%M:%S%z')) %}
{% if 'second' in relativeTime %}
  {% set relativeTime = 'einigen Sekunden' %}
{% endif %}
{{ states('sensor.manfred_hohn_measurement') }} mg/dl vor {{ relativeTime |  replace("years", "Jahre") | replace("days", "Tagen") |  replace("hours", "Stunden") | replace("minutes", "Minuten") |  replace("year", "Jahr") | replace("day", "Tag") | replace("hour", "Stunde") | replace("minute", "Minute") }}

The issue is that states.sensor.manfred_hohn_last_measurement_timestamp.state is empty at startup, you need to guard against that.

That worked. Many thanks. Here is code I’m using now:

entities:
  - entity: zone.home
    name: Personen Zuhause
  - type: section
    label: " "
  - entity: person.mhoehn
    type: custom:template-entity-row
    state: |
      {% if is_state('person.mhoehn', 'home') %}
        Zuhause
      {% elif is_state('person.mhoehn', 'not_home') %}
        Außerhalb
      {% else %}
        Unbekannt
      {% endif %}
    secondary: >
      {% set relativeTime =
      relative_time(strptime(states.sensor.manfred_hohn_last_measurement_timestamp.state,'%Y-%m-%dT%H:%M:%S%z')
      | default (0, true)) %} {% if 'second' in relativeTime %}
        {% set relativeTime = 'einigen Sekunden' %}
      {% endif %} {{ states('sensor.manfred_hohn_measurement') }} mg/dl vor {{
      relativeTime | replace("years", "Jahre") | replace("days", "Tagen") |
      replace("hours", "Stunden") | replace("minutes", "Minuten") |
      replace("year", "Jahr") | replace("day", "Tag") | replace("hour",
      "Stunde") | replace("minute", "Minute") }}