šŸ”¹ Lovelace_gen - Add abilities to ui_lovelace.yaml

Hello. Newbie here. Iā€™ve just startes using HA and found out this plugin! Just awsome! Great work.
Iā€™d like some help:
I have some groups, each containing specific devices/entities, and Iā€™d like to create cards for all them.
For that, so far I took this approach:

{% macro room_card(group_id, group_name) -%}
  - type: 'custom:auto-entities'
    ...
{%- endmacro %}

cards:
  {{ room_card('group.room1', 'Room 1') }}
  {{ room_card('group.room2', 'Room 2') }}
  ...

But, Iā€™d like to make this a little bit more generic. Something like:

{% macro room_card(group) -%}
  - type: 'custom:auto-entities'
    card:
      title: *group.friendly_name*
      type: entities
      show_header_toggle: false
    filter:
      include:
        - group: {{ group}}
    ...
{%- endmacro %}

cards:
{% for room in *group.rooms* %}
  {{ room_card(room) }}
{% endfor %}

ps: variables between * are just for illustration.

My questions are:

  1. How can I get the specific name for each room? Is that possible?
  2. I tried using ā€˜rawā€™ tag, so I could use something like state_attr to get the entities of a group. But I couldnā€™t make it work this way. Iā€™m not sure If this is a valid approach.
  3. Besides, everytime I use ā€˜rawā€™ tag, my ā€˜auto-entitiesā€™ cards stop working. Is there a known conflict between these two plugins? (I have no other one installed, just these 2 for lovelace)
  4. I coudnā€™t use my ā€˜group.roomsā€™ directly in any way I could think of. I had to create a global variable ā€˜duplicatingā€™ this group. Is there another way?

Thanks.

{%raw%} and {%endraw%} are entirely decoupled from any card, the only way they could cause problems is by using them incorrectly. Be mindful of linebreaks.

Lovelace_gen generates a static lovelace configuration, and is unaware of your current Home Assistant state. Thus it doesnā€™t know anything about any entity, including groups.

Is

# lovelace_gen

required only if the file is using the feature to pass arguments to included files?

I ask this because I just realised my top level view file uses this feature and seems to be working without that line at the top.

Or am I mistaken and there is something else going on that I have missed?

As as an aside, is there a (significant) overhead in having # lovelace_gen in the file even if it is unnecessary?

Itā€™s been a while, but iirc, the # lovelace_gen line is actually only required to use jinja templates.

An no, thereā€™s very little overhead, and itā€™s run very infrequently.

Hi there, just found this and must say itā€™s pretty awesome. iā€™ve been refactoring my setup and still learning on jinja and templates and how to reference dictionaries and lists.

Iā€™m running into a challenge trying to pull in some data i saved in my global config for my camera entities
Here is the structure below (the values i changed to to remove my actual values here)

cameras:
  - name: <camera-name>
    entity: <camera-entity>
    linked_devices:
      - entity: <device-id>
        icon: <mdi:icon>
      - entity: <device_id2>
     ...

I am properly able to read in my name and entities in my camera page/card like so:

{% for camera in _global.cameras %}
- type: picture-glance
  title: {{ camera["name"]}}
  camera_image: {{ camera["entity"] }}
  #camera_view: live
  entities:
    - entity: device.id
{% else %}
- type: markdown
  content: 'cameras not defined'
{% endfor %}

This works fine with hardcoded picture glance entities ( device.id in this example). Iā€™m doing something wrong trying to read the list of linked_devices dicts though. can someone point me in the right direction for syntax? below is what iā€™m trying to do

{% for camera in _global.cameras %}
- type: picture-glance
  title: {{ camera["name"]}}
  camera_image: {{ camera["entity"] }}
  #camera_view: live
  entities:
    {% for linked_devices in _global.cameras["linked_devices"] %}
    - entity: {{ linked_devices["entity"] }}
      icon: {{ linked_devices["icon"] | default('mdi:lightbulb') }}
    {% endfor %}
{% else %}
- type: markdown
  content: 'cameras not defined'
{% endfor %}

when i try the last code chunk above, i am getting a null returned value in the picture-glanceā€™s entity

the help is much appreciated!

fixed my own problem again after tinkering. i needed to reference within the for loop vs trying to access it outside of the parent for loop. here is what fixed my issue for others

{% for camera in _global.cameras %}
- type: picture-glance
  title: {{ camera["name"]}}
  camera_image: {{ camera["entity"] }}
  #camera_view: live
  entities:
    {% for device in camera["linked_devices"] %}
    - entity: {{ device["entity"] }}
      icon: {{ device["icon"]|default('mdi:lightbulb') }}
    {% endfor %}
{% else %}
- type: markdown
  content: 'cameras not defined'
{% endfor %}

Has a fix been found for this issue in the meantime?
I am still running hass 0.106.6 because lovelace_gen breaks for me when I update.
By now I am quite some versions behind, to the point that HACS just broke, requiring version >0.110.

The behaviour for me is exactly the same as you described.
I am really curious if anyone is running >=0.107 without issues.
Thanks.

Iā€™m running 110.4 and have no issues. I donā€™t configure resources in the configuration.yaml but use the UI for that, not sure if that makes any difference.

Using storage mode (required for the above) for the frontend does not prevent you from creating yaml dashboards and making them the default.

I need help. I tried to install lovelace_gen in Home Assistant 111.1. But the system starts up in safe mode.
Tanks.

I use lovelace_gen to enable me to create button templates, is it possible to pass multi line variables?

e.g.
This works

- !include
  - templates/button_title.yaml
  - title: >
      [[[ return 'TRAINS TIMES (AT ' + states['sensor.next_train'].last_updated.substring(11, 19) + ')' ]]]

but Lovelace doesnā€™t like this at all:

- !include
  - templates/button_title.yaml
  - title: >
      [[[
        return 'TRAINS TIMES (AT ' + states['sensor.next_train'].last_updated.substring(11, 19) + ')'
      ]]]

Returning the error:

while parsing a block mapping in "/config/lovelace/templates/button_title.yaml", line 18, column 1 expected <block end>, but found ']' in "/config/lovelace/templates/button_title.yaml", line 23, column 1

The reason for wanting to do this is that sensor.DOMAIN.OBJECT_ID.last_updated is in the (UTC) format 2020-06-21 15:49:37.906135+00:00, and I want to display it in local time which takes more than one line of Javascript.

button_title.yaml looks like this (with some styling, not shown):

type: custom:button-card
name: >
  {{ title }}

(Iā€™ve also tried with ā€˜|ā€™ instead of ā€˜>ā€™)

Iā€™m using lovelace_gen quite a lot, but I have a problem using it inside popup_cards.

My template:

#lovelace_gen

{{group}}:
  title: {{title}}
  card:
    type: horizontal-stack
    cards:
      - type: custom:button-card
        template: border_spacer
      - type: custom:auto-entities
        filter:
          include:
            - group: {{ group }}
              options:
                type: custom:button-card
                template: default
                entity: this.entity_id

        sort:
          method: name
          numeric: true
        card:
          type: custom:layout-card
          column_num: 3
          justify_content: start
          layout: horizontal
          column_width: 33%
      - type: custom:button-card
        template: border_spacer

How I use it:

popup_cards:
  !include 
    - '../templates/verlichting_kamer_popup.yaml'
    - group: group.verlichting_keuken
      title: Keuken

This works ok. However, the moment I try to add a second more-info dialog popup like this:

 popup_cards:
   !include 
     - '../templates/verlichting_kamer_popup.yaml'
     - group: group.verlichting_keuken
       title: Keuken

   !include 
      - '../templates/verlichting_kamer_popup.yaml'
      - group: group.verlichting_woonkamer
        title: bla

I get this error message:

2020-07-02 23:19:27 ERROR (SyncWorker_7) [custom_components.lovelace_gen] while parsing a block mapping
in ā€œ/config/lovelace/views/120_kamers.yamlā€, line 3, column 1
expected <block end>, but found ā€˜<tag>ā€™
in ā€œ/config/lovelace/views/120_kamers.yamlā€, line 12, column 3

Any clues as to what is causing this (and a way to resolve it ? :slight_smile:

I think I know what the problem is, but have a hard time explaining it. Basically, your !include statements each return something like {group.verlichting_keuken: {title: ..., card: ...}}, and then they are put together into {popup_cards: {group.verlichting_keuken: ...} {group.verlichting_woonkamer: ...}} when what you really need is {popup_cards: {group.verlichting_keuken: ..., group.verlichting_woonkamer: ...}}.

It might work if you do <<: !include ..., but I canā€™t be sure.

A safer way would be to do:

popup_cards:
  group.verlichting_keuken: !include
    - ...
  group.verlichting_woonkamer: !include
    - ...
title: {{title}}
card:
  ...

Thanks for your response @thomasloven. You mean putting the filename on the same line as the !include? I tired that already, it gave the same result.

I will try the safe approach. Bit more code, but in the end it will still save me quite a lot :slight_smile:

EDIT: the safe way is working !

No I meant adding <<: before each !include. But the better way is better.

<<: didnā€™t work :slight_smile:

First time using lovelace_gen and am having an error
My lovelace_new.yaml file starts like this

# lovelace_gen
{% set lights = ['diningroom_light', 'kitchen_light_ceiling'] %}

and I get error when trying to view lovelace_new.yaml:

while scanning for the next token found character '%' that cannot start any token in "/config/lovelace_new.yaml", line 2, column 2

I have lovelace_gen in my configuration file just before my lovelace declaration

lovelace_gen:

lovelace:
  mode: storage
  dashboards:
    lovelace-yaml-optimized:
      mode: yaml
      title: FloorPlan
      icon: mdi:home
      show_in_sidebar: true
      filename: lovelace_new.yaml
    lovelace-yaml:
      mode: yaml
      title: Test
      icon: mdi:script
      show_in_sidebar: true
      filename: lovelace_old.yaml

lovelace GUI mode and lovelace_old work fine - doesnā€™t have any lovelace_gen code.

How do I know lovelace_gen has loaded?

In Developer tools -> Info you can see a list of the loaded integrations.

I havenā€™t had time to test lovelace_gen with the new configuration yet, but it could be that it doesnā€™t really work on the ā€œmainā€ file.

Try including a file in lovelace_new.yaml and use lovelace_gen in the included file.

Moving the lovelace_gen to secondary (included) file worked.
Thank you

As mentioned before, I am having issues getting this card to work with any version of HA above 0.106.6.
I am surprised others donā€™t seem to experience issues, so perhaps I am doing something wrong.

I stripped my Lovelace configuration to a single view home.yaml, consisting of:

# lovelace_gen
title: home
path: home
cards:
  - !include
    - ../templates/test.yaml
    - text: 'hello world'

content of templates/test.yaml:

# lovelace_gen
type: markdown
content: '##### {{ text }}'

Initially, this seems to work well (after refreshing Lovelace, I get a page with a single header showing ā€˜hello worldā€™).
But after a reboot, HA completely breaks down, half the components fail to load and it decides to load into safe mode.
Startup logs:

2020-07-05 14:11:58 ERROR (MainThread) [homeassistant.setup] Error during setup of component lovelace
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/setup.py", line 193, in _async_setup_component
    result = await asyncio.wait_for(task, SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/lovelace/__init__.py", line 97, in async_setup
    resource_collection = await create_yaml_resource_col(hass, yaml_resources)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/lovelace/__init__.py", line 222, in create_yaml_resource_col
    ll_conf = await default_config.async_load(False)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/lovelace/dashboard.py", line 186, in async_load
    self._load_config, force
  File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/lovelace/dashboard.py", line 204, in _load_config
    config = load_yaml(self.path)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 61, in load_yaml
    return yaml.load(conf_file, Loader=SafeLineLoader) or OrderedDict()
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 51, in get_single_data
    return self.construct_document(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 55, in construct_document
    data = self.construct_object(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 192, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 155, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 223, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 408, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in construct_sequence
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in <listcomp>
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 113, in _include_yaml
    return _add_reference(load_yaml(fname), loader, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 61, in load_yaml
    return yaml.load(conf_file, Loader=SafeLineLoader) or OrderedDict()
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 51, in get_single_data
    return self.construct_document(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 55, in construct_document
    data = self.construct_object(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 192, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 155, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 223, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 408, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in construct_sequence
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in <listcomp>
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 192, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 155, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 223, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 408, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in construct_sequence
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in <listcomp>
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 192, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 155, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 223, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 408, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in construct_sequence
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 130, in <listcomp>
    for child in node.value]
  File "/srv/homeassistant/lib/python3.7/site-packages/yaml/constructor.py", line 100, in construct_object
    data = constructor(self, node)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/yaml/loader.py", line 111, in _include_yaml
    fname = os.path.join(os.path.dirname(loader.name), node.value)
  File "/usr/lib/python3.7/posixpath.py", line 94, in join
    genericpath._check_arg_types('join', a, *p)
  File "/usr/lib/python3.7/genericpath.py", line 149, in _check_arg_types
    (funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'list'
2020-07-05 14:12:00 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of frontend. Setup failed for dependencies: lovelace
2020-07-05 14:12:00 ERROR (MainThread) [homeassistant.setup] Setup failed for frontend: Could not set up all dependencies.
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of panel_iframe. Setup failed for dependencies: frontend
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Setup failed for panel_iframe: Could not set up all dependencies.
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of map. Setup failed for dependencies: frontend
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Setup failed for map: Could not set up all dependencies.
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of lovelace_gen. Setup failed for dependencies: lovelace
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Setup failed for lovelace_gen: Could not set up all dependencies.
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of hacs. Setup failed for dependencies: frontend, lovelace
2020-07-05 14:12:01 ERROR (MainThread) [homeassistant.setup] Setup failed for hacs: Could not set up all dependencies.
2020-07-05 14:12:02 ERROR (MainThread) [homeassistant.setup] Unable to set up dependencies of logbook. Setup failed for dependencies: frontend
2020-07-05 14:12:02 ERROR (MainThread) [homeassistant.setup] Setup failed for logbook: Could not set up all dependencies.

Iā€™m not much of a programmer, but it seems to me that at startup HA tries to load the yaml files using its built-in parser, which does not support passing more than one argument in the !include method. I tried moving the lovelace_gen: definition in configuration.yaml all the way to the top, but it does not seem to change the order in which things are loaded.

How are others using this plugin without this issue?
Any suggestions of things I could try to pinpoint the problem?

Loading HA directly into a lovelace_gen generated UI gives an error, probably because lovelace_gen did not load yet, until I refresh the browser.

Is there a way to delay loading the UI until lovelace_gen has been loaded? Or automatically refresh after lovelace_gen is loaded?