Need help with a template error!

Error log:

2021-09-11 12:52:59 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 12:52:59 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>

ui-lovelace.yaml card input

                      - type: markdown
                        card_mod:
                          style:
                            ha-markdown$: |
                              * {
                                font-family: FormulaOne, "Titillium Web";
                              }
                        content: >
                          {% set nr = states.sensor.formula_one_sensor.attributes.next_race %}
                          {% if not(nr == None) %}
                          <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp;
                          {{ nr.raceName }}</h1>
                    
                          <small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
                    
                          <a target="_new" href="{{nr.Circuit.url}}">
                            <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
                          </a>
                          {{nr.Circuit.circuitName}}
                          {% endif %}

Help is much appreciated!

Please post again properly formatted.

You can easily copy the error message by hitting the copy button top right in the chosen log entry.

Then use the code formatting button

and insert your yaml code so everyone can read and edit without pain.

Thx, Done!

1 Like

How did you set up your racing sensor?

With Hacs adding custom integration.

When Home Assistant evaluated the template, this entity didn’t have attributes:

states.sensor.formula_one_sensor.attributes.next_race

When did you receive the error? On startup, after reloading templates, or does this happen all the time?

If it happens on startup/reload, it’s probably due to the entity being initialized. If that’s the case then you will need to enhance your template to handle this situation.

Indeed on startup. Can you help me how to enchance the template?

The template does attempt to handle the situation when nr is None. However, it doesn’t do it correctly.
Change this:

{% set nr = states.sensor.formula_one_sensor.attributes.next_race %}

to this:

{% set nr = state_attr('sensor.formula_one_sensor', 'next_race') %}

Changed the template, but unfortunately same errors in log.

Post the error message. I want to see if it’s complaining about the attribute or the entity.

Whats new is that there are now a lot of messages.

2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:46 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:47 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:51 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% set nr = states.sensor.formula_one_sensor.attributes.next_race %} {% if not(nr == None) %} <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp; {{ nr.raceName }}</h1>
<small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
<a target="_new" href="{{nr.Circuit.url}}">
  <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
</a> {{nr.Circuit.circuitName}} {% endif %}'
2021-09-11 15:11:51 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:51 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'
2021-09-11 15:11:51 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th colspan="2">Drivers</th>
      <th class="center">Pts</th>
    </tr>
  </thead>
  <tbody>
{% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
        <td>{{driver.position}}</td>
        <td>{{driver.Driver.code}}</td>
        <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
        <td>{{driver.points}}</td>
    </tr>
{% endfor %}
  </tbody>
</table>'

It’s a different error message and, as I had suspected, it’s now complaining that the entity (sensor.formula_one_sensor) doesn’t exist. That means the template must first check if the entity is not None before proceeding to get the next_race attribute.

If you created this template then you probably know how to modify it to make the additional test. However, let us know if you need help doing it.


NOTE

One of the error messages contains templating code (a for-loop) that doesn’t exist in the example you posted. It implies you may have more than one markdown card or something else that refers to the same sensor.

Unfortunately not made by myself, comes from the repo from the integration. I’d really appreciate it if you could help me out. Thanks in advance! The owner from the repo is not replying to me.

Indeed, it’s for de F1 driver standings.

                      - type: markdown
                        card_mod:
                          style:
                            ha-markdown$: |
                              table {
                                width: 100%;
                                border-spacing: 0;
                                border-collapse: separate;
                              }
                              th {
                                color: white;
                                background: black;
                              }
                              tr:nth-child(even) {
                                background-color: #dddddd;
                                color: rgb(33,33,33);
                              }
                              td:nth-child(1) {
                                width: 50px;
                                text-align: center;
                              }
                              td:nth-child(2) {
                                width: 50px;
                                text-align: center;
                              }
                              td:nth-child(4) {
                                width: 60px;
                                text-align: center;
                              }
                        content: |-
                          <table>
                            <thead>
                              <tr>
                                <th>&nbsp;</th>
                                <th colspan="2">Drivers</th>
                                <th class="center">Pts</th>
                              </tr>
                            </thead>
                            <tbody>
                          {% for driver in states.sensor.formula_one_sensor.attributes.drivers %} <tr>
                                  <td>{{driver.position}}</td>
                                  <td>{{driver.Driver.code}}</td>
                                  <td>{{driver.Driver.givenName }} {{driver.Driver.familyName }}</td>
                                  <td>{{driver.points}}</td>
                              </tr>
                          {% endfor %}
                            </tbody>
                          </table>

You will have to add a test to confirm the sensor is not None before proceeding to the for-loop.

Basically, whoever made this custom integration didn’t anticipate the possibility that the sensor wouldn’t be available on startup. All the templating code uses the sensor without ensuring it actually exists. You don’t normally have to unless the sensor takes time to be initialized at startup.

Like this?

                      - type: markdown
                        card_mod:
                          style:
                            ha-markdown$: |
                              * {
                                font-family: FormulaOne, "Titillium Web";
                              }
                        content: >
                          {% if states.sensor.formula_one_sensor ==  none %}off{% else %}{{ states.sensor.formula_one_sensor.state }}{% endif %}
                          {% set nr = state_attr('sensor.formula_one_sensor', 'next_race') %}
                          {% if not(nr == None) %}
                          <h1><img height="22" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-{{nr.Circuit.Location.country}}.png">&nbsp;
                          {{ nr.raceName }}</h1>
                    
                          <small>{{ as_timestamp(nr.date + ' ' + nr.time) | timestamp_custom("%Y-%m-%d %H:%M") }}</small>
                    
                          <a target="_new" href="{{nr.Circuit.url}}">
                            <img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Track%20icons%204x3/{{nr.Circuit.Location.country}}%20carbon.png.transform/4col/image.png">
                          </a>
                          {{nr.Circuit.circuitName}}
                          {% endif %}

No, you shouldn’t reference an entity’s state using this method because it will result in an error if the entity doesn’t exist:

states.sensor.whatever

Use the states() function which will report None if the entity doesn’t exist.

states('sensor.whatever')

All of this explained the Templating - States documentation and includes the following warning:

Thx! This code solved it!

                          {% if is_state('sensor.formula_one_sensor', 'none') %} off {% else %} {% endif %}