Templates not working since 12.2023 Update

Hello Guys,

my tempaltes are not working anymore since 12.2023, can you guys assist me to correct it? Thanks

PS: Only Date, binary_sensor and Present Tempaltes are working

Updated:

- sensor:
    #Datum & Uhrzeit Formatiert
    - name: "Date & Time"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}
    - name: "Date"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%d.%m.%Y') }}
    - name: "Time"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%H:%M Uhr') }}

    #Backup
    - name: "Last Backup Format"
      state: >
        {{ as_timestamp(state_attr('sensor.backup_state', 'last_uploaded'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}
    - name: "Next Backup Format"
      state: >
        {{ as_timestamp(state_attr('sensor.backup_state', 'next_backup'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}

    #Comet DECT Current Temperature
    - name: "Wohnzimmer Comet DECT Current Temperature"
      unique_id: "Wohnzimmer Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'current_temperature' ) }}
    - name: "Schlafzimmer Comet DECT Current Temperature"
      unique_id: "Schlafzimmer Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'current_temperature' ) }}
    - name: "Küche Comet DECT Current Temperature"
      unique_id: "Küche Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'current_temperature' ) }}

    #Comet DECT Battery Level
    - name: "Wohnzimmer Comet DECT Battery Level"
      unique_id: "Wohnzimmer Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'battery_level' ) }}
    - name: "Schlafzimmer Comet DECT Battery Level"
      unique_id: "Schlafzimmer Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'battery_level' ) }}
    - name: "Küche Comet DECT Battery Level"
      unique_id: "Küche Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'battery_level' ) }}

    #Feiertage - Parameter https://community.home-assistant.io/t/convert-date-and-time-template/99328/2
    - name: "Feiertage Next Holiday"
      state: >
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
    - name: "Feiertage Next Date"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default = 0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Feiertage Next Date + Holiday"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default = 0) | timestamp_custom('%d.%m.%Y') }} -
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
    - name: "Feiertage Next Date + InDays"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default = 0) | timestamp_custom('%a, %d.%m.%Y') }}
        ({{ states('calendar.feiertage') }})
    - name: "Feiertage Next Date + Holiday + InDays"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default = 0) | timestamp_custom('%a, %d.%m.%Y') }} -
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
        ({{ states('calendar.feiertage') }})

    #Schulferien - Parameter https://community.home-assistant.io/t/convert-date-and-time-template/99328/2
    - name: "Schulferien Beginn"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Ende"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Beginn + Ende + InDays"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }} -
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }} 
        ({{ state_attr('sensor.ics_1', 'remaining') | default(0) }})

- binary_sensor:
    #Comet DECT Window Open
    - name: "Wohnzimmer Comet DECT Window Open"
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'window_open' ) }}
    - name: "Schlafzimmer Comet DECT Window Open"
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'window_open' ) }}
    - name: "Küche Comet DECT Window Open"
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'window_open' ) }}

    #Present Check
    - name: "Present_Wifi"
      state: >
        {{ is_state('device_tracker.martin_pixel_8', 'home')
          or is_state('device_tracker.martin_pixel_8', 'home') }}

    - name: "Present_GPRS"
      state: >
        {{ is_state('person.martin', 'home')
          or is_state('person.martin', 'home') }}

Look at the errors in Settings → Logs

here i have only this error log:

Logger: homeassistant.config
Source: config.py:552
First occurred: 09:39:42 (1 occurrences)
Last logged: 09:39:42

Invalid config for 'template' at templates.yaml, line 90: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '=') for dictionary value 'sensor->18->state', got "{{ as_timestamp(state_attr('sensor.ics_1', 'start'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y') }} ({{ state_attr('sensor.ics_1', 'remaining') | default = 0 }})\n"

That shows the new error reporting is pretty good. It says directly what your problem is.

The syntax for as_timestamp assumes the default to be a time as a number. You have put default = something which is a string it cannot interpret as a number. In reality you have tried to out epoch or 0 as the default. You can just replace the whole thing by a 0. The number 0 no quotes around it.

EDIT: See Petro’s answer just below for a correction of my statement. Thanks Petro

1 Like

Your last default in the template is invalid syntax. Should be | default(0)

2 Likes

thanks guys, now the other tempaltes are available again, but i have 2 new errors :sweat_smile:
Is my default value right or what you would prefer? (default = ‘1970-01-01T00:00:00Z’)
1:

Logger: homeassistant.helpers.event
Source: helpers/template.py:571
First occurred: 13:40:34 (3 occurrences)
Last logged: 13:40:54

Error while processing template: Template<template=({{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }} ({{ state_attr('sensor.ics_1', 'remaining' ) }})) renders=2>
Error while processing template: Template<template=({{ as_timestamp(state_attr('sensor.backup_state', 'last_uploaded'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}) renders=2>
Error while processing template: Template<template=({{ as_timestamp(state_attr('sensor.backup_state', 'next_backup'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1822, in forgiving_as_timestamp
    return dt_util.as_timestamp(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 135, in as_timestamp
    raise ValueError("not a valid date/time.")
ValueError: not a valid date/time.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2247, 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
  File "/usr/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1825, in forgiving_as_timestamp
    raise_no_default("as_timestamp", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1627, in raise_no_default
    raise ValueError(
ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }}  ({{ state_attr('sensor.ics_1', 'remaining' ) }})' 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 695, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 571, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }}  ({{ state_attr('sensor.ics_1', 'remaining' ) }})' but no default was specified

2:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:194
Integration: Template (documentation, issues)
First occurred: 13:40:54 (2 occurrences)
Last logged: 13:40:54

TemplateError('ValueError: Template error: timestamp_custom got invalid input '1970-01-01T00:00:00Z' when rendering template '{{ as_timestamp(state_attr('sensor.backup_state', 'last_uploaded'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}' but no default was specified') while processing template 'Template<template=({{ as_timestamp(state_attr('sensor.backup_state', 'last_uploaded'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.last_backup_format'
TemplateError('ValueError: Template error: timestamp_custom got invalid input '1970-01-01T00:00:00Z' when rendering template '{{ as_timestamp(state_attr('sensor.backup_state', 'next_backup'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}' but no default was specified') while processing template 'Template<template=({{ as_timestamp(state_attr('sensor.backup_state', 'next_backup'), default = '1970-01-01T00:00:00Z') | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.next_backup_format'

fix like this?

        value_template: >-
          {% set last_uploaded = state_attr('sensor.backup_state', 'last_uploaded') | as_timestamp %}
          {{ last_uploaded | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}
  1. You’re mising default values for as_timestamp
  2. As_timestamp default value should be an integer, in your 2nd two templates you’re returning a datetime string.

You can fix all of these by making default=0 for as_timestamp

thanks for reply, i have set the default value before and now i updated to default=0
but the error is the same, i hope i understand it right

    #Schulferien - Parameter https://community.home-assistant.io/t/convert-date-and-time-template/99328/2
    - name: "Schulferien Beginn"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Ende"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Beginn + Ende + InDays"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default=0) | timestamp_custom('%d.%m.%Y') }} -
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default=0) | timestamp_custom('%d.%m.%Y') }} 
        ({{ state_attr('sensor.ics_1', 'remaining') | default(0) }})

that’s correct and none of those will error

this error is in the protokoll, after reboot (with the last code)

Logger: homeassistant.helpers.event
Source: helpers/template.py:571
First occurred: 13:54:21 (1 occurrences)
Last logged: 13:54:21

Error while processing template: Template<template=({{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }} ({{ state_attr('sensor.ics_1', 'remaining' ) }})) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1822, in forgiving_as_timestamp
    return dt_util.as_timestamp(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 135, in as_timestamp
    raise ValueError("not a valid date/time.")
ValueError: not a valid date/time.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2247, 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
  File "/usr/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1825, in forgiving_as_timestamp
    raise_no_default("as_timestamp", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1627, in raise_no_default
    raise ValueError(
ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }}  ({{ state_attr('sensor.ics_1', 'remaining' ) }})' 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 695, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 571, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ as_timestamp(state_attr('sensor.ics_1', 'start')) | timestamp_custom('%a, %d.%m.%Y') }} - {{ as_timestamp(state_attr('sensor.ics_1', 'end')) | timestamp_custom('%d.%m.%Y') }}  ({{ state_attr('sensor.ics_1', 'remaining' ) }})' but no default was specified

You’ve posted about 8 similar errors and you aren’t reading them. Look at the last line of the error, it tells you what template to search for.

This is a new template that you haven’t posted yet. It’s the exact same errors as before. Missing defaults.

i have read it, but i have all defaults set.
he say the “sensor.ics_1” tempalte has no defualt but it is there

- sensor:
    #Datum & Uhrzeit Formatiert
    - name: "Date & Time"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso'), default=0) | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}
    - name: "Date"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Time"
      state: >
        {{ as_timestamp(states('sensor.date_time_iso'), default=0) | timestamp_custom('%H:%M Uhr') }}

    #Backup
    - name: "Last Backup Format"
      state: >
        {{ as_timestamp(state_attr('sensor.backup_state', 'last_uploaded'), default=0) | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}
    - name: "Next Backup Format"
      state: >
        {{ as_timestamp(state_attr('sensor.backup_state', 'next_backup'), default=0) | timestamp_custom('%d.%m.%Y - %H:%M Uhr') }}

    #Comet DECT Current Temperature
    - name: "Wohnzimmer Comet DECT Current Temperature"
      unique_id: "Wohnzimmer Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'current_temperature' ) }}
    - name: "Schlafzimmer Comet DECT Current Temperature"
      unique_id: "Schlafzimmer Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'current_temperature' ) }}
    - name: "Küche Comet DECT Current Temperature"
      unique_id: "Küche Comet DECT Current Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'current_temperature' ) }}

    #Comet DECT Battery Level
    - name: "Wohnzimmer Comet DECT Battery Level"
      unique_id: "Wohnzimmer Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'battery_level' ) }}
    - name: "Schlafzimmer Comet DECT Battery Level"
      unique_id: "Schlafzimmer Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'battery_level' ) }}
    - name: "Küche Comet DECT Battery Level"
      unique_id: "Küche Comet DECT Battery Level"
      unit_of_measurement: "%"
      device_class: battery
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'battery_level' ) }}

    #Feiertage - Parameter https://community.home-assistant.io/t/convert-date-and-time-template/99328/2
    - name: "Feiertage Next Holiday"
      state: >
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
    - name: "Feiertage Next Date"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Feiertage Next Date + Holiday"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default=0) | timestamp_custom('%d.%m.%Y') }} -
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
    - name: "Feiertage Next Date + InDays"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default=0) | timestamp_custom('%a, %d.%m.%Y') }}
        ({{ states('calendar.feiertage') }})
    - name: "Feiertage Next Date + Holiday + InDays"
      state: >
        {{ as_timestamp(state_attr('calendar.feiertage', 'next_date'), default=0) | timestamp_custom('%a, %d.%m.%Y') }} -
        {{ state_attr('calendar.feiertage', 'next_holiday' ) }}
        ({{ states('calendar.feiertage') }})

    #Schulferien - Parameter https://community.home-assistant.io/t/convert-date-and-time-template/99328/2
    - name: "Schulferien Beginn"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Ende"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default=0) | timestamp_custom('%d.%m.%Y') }}
    - name: "Schulferien Beginn + Ende + InDays"
      state: >
        {{ as_timestamp(state_attr('sensor.ics_1', 'start'), default=0) | timestamp_custom('%d.%m.%Y') }} -
        {{ as_timestamp(state_attr('sensor.ics_1', 'end'), default=0) | timestamp_custom('%d.%m.%Y') }} 
        ({{ state_attr('sensor.ics_1', 'remaining') | default(0) }})

- binary_sensor:
    #Comet DECT Window Open
    - name: "Wohnzimmer Comet DECT Window Open"
      state: >
        {{ state_attr('climate.wohnzimmer_comet_dect', 'window_open' ) }}
    - name: "Schlafzimmer Comet DECT Window Open"
      state: >
        {{ state_attr('climate.schlafzimmer_comet_dect', 'window_open' ) }}
    - name: "Küche Comet DECT Window Open"
      state: >
        {{ state_attr('climate.kuche_comet_dect', 'window_open' ) }}

    #Present Check
    - name: "Present_Wifi"
      state: >
        {{ is_state('device_tracker.martin_pixel_8', 'home')
          or is_state('device_tracker.martin_pixel_8', 'home') }}

    - name: "Present_GPRS"
      state: >
        {{ is_state('person.martin', 'home')
          or is_state('person.martin', 'home') }}

Are you sure you’re not looking at an old error?

yep, i cleanup up and restarted.