Template Sensor ignoring 'None' or unavailable states

This is my attempt to cope with the source data of a template (fed via a rest call) coping with the odd occasion when the rest call fails. The existing template, without the if statement is sometimes throwing this error in the logs

TemplateError('UndefinedError: 'None' has no attribute 'attributes'')
- sensor:
    - name: "Bin Collection Address"
      unique_id: bin_collection_address
      state: >
        {%if states('sensor.bin_collection_restful')!=None%} 
        {%set collection_address = state_attr('sensor.bin_collection_restful','label')%}
        {%endif%}
        {{collection_address}}

Will this work ? or is collection_address local only to the if statement ?
In which case, would this be better ?

- sensor:
    - name: "Bin Collection Address"
      unique_id: bin_collection_address
      state: >
        {%if states('sensor.bin_collection_restful')!=None%} 
        {{state_attr('sensor.bin_collection_restful','label')}}
        {%else%}
        {{states('sensor.bin_collection_address')}}
        {%endif%}

I have a similar template, and using the above, I’ve changed it to this:

- sensor:
    - name: "Black Waste Bin Collection Date"
      unique_id: black_waste_bin_collection_date
      state: >
        {{ state_attr('sensor.bin_collection_restful','Results').split('Your rubbish')[1].split('<br/>')[1].split('</em>')[0] | default(this.state, true) }}

But if I restart the rest entities from the developer tools yaml list, I get this:

TemplateError('UndefinedError: 'None' has no attribute 'split'') while processing template 'Template<template=({{ state_attr('sensor.bin_collection_restful','Results').split('Your rubbish')[1].split('<br/>')[1].split('</em>')[0] | default(this.state, true) }}) renders=8>' for attribute '_attr_native_value' in entity 'sensor.black_waste_bin_collection_date'

Which the same error when the rest call fails.

Hey @123 - Thanks for your feedback.

In retrospect I understand how these are different, and I get that I can’t reference .split if the object is not a string.

Both the split and original sensors come from a single rest call that has two attributes, label and results. In label, the data is clean and I can simply display it. In Results, the data I need is buried in a paragraph of text, and hence I use split to grab the bit I need.

Without explaining it, I think thats why I started my attempt by using

{%if states('sensor.bin_collection_restful')!=None%} 

Which (I thought) would cover whether the object exists or not, and work for both the split and original issue.

I’ve come up with a semi generic way of doing it, not sure this doesn’t have side effects, but my approach has been simply to check if the object is a string or not:

- sensor:
    - name: "Recycling Collection Date"
      unique_id: recycling_collection_date
      state: >
        {% if state_attr('sensor.bin_collection_restful','Results') is string %}
        {{state_attr('sensor.bin_collection_restful','Results').split('<br/>')[1].split('</em>')[0]}}
        {%else%} 
        {{this.state}}
        {%endif%}