Yaml mode - jinja2 with lovelace_gen error

Hello everyone,

I am currently trying to change my frontend a littlebit. As I am now working a lot with custom cards where it is not possible to use the ui editor I am trying to switch to the yaml mode.

decluttering_templates: !include ui/test_decluttering.yaml
views: !include ui/test_views.yaml

For some smaller cards it is working. But I have at least one configurations which is crashing the whole ui.
This is the code I am using in my views:

automation_template:
  card:
    type: custom:mushroom-template-card
    entity: "[[entity]]"
    primary: "[[primary]]"
    secondary: |-
      {% if is_state(entity, 'on') %}
        An
      {% elif is_state(entity, 'off') %}
        Aus
      {% else %}
        Nicht verfügbar
      {% endif %}

I am using it this way in my views.yaml:

        - type: custom:decluttering-card
          template: automation_template
          variables:
            - entity: automation.licht_abstellraum_aus
            - primary: Licht Abstellraum

But this forces my whole Dashboard to crash. In the log I can see the following output:


  File "/config/ui/test_decluttering.yaml", line 336, in top-level template code
    {% if is_state(entity, 'on') %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/utils.py", line 83, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I also have a problem without decluttering card. In my views I get the following error:


 File "/config/ui/main_views.yaml", line 94, in top-level template code
    {% set time = now().hour %} {% if (time >= 18) %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/utils.py", line 83, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'now' is undefined

Why is this failing and how can I fix this?

Not using mushrooms, but it should be like this:

automation_template:
  card:
    ...
    entity: '[[ENTITY]]'
    ...
    secondary: |-
      {% if is_state('[[ENTITY]]', 'on') %}

Suggest to use capitals to distinguish variables from other stuff.

Unfortunately this doesn’t fix the issue. It is also crashing when I have just a view with for example: secondary: “{{now()}}”

If this this your 1st testing of decluttering card - suggest to test with standard card.

decluttering_templates:
  automation_template:
    card:
      type: markdown
      title: test
      content: |-
        {% if is_state('[[ENTITY]]', 'on') -%}
          An
        {%- else -%}
          Aus
        {%- endif %}

There is a dedicated thread, no need to create a new one.

Ok, I realized it is related to the lovelace_gen by @thomasloven

So it seems like with this I can do a lot stuff but for now I am not able to print now().
Or I need a different syntax.

This is my smallest example which does not work:

# lovelace_gen
- title: Home
  cards:
    - type: custom:mushroom-template-card
      primary: '{{now()}}'
      secondary: ''
      icon: mdi:home

I am not sure if this does work with lovelace_gen. But how can I build a Dashboard with this values with lovelace_gen?

As it is related to lovelace_gen I will ask in this thread for help and close this one here.

lovelace_gen does not have access to HA jinja functions, you’re doing that in a few places. now() and is_state are HA jinja functions.

If you want to put templates into a card that supports jinja template from lovelace_gen, you need to use {% raw %}{% endraw %}. This is spelled out in the lovelace_gen documentation.

e.g.

# lovelace_gen

....

abc: {% raw %}'{{ now() }}'{% endraw %}

the {% raw %}{% endraw %} is for lovelace gen to not look at the interior contents, which ultimately end up being the template you want for the abc field.

Thank you @petro

that worked