Error with heating automation template

I have an automation that should activate a change in thermostat setting if one of my TRVs needs heat, but I’m getting some errors in the log I cannot figure out.

This is my automation:

alias: Study Ask Heat
description: ""
trigger:
  - platform: template
    value_template: |-
      {% set therm = states['climate.big_study_radiator'] %}
      {{ therm.attributes.current_temperature < therm.attributes.temperature }}
  - platform: template
    value_template: |-
      {% set therm = states['climate.small_study_radiator'] %}
      {{ therm.attributes.current_temperature < therm.attributes.temperature }}
condition:
  - condition: template
    value_template: |-
      {% set therm = states['climate.incomfort_1'] %}
      {{ therm.attributes.current_temperature > therm.attributes.temperature }}
action:
  - device_id: 90ed87e3fcc7dbd52e4ad12748189c45
    domain: mobile_app
    type: notify
    message: Living Room 19
  - service: climate.set_temperature
    data:
      temperature: 19
    target:
      entity_id: climate.incomfort_1
mode: single

And here’s the error code:

Logger: homeassistant.helpers.event
Source: helpers/template.py:425
First occurred: 11:35:11 (4 occurrences)
Last logged: 11:35:11

Error while processing template: Template("{% set therm = states['climate.big_study_radiator'] %} {{ therm.attributes.current_temperature < therm.attributes.temperature }}")
Error while processing template: Template("{% set therm = states['climate.small_study_radiator'] %} {{ therm.attributes.current_temperature < therm.attributes.temperature }}")
Error while processing template: Template("{% set therm = states['climate.big_study_radiator'] %} {{ therm.attributes.current_temperature >= therm.attributes.temperature }}")
Error while processing template: Template("{% set therm = states['climate.small_study_radiator'] %} {{ therm.attributes.current_temperature >= therm.attributes.temperature }}")
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
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'

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: TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'

As warned about in the Templating docs, you should always use the states() style functions instead of the state object method where possible to avoid errors caused by trying to render templates that include entities that aren’t fully loaded.

  - platform: template
    value_template: |-
      {% set therm = 'climate.big_study_radiator' %}
      {{ state_attr(therm, 'current_temperature') < state_attr(therm, 'temperature') }}
1 Like

Ah thanks, let me try that.

Seems like it worked, thanks. One small error, but most people should find that, there’s a missing ) in the last line. Just posting it here for future searches by others.

{% set therm = 'climate.big_study_radiator' %}
      {{ state_attr(therm, 'current_temperature') < state_attr(therm, 'temperature') }}