Template errors i don't understand

Everytime i start HA i get the following errors:

> 2024-11-06 11:42:55.531 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set bl = states('sensor.oppo_battery_level') | int %}
>   {% if bl < 10 %} mdi:battery-outline
>   {% elif bl < 20 %} mdi:battery-10
>   {% elif bl < 30 %} mdi:battery-20
>   {% elif bl < 40 %} mdi:battery-30
>   {% elif bl < 50 %} mdi:battery-40
>   {% elif bl < 60 %} mdi:battery-50
>   {% elif bl < 70 %} mdi:battery-60
>   {% elif bl < 80 %} mdi:battery-70
>   {% elif bl < 90 %} mdi:battery-80
>   {% elif bl < 100 %} mdi:battery-90
>   {% elif bl == 100 %} mdi:battery
>   {% else %} mdi:battery-unknown
>   {% endif %}) renders=4>
> Traceback (most recent call last):
>   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 632, in async_render
>     render_result = _render_with_context(self.template, compiled, **kwargs)
>                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2729, 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/src/homeassistant/homeassistant/helpers/template.py", line 2367, in forgiving_int_filter
>     raise_no_default("int", value)
>   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1905, in raise_no_default
>     raise ValueError(
> ValueError: Template error: int got invalid input 'unknown' when rendering template '{% set bl = states('sensor.oppo_battery_level') | int %}
>   {% if bl < 10 %} mdi:battery-outline
>   {% elif bl < 20 %} mdi:battery-10
>   {% elif bl < 30 %} mdi:battery-20
>   {% elif bl < 40 %} mdi:battery-30
>   {% elif bl < 50 %} mdi:battery-40
>   {% elif bl < 60 %} mdi:battery-50
>   {% elif bl < 70 %} mdi:battery-60
>   {% elif bl < 80 %} mdi:battery-70
>   {% elif bl < 90 %} mdi:battery-80
>   {% elif bl < 100 %} mdi:battery-90
>   {% elif bl == 100 %} mdi:battery
>   {% else %} mdi:battery-unknown
>   {% endif %}' 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 749, in async_render_to_info
>     render_info._result = self.async_render(  # noqa: SLF001
>                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 634, in async_render
>     raise TemplateError(err) from err
> homeassistant.exceptions.TemplateError: ValueError: Template error: int got invalid input 'unknown' when rendering template '{% set bl = states('sensor.oppo_battery_level') | int %}
>   {% if bl < 10 %} mdi:battery-outline
>   {% elif bl < 20 %} mdi:battery-10
>   {% elif bl < 30 %} mdi:battery-20
>   {% elif bl < 40 %} mdi:battery-30
>   {% elif bl < 50 %} mdi:battery-40
>   {% elif bl < 60 %} mdi:battery-50
>   {% elif bl < 70 %} mdi:battery-60
>   {% elif bl < 80 %} mdi:battery-70
>   {% elif bl < 90 %} mdi:battery-80
>   {% elif bl < 100 %} mdi:battery-90
>   {% elif bl == 100 %} mdi:battery
>   {% else %} mdi:battery-unknown
>   {% endif %}' but no default was specified

The template i am using is this:

>                       type: custom:mushroom-template-card
>                       entity: sensor.oppo_battery_level
>                       layout: vertical
>                       icon: |2
>                           {% set bl = states('sensor.oppo_battery_level') | int %}
>                           {% if bl < 10 %} mdi:battery-outline
>                           {% elif bl < 20 %} mdi:battery-10
>                           {% elif bl < 30 %} mdi:battery-20
>                           {% elif bl < 40 %} mdi:battery-30
>                           {% elif bl < 50 %} mdi:battery-40
>                           {% elif bl < 60 %} mdi:battery-50
>                           {% elif bl < 70 %} mdi:battery-60
>                           {% elif bl < 80 %} mdi:battery-70
>                           {% elif bl < 90 %} mdi:battery-80
>                           {% elif bl < 100 %} mdi:battery-90
>                           {% elif bl == 100 %} mdi:battery
>                           {% else %} mdi:battery-unknown
>                           {% endif %}
>                       icon_color: |2-
>                                   {% set bl = states('sensor.oppo_battery_level') | int %}
>                                   {% if bl < 10 %} #cc0c16
>                                   {% elif bl < 20 %} #e61e28
>                                   {% elif bl < 30 %} #e3464e
>                                   {% elif bl < 40 %} orange
>                                   {% elif bl < 50 %} #f0b93a
>                                   {% elif bl < 60 %} #f3f56c
>                                   {% elif bl < 70 %} #f2f536
>                                   {% elif bl < 80 %} #69f095
>                                   {% elif bl < 90 %} #2ee669
>                                   {% elif bl < 100 %} #05ad3b
>                                   {% elif bl == 100 %} #03872d
>                                   {% else %} grey
>                                   {% endif %}

Where i am wrong?

Start with defining a default value for a conversion to a number:

states() | int(default=…)

Otherwise if a state is unknown - you will get an error.

The sensor does not have a value int can convert into a number. You need to supply a default in this case. If you supply a number > 100 it will use your unknown icon. I chose 105

{% set bl = states('sensor.oppo_battery_level') | int(105) %}

Thanks to both @Ildar_Gabdullin @tom_l , but non being so expert with templates, can you post here the correct template?

I just did. Change your template to use this:

All you need to add is the (105) part to your existing template.

I take advantage of your expertise to try to resolve this error in another template.
The error is:

2024-11-06 12:00:46.011 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% if (state_attr('sensor.fha_place', 'direction_of_travel') == 'stationary') and (state_attr('sensor.fha_place','home_zone') == 'zone.home') %} Fha è a Casa {% elif (state_attr('sensor.fha_place', 'direction_of_travel') == 'stationary') and (state_attr('sensor.fha_place','home_zone') !== 'zone.home')%} Fha is Here:               {% else %} Fha sta {% endif %}) renders=50>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 576, in ensure_valid
    self._compiled_code = self._env.compile(self.template)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 3206, in compile
    compiled = 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 939, 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 749, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 626, in async_render
    compiled = self._compiled or self._ensure_compiled(limited, strict, log_fn)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 830, in _ensure_compiled
    self.ensure_valid()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 578, in ensure_valid
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: unexpected '='

And the template is:

                  {% if (state_attr('sensor.fha_place', 'direction_of_travel')
                  == 'stationary') and
                  (state_attr('sensor.fha_place','home_zone') == 'zone.home') %}
                  Fha è a Casa {% elif (state_attr('sensor.fha_place',
                  'direction_of_travel') == 'stationary') and
                  (state_attr('sensor.fha_place','home_zone') !== 'zone.home')%}
                  Fha is Here:               {% else %} Fha sta {% endif %}

Thanks for the support… can you give me a hint also for the other template error i posted here?

At least this one is wrong, should be “!=“.

1 Like

Yes, i tried and now it works! Thanks again…