Help needed with template error (in markdown card)

With HA 2021.4.x, errors are now being generated for templates that didn’t throw errors before. I am trying to fix this for a markdown card. Originally I didn’t have the if statements

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

that I added to try to prevent the error but they aren’t working and I don’t understand why. This is the card:

    - type: markdown
      title: Updates Available
      card_mod:
        style: |
          :host {
            --card-mod-icon-color: #42a5f5;
            font-size: 1.1em;
          }
      content: |
        
        {% if states('sensor.supervisor_updates') != None %}
          <ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') }}

          > {% for addon in states.sensor.supervisor_updates.attributes.addons %}
          > &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
          > {% endfor %}
        {% endif %}

        {% if states('sensor.hacs') != None %}
          <ha-icon icon="hacs:hacs"></ha-icon>&nbsp;&nbsp;&nbsp;HACS updates available: &nbsp;&nbsp; {{ states('sensor.hacs') }}


          > {% for repo in states.sensor.hacs.attributes.repositories %}
          >    **{{ repo.display_name }}** _{{ repo["installed_version"] }}_ -> _{{ repo["available_version"] }}_
          > {% endfor %}
        {% endif %}
        
        {% if states('sensor.supervisor_updates') != None %}
          
          |          |    Current         | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  Latest  |
          |---                |:---:                                                         |:---: |:---:           |
          | **Supervisor**&nbsp;&nbsp;&nbsp;    | {{ states.sensor.supervisor_updates.attributes.current_version }} | | {{ states.sensor.supervisor_updates.attributes.newest_version }} |
          | **Audio**         | {{ states.sensor.updater_audio.attributes.current_version }} | | {{ states.sensor.updater_audio.attributes.newest_version }} |
          | **CLI**           | {{ states.sensor.updater_cli.attributes.current_version }} | | {{ states.sensor.updater_cli.attributes.newest_version }} |
          | **DNS**           | {{ states.sensor.updater_dns.attributes.current_version }} | |  {{ states.sensor.updater_dns.attributes.newest_version }} |
          | **Multicast**     | {{ states.sensor.updater_multicast.attributes.current_version }} | |  {{ states.sensor.updater_multicast.attributes.newest_version }} |
          | **Observer**      | {{ states.sensor.updater_observer.attributes.current_version }}  | |  {{ states.sensor.updater_observer.attributes.newest_version }}  |
        {% endif %}

This error occurs 3 times on HA startup:

2021-04-11 12:22:50 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% if states('sensor.supervisor_updates') != None %}
<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}
{% endif %}
{% if states('sensor.hacs') != None %}
<ha-icon icon="hacs:hacs"></ha-icon>&nbsp;&nbsp;&nbsp;HACS updates available: &nbsp;&nbsp; {{ states('sensor.hacs') }}
> {% for repo in states.sensor.hacs.attributes.repositories %}
> **{{ repo.display_name }}** _{{ repo["installed_version"] }}_ -> _{{ repo["available_version"] }}_
> {% endfor %}
{% endif %}
{% if states('sensor.supervisor_updates') != None %}
| | Current | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Latest |
|--- |:---: |:---: |:---: |
| **Supervisor**&nbsp;&nbsp;&nbsp; | {{ states.sensor.supervisor_updates.attributes.current_version }} | | {{ states.sensor.supervisor_updates.attributes.newest_version }} |
| **Audio** | {{ states.sensor.updater_audio.attributes.current_version }} | | {{ states.sensor.updater_audio.attributes.newest_version }} |
| **CLI** | {{ states.sensor.updater_cli.attributes.current_version }} | | {{ states.sensor.updater_cli.attributes.newest_version }} |
| **DNS** | {{ states.sensor.updater_dns.attributes.current_version }} | | {{ states.sensor.updater_dns.attributes.newest_version }} |
| **Multicast** | {{ states.sensor.updater_multicast.attributes.current_version }} | | {{ states.sensor.updater_multicast.attributes.newest_version }} |
| **Observer** | {{ states.sensor.updater_observer.attributes.current_version }} | | {{ states.sensor.updater_observer.attributes.newest_version }} |
{% endif %}'

I understand that the error is about attributes, but I thought that if the sensor is None, it would dump out of the if block and it would never be looking at the attributes. Any help would be much appreciated!

Did you read this: 2021.4: For our advanced users ❤️ - Home Assistant ?
Scroll down at bit and click on the link named: “Warnings for undefined variables in Templates”.

Yes, thanks, I had read it, and I understand that I have to guard for the missing sensors on startup - that’s what I am struggling with.

Ok, I’ll explain what the hint means for your code.

You should change {{ states('sensor.supervisor_updates') }} to
{{ states('sensor.supervisor_updates') | default }}.
If that doesn’t work you could change to
{{ states('sensor.supervisor_updates') | default(None) }}

And do the same for {{ states('sensor.hacs') }}

With this there’s no need for if-statements.

I appreciate your help, thank you very much for your time. I tried using the | default before I tried using the if statements. I just tried again, removing the if statements, using both | default and | default(None) in just the two main sensors, and then in every set of {{ }} and it still throws the same error in all cases. So there is something that I still don’t understand…

Can you show the exact error when you use | default }}?

And maybe you can start over with one of the three parts that I see in your content, to not muddy the water too much?

Okay, I simplified it way down. Here is the card now:

    - type: markdown
      title: Updates Available

      content: |
      
          <ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}

          > {% for addon in states.sensor.supervisor_updates.attributes.addons %}
          > &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
          > {% endfor %}

Here’s what the card currently looks like after HA finishes starting:

temp

And here are the errors:

2021-04-11 14:20:21 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'
2021-04-11 14:20:21 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'
2021-04-11 14:20:22 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'

I’d suggest adding an if-statement in another place, the error is caused by states.sensor.supervisor_updates.attributes.addons.
I’ve just added the 2 lines, you can add the correct formatting yourself.

    - type: markdown
      title: Updates Available

      content: |
      
          <ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}

          if states('sensor.supervisor_updates') != None
          > {% for addon in states.sensor.supervisor_updates.attributes.addons %}
          > &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
          > {% endfor %}
         endif

Still not working. Card:

    - type: markdown
      title: Updates Available

      content: |
      
          <ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}

          {% if states('sensor.supervisor_updates') != None %}
            > {% for addon in states.sensor.supervisor_updates.attributes.addons %}
            > &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
            > {% endfor %}
          {% endif %}

Errors:

2021-04-11 14:51:15 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'
2021-04-11 14:51:15 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'
2021-04-11 14:51:15 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}'
2021-04-11 14:51:15 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
{% if states('sensor.supervisor_updates') != None %}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}
{% endif %}'
2021-04-11 14:51:16 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default(None) }}
{% if states('sensor.supervisor_updates') != None %}
> {% for addon in states.sensor.supervisor_updates.attributes.addons %}
> &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
> {% endfor %}
{% endif %}'

access the attributes safely

state_attr('sensor.supervisor_updates', 'addons')
1 Like

Thank you so much, Petro, that’s fixed it! I still don’t understand why the if statements didn’t work,
but I will just accept I’m above my pay grade here, and keep on learning :slightly_smiling_face:

For reference, if anyone comes across this and is interested, here’s my final working card that throws no errors on HA startup:

    - type: markdown
      title: Updates Available
      card_mod:
        style: |
          :host {
            --card-mod-icon-color: #42a5f5;
            font-size: 1.1em;
          }
      content: |
        <ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;&nbsp;&nbsp;Add-ons needing update: &nbsp;&nbsp; {{ states('sensor.supervisor_updates') | default }}
        > {% for addon in state_attr('sensor.supervisor_updates', 'addons') %}
        >   &nbsp;&nbsp;{{ addon.name }}&nbsp; {{ addon.version }} -> {{ addon.version_latest }}
        > {% endfor %}
        
        <ha-icon icon="hacs:hacs"></ha-icon>&nbsp;&nbsp;&nbsp;HACS updates available: &nbsp;&nbsp; {{ states('sensor.hacs') | default }}
        > {% for repo in state_attr('sensor.hacs', 'repositories') %}
        >   &nbsp;&nbsp;{{ repo.display_name }} {{ repo["installed_version"] }} -> {{ repo["available_version"] }}
        > {% endfor %}
        
        |          |    Current | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Latest |
        |--- |:---: |:---: |:---: |
        | **Supervisor**&nbsp;&nbsp;&nbsp; | {{ state_attr('sensor.supervisor_updates', 'current_version') }} | | {{ state_attr('sensor.supervisor_updates', 'newest_version') }} |
        | **Audio** | {{ states.sensor.updater_audio.attributes.current_version }} | | {{ states.sensor.updater_audio.attributes.newest_version }} |
        | **CLI** | {{ states.sensor.updater_cli.attributes.current_version }} | | {{ states.sensor.updater_cli.attributes.newest_version }} |
        | **DNS** | {{ states.sensor.updater_dns.attributes.current_version }} | | {{ states.sensor.updater_dns.attributes.newest_version }} |
        | **Multicast** | {{ states.sensor.updater_multicast.attributes.current_version }} | | {{ states.sensor.updater_multicast.attributes.newest_version }} |
        | **Observer** | {{ states.sensor.updater_observer.attributes.current_version }} | | {{ states.sensor.updater_observer.attributes.newest_version }} |

which looks like this at the moment:

temp2

Edited to add: Sensors created with the help of this thread:
Update notifications! Core, HACS, Supervisor and Addons - Share your Projects! - Home Assistant Community (home-assistant.io)

Hello everyone. I receive a template sensor error with a markdown card

Here is my code located in the decluttering_templates.yaml card:

  summary:
    card:
      type: markdown
      card_mod:
        style: |
          ha-card {
            background: none;
            box-shadow: none;
            font-family: Quicksand;
            margin-bottom: -0.9em;
            text-align: center
          }
      content: |
        {{ states("sensor.date_fr") | default }}, nous fêtons les {{ states("sensor.nameday_fr") | default }}
        
        {{ states.sensor.summary.attributes.text }}

And here is the error:

Template variable error: ‘None’ has no attribute ‘attributes’ when rendering ‘{{ states(“sensor.date_fr”) | default }}, nous fêtons les {{ states(“sensor.nameday_fr”) | default }} {{ states.sensor.summary.attributes.text }}’

I hope someone can help me solve it.

Thanks.

Just from the message, it would seem sensor.summary does not exist

The sensor exist with the text as attribute:

I am now getting the following errors in the logs after the October update:

Template variable error: 'None' has no attribute 'attributes' when rendering '<ha-icon icon="mdi:home-assistant"></ha-icon>&nbsp;**Add-ons needing update:** &nbsp;&nbsp; {{ states.sensor.supervisor_updates.state }} > {% for addon in states.sensor.supervisor_updates.attributes.addons %} > {{ addon.name }}&nbsp; {{ addon.installed }} -> {{ addon.version }} > {% endfor %} <ha-icon icon="hacs:hacs"></ha-icon>&nbsp;**HACS updates available:** &nbsp;&nbsp; {{ states.sensor.hacs.state }} > {% for repo in states.sensor.hacs.attributes.repositories %} > **{{ repo.display_name }}** _{{ repo["installed_version"] }}_ -> _{{ repo["available_version"] }}_ > {% endfor %} --- | | Current&nbsp;&nbsp;&nbsp; | Latest | |--- |:---: |:---: | | **Supervisor**&nbsp;&nbsp;&nbsp; | {{ states.sensor.supervisor_updates.attributes.current_version }} | {{ states.sensor.supervisor_updates.attributes.newest_version }} |'

Because you’re not using state_attr, you’re using the long form for getting attributes. That’s prone to issues when the entities don’t exist

I see your answer above state_attr(‘sensor.supervisor_updates’, ‘addons’) but I am confused with the braces and bracket placement.
I have {% for addon in states.sensor.supervisor_updates.attributes.addons %}
does this change to {% for addon in state_attr(‘sensor.supervisor_updates’, ‘addons’) %}
?

All you have to understand is that you should replace all instances of

states.abc.xyz.attributes.ijk

with

state_attr('abc.xyz', 'ijk')

Thank you. I’m getting there!
What does  &nbsp mean?

Non breaking space

That’s html not templating