thomasloven / lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
MIT License
210 stars 16 forks source link

Templates no longer render after update to 1.4.0 #120

Closed kaechele closed 4 months ago

kaechele commented 4 months ago

Somehow templates stopped rendering in my card after updating to 1.4.0 today. Everything works fine after a downgrade to 1.3.2.

Here's what it should look like: image

This is what it looks like with 1.4.0: image

Card YAML

type: entities
show_header_toggle: false
title: At a Glance
entities:
  - type: custom:template-entity-row
    entity: sensor.recollect_waste
    name: >
      Waste Pickup:  {% for item in
      state_attr('sensor.recollect_waste','pickup_types') -%}

      {%- if loop.first == false %}, {% endif -%}

      {{ item |  replace("recycling_Cart","Recycling") |
      replace("Organics_Cart","Organics") | replace("Garbage_Cart","Garbage") |
      replace("TextileCollectionWeek","Textiles") |
      replace("ElectronicsCollectionWeek", "Electronics") | title }}

      {%- endfor -%}
    icon: |
      {%- set items = state_attr('sensor.recollect_waste','pickup_types') -%}
      {%- if 'recycling_Cart' in items -%}mdi:newspaper
      {%- else -%}mdi:trash-can-outline
      {%- endif -%}
    active: |
      {%- set next = as_timestamp(states('sensor.recollect_waste')) -%}
      {%- set days = (next - as_timestamp(now())) / 86400 -%}
      {{ 0 < days < 1 }}
    state: |
      {%- set next = as_timestamp(states('sensor.recollect_waste')) -%}
      {%- set days = (next - as_timestamp(now())) / 86400 -%}
      {%- if days < 1.0 and days > 0 -%}Tomorrow
      {%- elif days < 0 and days > -1 -%}Today
      {%- elif days < -1 and days > -2 -%}Yesterday
      {%- elif days < -2 -%}{{ days | abs | round(0,'ceil') }} days ago
      {%- else -%}{{ days | round(0,'ceil') }} days
      {%- endif -%}
    secondary: >
      {%- set next = as_timestamp(states('sensor.recollect_waste')) -%} {%- if
      next | timestamp_custom('%a',false) != 'Thu' -%}⚠️ {% endif -%} {{ next |
      timestamp_custom('%A, %B %-d',false) }}
  - entity: >-
      sensor.alectra_utilities_corporation_powerstream_rate_zone_residential_rate
    type: custom:template-entity-row
    name: Electricity Rate
    state: >-
      {{
      states('sensor.alectra_utilities_corporation_powerstream_rate_zone_residential_rate')
      }} $/kWh
    secondary: >
      {%- set active_peak =
      state_attr('sensor.alectra_utilities_corporation_powerstream_rate_zone_residential_rate',
      'active_peak') -%} {%- if active_peak == 'on_peak' -%}On Peak {%- elif
      active_peak == 'mid_peak' -%}Mid Peak {%- else -%}Off Peak {%- endif -%}
    color: >
      {%- set active_peak =
      state_attr('sensor.alectra_utilities_corporation_powerstream_rate_zone_residential_rate',
      'active_peak') -%} {%- if active_peak == 'on_peak' -%}red {%- elif
      active_peak == 'mid_peak' -%}yellow {%- else -%}green {%- endif -%}
  - entity: sensor.airthings_living_room_co2
    name: CO₂
  - entity: sensor.airthings_living_room_radon
    name: Radon
    icon: mdi:radioactive
jasonc-glitch commented 4 months ago

I'm having the same issue running version 1.4 with Home Assistant 2024.2.1.

Have uninstalled the add on and reinstalled, multiple restarts and the problem remains.

Mariusthvdb commented 4 months ago

can confirm most templates to no longer work (if the config.entity is called?), all report None or undefined.in the logger

downgrading works 100% perfect, so templates are ok.

Logger: homeassistant.helpers.event
Source: helpers/template.py:574
First occurred: 08:43:02 (20 occurrences)
Last logged: 09:00:17

Error while processing template: Template<template=({% if states[config.entity] is not none %} {% set id = state_attr(config.entity,'friename').split('hygro')[0] %} {{id}} {% else %} Initialiseren {% endif %}) renders=6>
Error while processing template: Template<template=({% if states[config.entity] is not none %} {% set id = state_attr(config.entity,'friname').split('hygro')[0] %} {{id}} {% else %} Initialiseren {% endif %}) renders=6>
Error while processing template: Template<template=({% if states[config.entity] is not none %} {% set id = state_attr(config.entity,'frname').split('hygro')[0] %} {{id}} {% else %} Initialiseren {% endif %}) renders=6>
Error while processing template: Template<template=({% if states[config.entity] is not none %} {% set id = state_attr(config.entity,'fname').split('hygro')[0] %} {{id}} {% else %} Initialiseren {% endif %}) renders=6>
Error while processing template: Template<template=({% if states[config.entity] is not none %} {% set id = state_attr(config.entity,'name').split('hygro')[0] %} {{id}} {% else %} Initialiseren {% endif %}) renders=6>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 572, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2332, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 7, in top-level template code
jinja2.exceptions.UndefinedError: 'id' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 698, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 574, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'id' is undefined

and yet this secondary:

      - type: custom:template-entity-row
        entity: binary_sensor.donker_buiten
        name: >
          Donker buiten (< -4°)
        secondary: >
          {% if states[config.entity] is not none %}
            {{relative_time(states[config.entity].last_changed)}} ago, Elevation:
            {{state_attr('sun.sun','elevation')}}
          {% else %} Not yet set, Elevation: {{state_attr('sun.sun','elevation')}}
          {% endif %}

still works fine. so, it is not that straightforward just yet. Need to find out what was changed specifically, and why this breaks templates, but not all

I also can confirm the jinja templates to still work in dev tools template, and dont show any error at all.

thomasloven commented 4 months ago

I was not able to reproduce this, but I've tried to fix the only related thing that was changed in 1.4.0.

Mariusthvdb commented 4 months ago

can confirm the template to show correctly again, thx

Scherm­afbeelding 2024-02-12 om 11 47 45
jasonc-glitch commented 4 months ago

Also fixed it for me. Thank you!

kaechele commented 4 months ago

1.4.1 works. Thanks!