thomasloven / lovelace-card-mod

🔹 Add CSS styles to (almost) any lovelace card
MIT License
1.05k stars 169 forks source link

Unable to Render a Reusable Jinja Macro Inside Card-Mod #296

Closed MizterB closed 9 months ago

MizterB commented 9 months ago

My Home Assistant version: 2023.9.2

My lovelace configuration method (GUI or yaml): YAML

What I am doing: I am trying to render a reusable Jinja macro inside the secondary info of an entity row.

What I expected to happen: The output of the macro is shown in the secondary info field of the entity row.

What happened instead: No output is shown

Minimal steps to reproduce: This is a simple set of steps to reproduce, not illustrative of a real-world use case.

1) Create a reusable Jinja macro in /config/custom_templates/sun.jinja, containing

{% macro show_elevation() %}
    The elevation is {{ state_attr('sun.sun', 'elevation') }}
{% endmacro %}

2) Reload the custom Jinja integration

3) Verify output in the Template Developer Tool Input:

{% import 'sun.jinja' as s %}{{s.show_elevation()}}

Output result is a string: The elevation is 46.62

4) Create an entities card, using card-mod to modify the secondary info text with the same template used in 3):

cards:
  - type: entities
    entities:
      - entity: sun.sun
        secondary_info: true
        style:
          hui-generic-entity-row:
            $: |
              .secondary::after {
                content: "{% import 'sun.jinja' as s %}{{s.show_elevation()}}";
              }

5) There is no text shown in the secondary info area

6) NOTE: If I change the template to content: "{{ state_attr('sun.sun', 'elevation') }}";, it renders correctly. The render issue only appears when rendering via a macro.

Error messages from the browser console: This appears unrelated, but shared for completeness:

[Error] TypeError: null is not an object (evaluating 'e.hass.config.location_name') — original-states-strategy.ts:71
    (anonymous function) (5226-t5bb6snarI0.js:1:29299)

By putting an X in the boxes ([]) below, I indicate that I:

ildar170975 commented 9 months ago

Try using “-%}”, “{%-“ inside the macro.

MizterB commented 9 months ago

Wow - that did solve the problem! Thank you very much for the advice!