Problem with decluttering card and lovelace_gen together

I’m trying to use decluttering card and lovelace_gen together but ran in to a problem where variables from the decluttering card isn’t parsed the way they’re supposed to with lovelace_gen.

I have this declaration: decluttering_templates: !include test_template.yaml

test_template.yaml:

# lovelace_gen
test_template:
  card:
    {% set showIt = '[[showMe]]' %}
    {% if showIt == 'a' %}
    type: custom:mushroom-title-card
    title: '{{showIt}} - a'
    {% elif showIt == 'b' %}
    type: custom:mushroom-entity-card
    entity: sensor.my_sensor
    {% else %}
    type: custom:mushroom-title-card
    title: '{{showIt}} - failed'
    {% endif %}

and my decluttering-card:

- type: custom:decluttering-card
  template: test_template
  variables:
    - showMe: 'a'


- type: custom:decluttering-card
  template: test_template
  variables:
    - showMe: 'b'

the problem is that lovelace_gen seems to interpret [[showMe]] as 'showMe' instead of 'a' or 'b'.
The output becomes a - failed or b - failed.

Is there a way to solve this?

I just now noticed that the decluttering card use javascript for variables so it won’t be possible to pass those into jinja which lovelace_gen is using, so I guess one of these 2 has to be excluded from my code.

So now the question is how to solve this problem. Instead of an example like above I’ll show you one of my real case scenarios.

I’m using tabbed-card and what I need is to show/hide tabs depending on the existence of entities in an area.
So let’s say I have 2 tabs:

  type: custom:tabbed-card
  tabs:
    # LIGHTS - section
    ##########
    - card:
        - type: custom:auto-entities
          card:
            type: entities
          filter:
            - domain: light
              area: '{{area_name}}' # this comes from another file using lovelace_gen's !include
      attributes:
        label: 'Lights'

    - card:
    # MEDIA - section
    ##########
        - type: custom:auto-entities
          card:
            type: entities
          filter:
            - domain: media_player
              area: '{{area_name}}' # this comes from another file using lovelace_gen's !include
      attributes:
        label: 'Media'

If there’s no light entities, then the LIGHTS - section shouldn’t be shown, that includes the - cardpart that starts the section (each - card is a tab). Same goes for the MEDIA - section.

I tried using the decluttering card and card-mod to add the css display: none, it works, but to get it to actually work in a browser I had to do a hard refresh (ctrl+r) of the page every time cause it starts with showing all the tabs, after the refresh it would hide the tab.

So any ideas on how to solve this problem?
This is for my custom dashboard Aleborg Frontend.
I really wish that they could add templating possibilities to the frontend…

because you wrapped showMe in quotes.

if showMe is a variable…

{% set showIt = showMe | default %}

Just keep in mind that decluttering card will not ‘send values’ to lovelace_gen. Lovelace_gen generates your config before decluttering card is even used. In fact, nothing ‘can be sent’ to lovelace_gen.

lovelace_gen writes your configuration, home assistant loads the configuration. All card behavior occurs after home assistant loads the configuration, which is after lovelace_gen has written your config.


As a sidebar, you 100% do not need decluttering card with lovelace_gen. If you do, you’re doing something wrong. The whole idea behind lovelace_gen is to have jinja automatically generate your configuration from templated cards. Decluttering card is a way to template cards without lovelace_gen. While you can use both together, there’s really no need to use decluttering w/ lovelace_gen because it has the same functionality.

I’ve tried with and without quotes and brackets, the problem lays within the fact that in decluttering card you can’t use jinja in the template, only in the card. If it was possible then I wouldn’t use it.

In lovelace_gen you can’t use area_id or state etc, that’s why I need to combine them. And I’m using lovelace_gen to put a lot of my settings for the dashboard in one file.

Templates:

decluttering_templates:
  tabs_card_template: !include tabs_card_template.yaml
  page_template: !include page_template.yaml

Template: tabs_card_template.yaml

# lovelace_gen

# Without lovelace_gen jinja wont work here

    card:
      type: custom:tabbed-card
      tabs:
        # Lights
        #############################################
        {% set theLights = '[[showLights]]' %} # "set" was initially used to test the variable, shouldn't be needed at all 
        {% if theLights %}
        # this will never be true since theLights contains the text [[showLights]]
        - card: 
            type: custom:mushroom-title-card
            title: '{{ theLights }} - true section' 
        {% else %}
        # This will be displayed, but will contain [[showLights]] which is replaced with 'True' from javascript/json and display 'True - false section
        - card: 
            type: custom:mushroom-title-card
            title: '{{theLights }} - false section'
        {% endif %}
        #############################################

        # Multimedia
        #############################################
        {% set theMedia = showMedia %}
        {% if theMedia %}
        # this will never be true since theMedia is now undefined
        - card:
            type: custom:mushroom-title-card
            title: '{{ theMedia }} - true section'
        {% else %}
        # This will be displayed, but will display the text 'undefined - false section'
        - card:
            type: custom:mushroom-title-card
            title: '{{ theMedia }} - false section'
          attributes:
            label: 'Media'
            icon: "mdi:music"
        {% endif %}
        #############################################

Template: page_template.yaml

    card:
      - type: custom:decluttering-card
        template: tabs_card_template
        variables:
          # this could probably be done with  
          # showLights: >-
          #   area_entities('[[areaId]]')|string|contains('light.')
          - showLights: >-
              {% if (area_entities('[[areaId]]')|string|contains('light.')) -%}
              true
              {%- else -%}
              false
              {%- endif %}
          - showMedia: >-
              {% if (area_entities('[[areaId]]')|string|contains('media_player.')) -%}
              true
              {%- else -%}
              false
              {%- endif %}
          - title: '[[title]]'
          - areaId: '[[areaId]]'

File: view.yaml

# lovelace_gen
theme: {{ _global.tablet.settings.theme }}
title: {{ room.title }}
path: {{ room.areaId }}
icon: {{ room.icon }}
type: custom:ha-dashboard
usePanel: true
cards:
  - !include
    - page.yaml
    - room: {{ (room|tojson) }}

File: page.yaml

- type: custom:decluttering-card
  template: page_template
  variables:
    - title: '{{ room.title }}' # From lovelace_gen
    - areaId: '{{ room.areaId }}' # From lovelace_gen

There might be some errors and misspellings in the code since I wrote most of it out of my head and it’s also simplified a lot…

Saw that you had edited your post while I was writing my reply :grin:
The problem is that in lovelace_gen you can’t use area_id or state or anything from the hass api

Right, but this goes back to my original edits:

You have lovelace gen creating the configuration for your card here:

but you’re trying to populate showLights here:

lovelace_gen is done running well before your decluttering card is loaded. If you want raw jinja to be created from lovelace_gen in your decluttering card, you need to make use of {% raw %}

lastly, what you could do (which is what I do), is you can use a python script to generate a configuration for lovelace_gen. The python script simply writes my main configuration based on my system. I run the script whenever I make changes to the system.

I used to just maintain a large dictionary that built my config too in the first lovelace_gen file.

Also, you could just make use of a template card that allows you to template any field. But the long story short is, what you’re trying to do is a chicken/egg scenario that lovelace_gen cannot do.

Thanks @petro for the time you’re spending on this! :orange_heart:

hat’s what I meant with

To solve the problem before I knew this, I tried to make use of the decluttering card to see if it could solve my problem with getting area_entities. Using {% raw %} won’t help me much since I need variables stored in lovelace_gen variable function and they can’t be passed into the {% raw %} section.

This sounds interesting, do you have any example of that python file?
This is how my configuration file looks like for the moment: settings.yaml
I would love to get as much as possible of it automated.

And while I’m at it, is there a way to update changes made to lovelace_gen’s variables without restarting HA every time?

I don’t have an example of the python script. I’d have to dig around to find it, it’s missing from my public config.

I get around changing parameters by putting my lovelace_gen configuration in the initial file and passing everything round from there. I have an example of that, but it’s not easy to read.