Prevent template error: must be None, not type 'Undefined'

since 116, I have these errors on some of my templates, based on a device_trackers tzinfo argument, that apparently doesnt get initialized.

homeassistant.exceptions.TemplateError: TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'Undefined'

and

TemplateError('TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'Undefined'') while processing template 'Template("{% set state = states.device_tracker.she %} {% if state.attributes.time_zone is not none %} {% set n = now() %} {{(n.astimezone(state.attributes.last_seen.tzinfo).utcoffset() - n.utcoffset()).total_seconds()/3600}} {% else %} undetermined {% endif %} ")' for attribute '_state' in entity 'sensor.person_tz_offset'

the template is:

          {% set state = states.device_tracker.she %}
          {% if state.attributes.time_zone and
                state.attributes.time_zone is defined %}
            {% set n = now() %}
            {{(n.astimezone(state.attributes.last_seen.tzinfo).utcoffset() -
               n.utcoffset()).total_seconds()/3600}}
          {% else %}
            undetermined
          {% endif %}

and Ive already tried

  {% if state.attributes.time_zone and
          state.attributes.time_zone is not none and
state.attributes.time_zone != 'unknown' and 
                state.attributes.time_zone is defined %}

odd thing is it only happens on 2 of the 6 identical templates, the 4 that are working as before have only this:

         {% set state = states.device_tracker.person_cc %}
          {% if state.attributes.time_zone and
                state.attributes.time_zone != 'unknown' %}
            {% set n = now() %}
            {{(n.astimezone(state.attributes.last_seen.tzinfo).utcoffset() -
               n.utcoffset()).total_seconds()/3600}}
          {% else %}
            undetermined
          {% endif %}

and have been like this for 2 years or so…

also, Ive tried the original guard in Phil’s site:

          {% if state.attributes is defined and
                state.attributes.time_zone is defined and
                state.attributes.time_zone != 'unknown' %}

but that also errors.

the error that’s specifically intriguing is the

for attribute '_state' in entity 'sensor.person_tz_offset'

where am I setting an attribute ‘_state’ ?

to add to the confusion, the time_zone is there:


please have a look how I can prevent this?