Wireguard user sensors are not working right, template issue

I have a set of sensors set up to monitor user connection status, up/down. Since the mid 0.100’s, around 0.108 the stats becomes unavailable for a short amount of time. Connections are never affected, but it fills up my logs with error messages. I am sure something changed with templates that I missed, but I cannot find the issue.

Thanks Matt

Currently running 0.118.4.

Normal state:
image

Errored state:
image

Log messages

2020-11-30 17:39:03 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'latest_handshake'') while processing template 'Template("{% if state_attr('sensor.wireguard', 'work').latest_handshake > ( as_timestamp(now()) ) -180 %}
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_work'
2020-11-30 17:39:03 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'latest_handshake'') while processing template 'Template("{% if state_attr('sensor.wireguard', 'matt').latest_handshake > ( as_timestamp(now()) ) -180 %}
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_matt'
2020-11-30 17:39:03 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'latest_handshake'') while processing template 'Template("{% if state_attr('sensor.wireguard', 'chris').latest_handshake > ( as_timestamp(now()) ) -180 %}
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_chris'
2020-11-30 17:39:03 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'latest_handshake'') while processing template 'Template("{% if state_attr('sensor.wireguard', 'alex').latest_handshake > ( as_timestamp(now()) ) -180 %}
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_alex'
2020-11-30 17:39:03 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'latest_handshake'') while processing template 'Template("{% if state_attr('sensor.wireguard', 'sam').latest_handshake > ( as_timestamp(now()) ) -180 %}
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_sam'
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_sam'
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_alex'
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_matt'
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_chris'
    Up
  {% else %}
    Down
  {% endif %}")' for attribute '_state' in entity 'sensor.wireguard_work'

Sensor configs:

- platform: template
  sensors:
    wireguard_work:
      friendly_name: "Work"
      value_template: >-
          {% if state_attr('sensor.wireguard', 'work').latest_handshake > ( as_timestamp(now()) ) -180 %}
              Up
            {% else %}
              Down
            {% endif %}
    wireguard_matt:
      friendly_name: "Matt"
      value_template: >-
          {% if state_attr('sensor.wireguard', 'matt').latest_handshake > ( as_timestamp(now()) ) -180 %}
              Up
            {% else %}
              Down
            {% endif %}
    wireguard_chris:
      friendly_name: "Chris"
      value_template: >-
          {% if state_attr('sensor.wireguard', 'chris').latest_handshake > ( as_timestamp(now()) ) -180 %}
              Up
            {% else %}
              Down
            {% endif %}
    wireguard_alex:
      friendly_name: "Alex"
      value_template: >-
          {% if state_attr('sensor.wireguard', 'alex').latest_handshake > ( as_timestamp(now()) ) -180 %}
              Up
            {% else %}
              Down
            {% endif %}
    wireguard_sam:
      friendly_name: "Sam"
      value_template: >-
          {% if state_attr('sensor.wireguard', 'sam').latest_handshake > ( as_timestamp(now()) ) -180 %}
              Up
            {% else %}
              Down
            {% endif %}

Open up dev tools, go to states and find sensor.wireguard

Do those attributes still exist? It’s saying the attribute sensor.wireguard, ‘work’ doesn’t exist…so obviously it can’t do .latest_handshake on a None object.

Oh, you say this is just an intermittent thing. Ok, do this (for each sensor)

wireguard_work:
  friendly_name: "Work"
  value_template: >-
      {% set a = state_attr('sensor.wireguard', 'work') %}
      {{ 'Up' if a and a.latest_handshake > ( as_timestamp(now()) ) -180 else 'Down' }}

Now when it’s unavilalbe, this should report ‘Down’ instead of throwing an error. We are basically making sure the state_attr is not none before trying to access more info from it.