Some warnings/error messages - solvable?

Hi… i have a few warnings and errors. Any ideas, how to solve them?

Logger: homeassistant.helpers.template
Source: helpers/template.py:1366
First occurred: 9:36:26 (14 occurrences)
Last logged: 10:48:20

Template variable warning: 'dict object' has no attribute 'click' when rendering '{{ value_json.click }}'

2nd: My Sensors:

  - platform: template
    sensors:
      tempmaxheute:
        friendly_name: 'Temp Max Heute'
        value_template: "{{ state_attr('weather.weatherbit_rehfelde', 'forecast')[0].temperature }}"
        unit_of_measurement: '°C'

Error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:72
Integration: Template (documentation, issues)
First occurred: 10:59:56 (2 occurrences)
Last logged: 10:59:56

TemplateError('UndefinedError: None has no element 0') while processing template 'Template("{{ state_attr('weather.weatherbit_rehfelde', 'forecast')[0].temperature }}")' for attribute '_state' in entity 'sensor.tempmaxheute'
TemplateError('UndefinedError: None has no element 1') while processing template 'Template("{{ state_attr('weather.weatherbit_rehfelde', 'forecast')[1].precipitation }}")' for attribute '_state' in entity 'sensor.regenmengemorgen'

and

Logger: homeassistant.helpers.event
Source: helpers/template.py:391
First occurred: 10:59:56 (2 occurrences)
Last logged: 10:59:56

Error while processing template: Template("{{ state_attr('weather.weatherbit_rehfelde', 'forecast')[0].temperature }}")
Error while processing template: Template("{{ state_attr('weather.weatherbit_rehfelde', 'forecast')[1].precipitation }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 389, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1358, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 326, in getattr
    value = getattr(obj, attribute)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1382, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1374, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: None has no element 0

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 505, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 391, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: None has no element 0

But its working…

Any ideas? Thanks

The first message is telling you that the JSON data that was received doesn’t contain anything named click.

no attribute ‘click’ when rendering ‘{{ value_json.click }}’

You’ll have to investigate the automation (or sensor) that uses that template. Whatever is supposed to supply a JSON payload either is failing to do that or the payload lacks a click.

The second message is telling you that you’re trying to get an element from a list that doesn’t exist. This is happening for two sensors (tempmaxheute and regenmengemorgen). What they have in common is that their template uses the state_attr() function to get the forecast attribute from weather.weatherbit_rehfelde. The message is telling you that the result of the state_attr() function is None (i.e. the function retrieved nothing).

The cause may be due to weather.weatherbit_rehfelde not being ready at startup when the two Template Sensors attempt to use it.

1 Like

I get the same none has no element 0 on my value template that pulls weather info.
What needs to be done to eliminate the error?

value_template: "{{ state_attr('weather.17_seaview_avenue', 'forecast') [0].temperature}}"

Test the result of the state_attr() is a list before attempting to get the zeroth index of the list.

You can do that directly in value_template or within the availability option (that’s what I would suggest using).

I guess I need to add a lot of if statements to my templates?

Try this; it checks if the value of the forecast attribute is iterable and contains at least one item.

availability: >
  {% set f = state_attr('weather.17_seaview_avenue', 'forecast') %}
  {{ f is iterable and f|count > 0 }}

If it fails the test, the sensor’s value_template won’t be evaluated and the sensor’s state value will be set to unavailable.

If you want it to be set to some other value (instead of unavailable) omit the availability option altogether and enhance the value_template.

value_template: >
  {% set f = state_attr('weather.17_seaview_avenue', 'forecast') %}
  {{ f[0].temperature if f is iterable and f|count > 0 else 'unknown' }}

To make it to be more robust, add a test to confirm the temperature key exists before attempting to access its value.

{{ f[0].temperature if f is iterable and f|count > 0 and f[0].temperature is defined else 'unknown' }}
1 Like

Currently I got it working with and if statement. Still learning templates and will try to evaluate which is more efficient coding.
Thanks.