Log Error - Error while passing template

Hi everyone

I have the following error in my log. How can I fix this?

It talks about the use of the an unexpected curly brace. I can’t see where this is.

Please help.

Logger: homeassistant.helpers.event
Source: helpers/template.py:514
First occurred: 12:17:49 (55 occurrences)
Last logged: 12:24:40

Error while processing template: Template<template=({%- if has_value('sensor.bicester_village_station_buses') -%} {%- if (state_attr('sensor.bicester_village_station_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %} {%- endi -%}) renders=2>
Error while processing template: Template<template=({%- if has_value('sensor.bicester_village_station_buses') -%} {%- if (state_attr('sensor.bicester_village_station_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=6>
Error while processing template: Template<template=({%- if has_value('sensor.bicester_village_station_buses') -%} {%- if (state_attr('sensor.bicester_village_station_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=10>
Error while processing template: Template<template=({%- if (state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %} {%- endif -%}) renders=2>
Error while processing template: Template<template=({%- if (state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %} {%- endif -%}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 512, in ensure_valid
    self._compiled_code = self._env.compile(self.template)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2607, in compile
    cached = self.template_cache[source] = super().compile(source)
                                           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: unexpected '}'

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 562, in async_render
    compiled = self._compiled or self._ensure_compiled(limited, strict, log_fn)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 765, in _ensure_compiled
    self.ensure_valid()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in ensure_valid
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: unexpected '}'
  1. line, last {% endif%} is missing an ‘f’.

Hi @pedolsky

Thanks for your help. The funny thing is that the {%- endi -%} was only put in to see if I could cure another log error. Can you help with this one:

Logger: homeassistant.helpers.event
Source: helpers/template.py:570
First occurred: 12:17:49 (106 occurrences)
Last logged: 12:17:49

Error while processing template: Template<template=({%- if (state_attr('sensor.bicester_village_station_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=112>
Error while processing template: Template<template=({%- if (state_attr('sensor.bicester_village_station_concourse_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th style="overflow: hidden; text-overflow: ellipsis">Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_concourse_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_concourse_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_concourse_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=28>
Error while processing template: Template<template=({%- if (state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=44>
Error while processing template: Template<template=(<table style="text-align:left"> <tr><th style="width:65%">Destination</th><th style="width:20%">Expected</th><th style="width:20%">Platform</th></tr> {% for train in state_attr('sensor.train_schedule_bit_oxf','trains') %} <tr><td style="width:60%">{{ train.terminus }}</td> <td style="width:20%">{{ (train.expected | string)[11:16] }}</td> <td style="width:20%">{% if train.platform is not none %} {{ train.platform }} {% endif %} </td></tr> {%- endfor %} </table><br /> <table style="text-align:left"> <tr><th style="width:65%">Destination</th><th style="width:20%">Expected</th><th style="width:20%">Platform</th></tr> {% for train in state_attr('sensor.train_schedule_bit_myb','trains') %} <tr><td style="width:60%">{{ train.terminus }}</td> <td style="width:20%">{{ (train.expected | string)[11:16] }}</td> <td style="width:20%">{% if train.platform is not none %} {{ train.platform }} {% endif %} </td></tr> {%- endfor %} </table>) renders=18>
Error while processing template: Template<template=({%- if (state_attr('sensor.bicester_village_station_concourse_buses', 'calls') | count) == 0 %} Information is not available {%- else %} <table style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th style="overflow: hidden; text-overflow: ellipsis">Destination</th><th>Expected</th> </tr> {%- for call in state_attr('sensor.bicester_village_station_concourse_buses', 'calls') %} <tr> <td>{{ call.service_description }}</td> <td style="max-width: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ call.destination }}</td> <td style="width: 20%; white-space: nowrap"> {%- if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if state_attr('sensor.bicester_village_station_concourse_buses', 'messages') is not none %} <br /> {% for message in state_attr('sensor.bicester_village_station_concourse_buses', 'messages') %} <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor %} {% endif %} {% endif %}) renders=30>
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
TypeError: object of type 'NoneType' has no len()

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: TypeError: object of type 'NoneType' has no len()

Probably that both sensors. When the template gets reloaded, value is none.

So, do I need to use has_value() ?

Your templates should at first check, whether the attribute (already) exists or not. I have not much experience with has_value but in my logic it should work :innocent:

Thank you. It’s not working yet but I’ll get there.

Can you post your code? The error message is cumbersome to read.

Thanks @pedolsky

There are three parts. Apologies fir the formatting but it has become a bit mangled.

type: vertical-stack
cards:
  - type: custom:html-template-card
    title: Manorsfield Road (3)
    ignore_line_breaks: false
    content: >-
      {%- if  (state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') |
      count) == 0 %} Information is not available {%- else %} <table
      style="text-align: left"> <tr> <th style="width:
      5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in
      state_attr('sensor.manorsfield_road_stand_2_buses', 'calls') %} <tr>
      <td>{{ call.service_description }}</td> <td style="max-width: 10px;
      white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{
      call.destination }}</td> <td  style="width: 20%; white-space: nowrap"> {%-
      if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16]
      }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if
      call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%-
      endfor %} </table> {% if
      state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') is not
      none %} <br /> {% for message in
      state_attr('sensor.manorsfield_road_stand_2_buses', 'messages') %}
      <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%-
      endfor %} {% endif %} {% endif %}
  - type: custom:html-template-card
    title: Coach House Mews
    ignore_line_breaks: false
    content: >-
      {%- if (state_attr('sensor.bicester_village_station_buses', 'calls') |
      count) == 0 %} Information is not available {%- else %} <table
      style="text-align: left"> <tr> <th style="width:
      5%">Bus</th><th>Destination</th><th>Expected</th> </tr> {%- for call in
      state_attr('sensor.bicester_village_station_buses', 'calls') %} <tr>
      <td>{{ call.service_description }}</td> <td style="max-width: 10px;
      white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{
      call.destination }}</td> <td  style="width: 20%; white-space: nowrap"> {%-
      if call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16]
      }} {{ '(' }} {%- endif -%} {{ call.display_time }} {%- if
      call.display_time[2] != ":" %} {{- ')' }} {%- endif %} </td> </tr> {%-
      endfor %} </table> {% if
      state_attr('sensor.bicester_village_station_buses', 'messages') is not
      none %} <br /> {% for message in
      state_attr('sensor.bicester_village_station_buses', 'messages') %}
      <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%-
      endfor %} {% endif %} {% endif %}
type: custom:html-template-card
title: Bicester Village Station
ignore_line_breaks: false
content: >-
  <table style="text-align:left"> <tr><th style="width:65%">Destination</th><th
  style="width:20%">Expected</th><th style="width:20%">Platform</th></tr> {% for
  train in state_attr('sensor.train_schedule_bit_oxf','trains') %} <tr><td
  style="width:60%">{{ train.terminus }}</td> <td style="width:20%">{{
  (train.expected | string)[11:16] }}</td> <td style="width:20%">{% if
  train.platform is not none %} {{ train.platform }} {% endif %} </td></tr> {%-
  endfor %} </table><br /> <table style="text-align:left"> <tr><th
  style="width:65%">Destination</th><th style="width:20%">Expected</th><th
  style="width:20%">Platform</th></tr> {% for train in
  state_attr('sensor.train_schedule_bit_myb','trains') %} <tr><td
  style="width:60%">{{ train.terminus }}</td> <td style="width:20%">{{
  (train.expected | string)[11:16] }}</td> <td style="width:20%">{% if
  train.platform is not none %} {{ train.platform }} {% endif %} </td></tr> {%-
  endfor %} </table>
type: custom:html-template-card
title: Bicester Village Station
ignore_line_breaks: false
content: >-
  {%- if (state_attr('sensor.bicester_village_station_concourse_buses', 'calls')
  | count) == 0 %} Information is not available {%- else %} <table
  style="text-align: left"> <tr> <th style="width: 5%">Bus</th><th
  style="overflow: hidden; text-overflow:
  ellipsis">Destination</th><th>Expected</th> </tr> {%- for call in
  state_attr('sensor.bicester_village_station_concourse_buses', 'calls') %} <tr>
  <td>{{ call.service_description }}</td> <td style="max-width: 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{
  call.destination }}</td> <td  style="width: 20%; white-space: nowrap"> {%- if
  call.display_time[2] != ":" %} {{ call.expected_departure_time[11:16] }} {{
  '(' }} {%- endif -%} {{ call.display_time }} {%- if call.display_time[2] !=
  ":" %} {{- ')' }} {%- endif %} </td> </tr> {%- endfor %} </table> {% if
  state_attr('sensor.bicester_village_station_concourse_buses', 'messages') is
  not none %} <br /> {% for message in
  state_attr('sensor.bicester_village_station_concourse_buses', 'messages') %}
  <marquee behavior="scroll" direction="left">{{ message }}</marquee> {%- endfor
  %} {% endif %} {% endif %}

I have a Markdown Card to display calendar events, and using is iterable doesn’t throw errors:


{% if state_attr('sensor.anstehende_termine', 'termine') is iterable %}
  {% set blabla %}
  {% for termin in termine if termin.start is defined and termine is iterable %}
    {{ blabla }}
  {% endfor %}
{% else %}Calendar not available
{% endif %}

Despite making no changes, the error seems to have gone from my log files after my regular 2am restart. Time will tell if the problem returns.

I have suspected for a while that it takes multiple restarts or some period of time before dashboard changes are flushed through the system. As an example, watchman will report missing entities in lovelace for a while after corrections are made. After restarts or some time period the issues go away.

Watchman updates after reloading core config, I have a button to do this:


type: tile
entity: sensor.watchman_last_updated
name: Last Check
tap_action:
  action: call-service
  service: homeassistant.reload_core_config

1 Like