Templating with unavailable entities (Visual Editor)

First of all, I am sure others have had this problem, and I have searched but I couldn’t find a solution yet.

I make all my automations in the visual editor (for now, since I haven’t found a reason not to). The automation below is one where I get errors when one of the devices is unavailable (which sometimes happens).

alias: Study Ask Heat
description: ""
trigger:
  - platform: template
    value_template: >-
      {% set therm = 'climate.big_study_radiator' %}

      {{ state_attr(therm, 'current_temperature') < state_attr(therm,
      'temperature') }}
  - platform: template
    value_template: >-
      {% set therm = 'climate.small_study_radiator' %}

      {{ state_attr(therm, 'current_temperature') < state_attr(therm,
      'temperature') }}
condition:
  - condition: template
    value_template: >-
      {% set therm = 'climate.incomfort_1' %}

      {{ state_attr(therm, 'current_temperature') > state_attr(therm,
      '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

If I understand correctly, the errors below can be avoided with a default value or something that HA knows what to do when the entity isn’t available. But I can’t figure out how and where to set that data within the visual editor. Is it with every “template” piece of text?

Thanks for any tips!

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

Error while processing template: Template("{% set therm = 'climate.big_study_radiator' %} {{ state_attr(therm, 'current_temperature') < state_attr(therm, 'temperature') }}")
Error while processing template: Template("{% set therm = 'climate.small_study_radiator' %} {{ state_attr(therm, 'current_temperature') < state_attr(therm, 'temperature') }}")
Error while processing template: Template("{% set therm = 'climate.big_study_radiator' %} {{ state_attr(therm, 'current_temperature') >= state_attr(therm, 'temperature') }}")
Error while processing template: Template("{% set therm = 'climate.small_study_radiator' %} {{ state_attr(therm, 'current_temperature') >= state_attr(therm, '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'

One way to approach it is to use an if guard clause. The temperature attribute of a climate entity is more likely to be None than current_temperature because it is None when the device is ‘off’ as well as during restart, so that’s where I would start…

{% set therm = 'climate.big_study_radiator' %}
{% if state_attr(therm,'temperature') is not none %}
{{ state_attr(therm, 'current_temperature') < state_attr(therm,'temperature') }}
{% endif %}

You could add a condition on NoneType. As I haven’t used that before in HA / Jinja I assume python behaviour.
Then this could be:

  - condition: template
    value_template: {{ state_attr(therm, 'current_temperature') }}
  - condition: template
    value_template: {{ state_attr(therm,'temperature') }
  - condition: template
    value_template: >-
      {% set therm = 'climate.incomfort_1' %}

      {{ state_attr(therm, 'current_temperature') > state_attr(therm,
      'temperature') }}

If None does not default to false an is not none need to be appended.

I’ve implemented this, let’s see if I still get errors.

I am trying the above solution first. I got a bit confused with your answer, definitely due to my lack of Jinja knowledge…