Value template can no longer reference attributes

Hi.

I recently updated the HA instance running catching up on a few releases. In the process, a previously working sensor (several actually but same root cause so I’m just worrying about the one for now) broke and I’m struggling to see a tidy way to fix it.

As best as I can tell, the HA behaviour for evaluating states and attributes has changed and any state that was referencing an attribute in the sensor as part of its evaluation now fails completely. (If I run the config for an attribute through the template tool on its own it works.)

So, background:

  • The sensor in this instance is keeping an eye on backups. The machine doing the backups will periodically send a JSON structure dump to an MQTT sensor (backups_json) which is broken down with MQTT attributes configuration:
    json_attributes_template: "{{ value_json | tojson }}"
  • The sensor for the individual backup (my_backup for example) has a number of attributes which are mapped from the MQTT JSON sensor. The overall state for my_backup is derived from its attributes. Full sensor config below.

I’ve tried swapping out for the this referencing but that failed YAML config checks entirely. I don’t really want to have to break this out in to another sensors as an interim step but it’s the only way I can see.

Is there a better way I should be doing this?

        my_backup:
          friendly_name: "My Backup"
          value_template: >-
            {% set maxDaysOld = 2 %}
            {% set cutoff = ((as_timestamp(states("sensor.date_time_iso")) | float) - ((maxDaysOld*24*3600) | float)) %}
            {% set backupTime = (as_timestamp(state_attr("sensor.my_backup", "lastbackup_local")) | float) %}
            {% if ( backupTime > cutoff ) and state_attr("sensor.my_backup", "lastbackup_status") == "Successful" %}
              Healthy
              {% else %}
              At Risk
            {% endif %}
          attribute_templates:
            lastbackup_utc: >-
              {% set ns = namespace(found=false) %}
              {% set myPlan = "a1caa3c9-834a-490c-b696-c0a15a8c3aca" %}
              {% for i in state_attr('sensor.backups_json','Backups') %}
                {%- if i.plan_id == myPlan %}
                  {% set ns.found = true %}
                  {% set dsus = (i.date_start_utc | string) %}
                  {{ dsus[:4] }}-{{ dsus[4:6] }}-{{dsus[6:8]}}T{{dsus[8:10]}}:{{dsus[10:12]}}:{{dsus[12:14]}}Z
                  {% else %}
                {% endif -%}
              {% endfor %}
              {% if not ns.found %}"Unknown"{% endif %}
            lastbackup_local: >-
              {{ ( as_timestamp(state_attr('sensor.my_backup', 'lastbackup_utc')) | timestamp_local ) }}
            lastbackup_status: >-
              {% set ns = namespace(found=false) %}
              {% set myPlan = "a1caa3c9-834a-490c-b696-c0a15a8c3aca" %}
              {% for i in state_attr('sensor.backups_json','Backups') %}
                {%- if i.plan_id == myPlan %}
                  {% set ns.found = true %}
                  {%- if i.result == 2 -%}In Progress{%- elif i.result == 6 -%}Successful{%- else -%}Failed{%- endif -%}
                {% endif -%}
              {% endfor %}
              {% if not ns.found %}"Unknown"{% endif %}

Thought I’d come back and mark this one solved.

Not sure what changed over the last few months in terms of sensor/attribute evaluation but it appears that the default behaviour changes done last year influenced it.

Amending the YAML to include default values for as_timestamp was enough to fix this.

It might be time to trawl through my log file and tidy bring things up to date based on warnings…