Rest sensor unavailable; logfile full of errors

Hi, probably the solution is simple, but cannot figure this out, how to prevent many error messages.

I read a JSON from a REST sensor, and merge them in multiple sensors and binary_sensors.

Sometimes the REST fails, and the sensor will give “unavailable” for all other sensors and binary_sensors.
When this happens, the log is flooded with errors;

ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ state_attr('sensor.laadpaal_straat_sensors', 'evses')[0]['status'] == 'Occupied' }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 406, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1771, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/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.9/site-packages/jinja2/sandbox.py", line 303, in getitem
    return obj[argument]
jinja2.exceptions.UndefinedError: None has no element 0

Binary_sensor:

  - platform: template
    sensors:
      laadpaal_po_l:
        device_class: occupancy
        friendly_name: "EV charger links"
        value_template: >-
          {{ state_attr('sensor.laadpaal_straat_sensors', 'evses')[0]['status'] == 'Occupied' }}
        icon_template: >
          {% if state_attr('sensor.laadpaal_straat_sensors', 'evses')[0]['status'] == 'Occupied' %} mdi:car-electric
          {% elif state_attr('sensor.laadpaal_straat_sensors', 'evses')[0]['status'] == 'Available' %} mdi:car-electric-outline
          {% else %} mdi:alert-circle-outline
          {% endif %}

Any help or suggestion is welcome!

I would surround each template with an if statement that checks if the source sensor is available or not. Something like this (the else value obviously depends on your particular circumstances):

        value_template: >-
          {% if not is_state('sensor.laadpaal_straat_sensors', 'unavailable') %}{{ state_attr('sensor.laadpaal_straat_sensors', 'evses')[0]['status'] == 'Occupied' }}{% else %}False{% endif %}

Likewise for the icon_template.

And, you could add an availability template like this to improve the overall experience:

        availability_template: {{ not is_state('sensor.laadpaal_straat_sensors', 'unavailable') }}