Error with template coding

Hi All.

Can somebody please help me, I’ve quite recently started encountering my home assistant crashing and rebooting and in the error log I have this:

Logger: homeassistant.helpers.event
Source: helpers/template.py:574
First occurred: 9 February 2024 at 19:04:45 (434 occurrences)
Last logged: 20:54:48

Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1) | float(0) }} km) renders=264>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1) | float(0) }} km) renders=268>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1) | float(0) }} km) renders=272>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1) | float(0) }} km) renders=276>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1) | float(0) }} km) renders=280>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1656, in forgiving_round
    value = round(float(value), precision)
                  ^^^^^^^^^^^^
TypeError: float() argument must be a string or a real number, not 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 572, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2332, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/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/src/homeassistant/homeassistant/helpers/template.py", line 1661, in forgiving_round
    raise_no_default("round", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1637, in raise_no_default
    raise ValueError(
ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr(entity, 'distance') | round(1) }} km' 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 698, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 574, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr(entity, 'distance') | round(1) }} km' but no default was specified

Any help appreciated thanks.

Disable automation or template and see will it reboot.
If it stop reboot than post it here, some will be able to help you with templating.

It reboots fine it just randomly happens after a short period of time of being on the app

Maybe you have some hardware issue. I replaced my working server for i7 processor, 1 gen. And it started to crash and rebooting.
Going back to my old server everything was working fine. No more crashed no more reboots. I couldn’t find anything in the logs that make sense.

Hi @stevo92

I doubt that this route is the cause of your crash. However, if you post the Jinja code that causes this we can take a look.

Try replacing entity with the actual device or config.entity. I recently struggled with this errors as well.

{{ state_attr(config.entity, 'distance') | round(1) }}
or
like this with your entity and attribute.

{{ state_attr('media_player.emby_shield', 'media_position') | round(1) }}

Although looking at your logs the errors shouldn’t be linked to your crashed. Let us know.

              primary: >-
                {{ states(entity) }} {{ state_attr(entity,
                'unit_of_measurement') }}
              secondary: '{{ state_attr(entity, ''distance'') | round(1) | float(0) }} Mi'

I

Is one of the pieces of code that crashes, only recently added float(0) to try to fix the issue .

float or int. After some advice I avoid round.

1 Like

Should you have two single quotes each side of distance?

Probably happened with a cut and paste or populated because double quotes weren’t used, but good catch! There is a random I in the left corner too.

Thanks for advice guys, changed it to a double quote, taken the round (0) out and the “I” was actually added by me when pasting here sorry lol

Logger: homeassistant.helpers.event
Source: helpers/template.py:518
First occurred: 10 February 2024 at 05:14:35 (51 occurrences)
Last logged: 11:24:27

Error while processing template: Template<template=({{ state_attr(entity, 'distance') | | float(0) }} km) renders=2>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | float(0) }} Mi) renders=10>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | float(0) }} Mi) renders=14>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | float(0) }} Mi) renders=18>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | float(0) }} Mi) renders=22>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 516, in ensure_valid
    self._compiled_code = self._env.compile(self.template)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2753, in compile
    cached = self.template_cache[source] = super().compile(source)
                                           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.12/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: expected token 'name', got 'end of print statement'

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 698, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 566, in async_render
    compiled = self._compiled or self._ensure_compiled(limited, strict, log_fn)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 777, in _ensure_compiled
    self.ensure_valid()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 518, in ensure_valid
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: expected token 'name', got 'end of print statement'

Is what I get now

Supply round(1) with a default.

{{ state_attr(entity, 'distance') | round(1, 0) }}
1 Like

That is really good to know!!

Logger: homeassistant.helpers.event
Source: helpers/template.py:574
First occurred: 9 February 2024 at 19:04:45 (621 occurrences)
Last logged: 11:35:22

Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1, ) }} km) renders=2>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1, 0) }} km) renders=2>
Error while processing template: Template<template=({{ state_attr(entity, "distance") | round(1, 0) }} Mi) renders=166>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1, 0) }} km) renders=6>
Error while processing template: Template<template=({{ state_attr(entity, 'distance') | round(1, 0) }} km) renders=10>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1656, in forgiving_round
    value = round(float(value), precision)
                  ^^^^^^^^^^^^
TypeError: float() argument must be a string or a real number, not 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 572, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2332, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/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/src/homeassistant/homeassistant/helpers/template.py", line 1661, in forgiving_round
    raise_no_default("round", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1637, in raise_no_default
    raise ValueError(
ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr(entity, 'distance') | round(1) }} km' 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 698, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 574, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr(entity, 'distance') | round(1) }} km' but no default was specified

Please post your full code, I’m still seeing multiple typos

{{ state_attr(entity, 'distance') | round(1, ) }} km) renders=2>
({{ state_attr(entity, "distance") | round(1, 0) }} Mi) renders=166>

You missed one. Look at the template in the error, notice it’s missing the default.

Still pulls an error

Logger: homeassistant.helpers.event
Source: helpers/template.py:518
First occurred: 10 February 2024 at 05:14:35 (109 occurrences)
Last logged: 12:21:34

Error while processing template: Template<template=({{ state_attr(entity, "distance) | round(1, 0) }} Mi) renders=34>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | round(1, 0) }} Mi) renders=38>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | round(1, 0) }} Mi) renders=42>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | round(1, 0) }} Mi) renders=46>
Error while processing template: Template<template=({{ state_attr(entity, "distance) | round(1, 0) }} Mi) renders=50>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 516, in ensure_valid
    self._compiled_code = self._env.compile(self.template)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2753, in compile
    cached = self.template_cache[source] = super().compile(source)
                                           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.12/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: expected token 'name', got 'end of print statement'

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 698, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 566, in async_render
    compiled = self._compiled or self._ensure_compiled(limited, strict, log_fn)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 777, in _ensure_compiled
    self.ensure_valid()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 518, in ensure_valid
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: expected token 'name', got 'end of print statement'

change this to
{{ state_attr(entity, "distance) | round(1, 0) }}
this
{{ state_attr(entity, "distance") | round(1, 0) }}

You missed a set of double quotes

Fixed that thanks, sadly still throwing same errors