Templete causes HA to boot in safe mode

I have used a templete that automatically creates a group of sensors and when I check config there is no issue but if I restart HA, it enters safe mode.
I then remove the config reboot and once booted I add it back and reload templetes and everything works fine till I have to restart for some reason.

Any idea why this maybe and how I can get around it?

Here is the code, I have multipe instances of this in my file as I use it for lights, switches, motion, windows, etc.

  - sensor:
      - name: Office Switches On
        <<: &area_info
          state: "{{ states['switch'] | selectattr('state','in','on') | selectattr('entity_id','in',area_entities(this.name[:-12])) |rejectattr('entity_id', 'is_hidden_entity') |rejectattr('entity_id', 'search', 'all_switches') |map(attribute='entity_id')|list|count }}"
      - name: Dining Switches On
        <<: *area_info
      - name: Dressing Room Switches On
        <<: *area_info
      - name: Entrance Switches On
        <<: *area_info
      - name: Bathroom Switches On
        <<: *area_info
      - name: Garage Switches On
        <<: *area_info
      - name: Hallway Switches On
        <<: *area_info
      - name: Kitchen Switches On
        <<: *area_info
      - name: Landing Switches On
        <<: *area_info
      - name: Loft Switches On
        <<: *area_info
      - name: Lounge Switches On
        <<: *area_info
      - name: Main Bedroom Switches On
        <<: *area_info
      - name: Outside Switches On
        <<: *area_info
      - name: Toilet Switches On
        <<: *area_info