Template error: int got invalid input 'unknown' when rendering template

Hi, i have created a template sensor, that does a SUM of 2 other sensors, but upon restart when the integrations are being loaded, it takes a while before this sensor gets data : sensor.kostal_piko_current_power

Si already checked if there is a value, but still receive error below …how can i get rid of that error in my log? its only once at reboot/restart

  - name: Real Consumption
    unique_id: real_consumption
    state_class: measurement
    device_class: energy  
    unit_of_measurement: 'W'
    state: >-
      {% set val = states('sensor.kostal_piko_current_power') %}
      {% if val is defined %}
        {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
      {% else %}
        0
      {% endif %}

error:

2023-01-11 14:39:34.004 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% set val = states('sensor.kostal_piko_current_power') %} {% if val is defined %}
  {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
{% else %}
  0
{% endif %}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1950, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1729, in forgiving_int_filter
    raise_no_default("int", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1411, in raise_no_default
    raise ValueError(
ValueError: Template error: int got invalid input 'unknown' when rendering template '{% set val = states('sensor.kostal_piko_current_power') %} {% if val is defined %}
  {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
{% else %}
  0
{% 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 540, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 425, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: int got invalid input 'unknown' when rendering template '{% set val = states('sensor.kostal_piko_current_power') %} {% if val is defined %}
  {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
{% else %}
  0
{% endif %}' but no default was specified
2023-01-11 14:39:34.007 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: int got invalid input 'unknown' when rendering template '{% set val = states('sensor.kostal_piko_current_power') %} {% if val is defined %}
  {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
{% else %}
  0
{% endif %}' but no default was specified') while processing template 'Template("{% set val = states('sensor.kostal_piko_current_power') %} {% if val is defined %}
  {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
{% else %}
  0
{% endif %}")' for attribute '_attr_native_value' in entity 'sensor.real_consumption'

You have to supply a default to int

as a sidebar, val will always be defined because you define it in the previous line. Using defined checks to see if the variable exists in the templates namespace. It does not check the variables value.

You can also just add an availability template too to avoid adding defaults

    state: >
      {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | int + states("sensor.kostal_piko_current_power") | int }}
    availability: >
      {{ states("sensor.smappee_1006001738_local_total_consumption_active_power") | is_number and states("sensor.kostal_piko_current_power") | is_number }}

ok, the second one is indeed a good approach !!

I have a similar one, on startup i create water / gas sensor like below, also on start, the json is not filled yet, and receive : jinja2.exceptions.UndefinedError: None has no element 0 in log

  - name: Smappee Water Consumption
    unit_of_measurement: "L"
    state_class: total_increasing
    device_class: water
    state: >-
      {{ state_attr('sensor.smappee_water_gas', 'records')[0]["value1"] }}

Can i use here the method is used before to check if its defined or != None ?

or something like:

    state: >-
      '{% if state_attr('sensor.smappee_water_gas', 'records') %}{{ state_attr('sensor.smappee_water_gas', 'records')[0]["value1"] }}{% endif %}'

So the availability template overrides errors in the template now?

I know there was talk of this but wasn’t sure it was implemented yet.

Yah, been that way for at least 6 months

1 Like