custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.93k stars 233 forks source link

Jinja2 templates don't work for navigation_path and maybe others? #702

Closed Clooos closed 1 year ago

Clooos commented 1 year ago

Checklist

Describe the bug I am trying to create buttons that dynamically change to fit other entities. However, templates don't work for navigation_path, it only render the raw code instead of the link:

/lovelace/%7B%%20from%20'dynamic_room_card_variables.jinja'%20import%20path%%7D%20%7B%7B%20path%20%7D%7D

It works for name and icon with the exact same Jinja2 template.

Version of the card Version: 3.5.0

To Reproduce This is the configuration I used:

- type: custom:paper-buttons-row
  buttons:
    - name: >
        {% from 'dynamic_room_card_variables.jinja' import name
        %} {{ name }}
      layout: icon|name
      icon: >
        {% from 'dynamic_room_card_variables.jinja' import icon
        %} {{ icon }}
      tap_action:
        action: navigate
        navigation_path: >
          {% from 'dynamic_room_card_variables.jinja' import path
          %} {{ path }}
...

My custom template dynamic_room_card_variables.jinja https://www.home-assistant.io/docs/configuration/templating/#reusing-templates

 {% set sensors = 
  [
    {'name': 'salon', 'last_seen': as_timestamp(states.binary_sensor.salon_motion.last_updated) },
    {'name': 'cuisine', 'last_seen': as_timestamp(states.binary_sensor.hue_motion_sensor_1_motion.last_updated)},
    {'name': 'salle_a_manger', 'last_seen': as_timestamp(states.binary_sensor.presence_salle_a_manger.last_updated)},
    {'name': 'chambre_parentale', 'last_seen': as_timestamp(states.binary_sensor.hue_motion_sensor_2_motion.last_updated)},
    {'name': 'salle_de_douche', 'last_seen': as_timestamp(states.binary_sensor.salle_de_bain_motion.last_updated)} 
  ] 
%}

{% set index %}
    {% for sensor in sensors|sort(attribute='last_seen', reverse=True) %}
        {% if sensor.name == 'cuisine' %}
            {{ loop.index0 + 1 }}
        {% endif %}
    {% endfor %}
{% endset %}

{% set name %}
    {% for sensor in sensors|sort(attribute='last_seen', reverse=True) %}
        {% if loop.index0 == 0 and sensor.name == 'cuisine' %}
            Cuisine
        {% elif loop.index0 == 0 and sensor.name == 'salon' %}
            Salon
        {% elif loop.index0 == 0 and sensor.name == 'salle_a_manger' %}
            Salle à manger
        {% elif loop.index0 == 0 and sensor.name == 'chambre_parentale' %}
           Chambre
        {% elif loop.index0 == 0 and sensor.name == 'salle_de_douche' %}
           Salle de douche
        {% endif %}
    {% endfor %}
{% endset %}

{% set icon %}
    {% for sensor in sensors|sort(attribute='last_seen', reverse=True) %}
        {% if loop.index0 == 0 and sensor.name == 'cuisine' %}
            mdi:fridge
        {% elif loop.index0 == 0 and sensor.name == 'salon' %}
            mdi:sofa
        {% elif loop.index0 == 0 and sensor.name == 'salle_a_manger' %}
            mdi:silverware-fork-knife
        {% elif loop.index0 == 0 and sensor.name == 'chambre_parentale' %}
           mdi:bed
        {% elif loop.index0 == 0 and sensor.name == 'salle_de_douche' %}
           mdi:shower-head
        {% endif %}
    {% endfor %}
{% endset %}

{% set path %}
    {% for sensor in sensors|sort(attribute='last_seen', reverse=True) %}
        {% if loop.index0 == 0 and sensor.name == 'cuisine' %}
           /lovelace/cuisine
        {% elif loop.index0 == 0 and sensor.name == 'salon' %}
           /lovelace/salon
        {% elif loop.index0 == 0 and sensor.name == 'salle_a_manger' %}
           /lovelace/salle_a_manger
        {% elif loop.index0 == 0 and sensor.name == 'chambre_parentale' %}
           /lovelace/chambre
        {% elif loop.index0 == 0 and sensor.name == 'salle_de_douche' %}
           /lovelace/salle_de_douche
        {% endif %}
    {% endfor %}
{% endset %}

Screenshot

Pasted Graphic

Expected behavior I would like to use jinja2 templates everywhere, but there is maybe an other way to achieve what I need?

Thanks in advance!

Clooos commented 1 year ago

Wrong GitHub repo, I need a break I guess 😅