đŸ”č Lovelace_gen - Add abilities to ui_lovelace.yaml

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?

i’ve had general issues with relative paths, so i moved to using absolute paths.

Is there a way to parameterize the entity (e.g. input_boolean.living_room_motion_override) in custom_fields?

I have 3 ceiling lights/fans and split it to 2 yaml files:

ceiling_light.yaml

type: custom:button-card
name: {{ name }}
entity: {{ entity }}
tap_action:
  action: toggle
hold_action:
  action: call-service
  service: input_boolean.toggle
  service_data:
    entity_id: {{ entity_id }}
show_icon: true
show_name: true
show_state: false
styles:
  card:
    - font-size: 16px
  icon:
    - height: 30px
  grid:
    - grid-template-areas: '"lock lock" "i i" "n n"'
    - grid-template-columns: 0px
    - grid-template-rows: 0px
custom_fields:
  lock: >
    [[[
    if (states['**input_boolean.living_room_motion_override**'].state == 'on')
    return `<ha-icon
      icon="mdi:motion-sensor-off"
        style="width: 15px; height: 15px; color: red;">`
    ]]]

ceiling_fan.yaml

type: custom:button-card
name: {{ name }}
entity: {{ entity }}
icon: {{ icon }}
tap_action:
  action: toggle
hold_action:
  action: none
show_icon: true
show_name: true
show_state: false
styles:
  card:
    - font-size: 16px
  icon:
    - height: 30px

I then use !include:

  - type: horizontal-stack
    cards:
      - !include
        - ceiling_light.yaml
        - name: "Light"
          entity: switch.sonoff_100092da5b_1
          entity_id: input_boolean.living_room_motion_override
      - !include
        - ceiling_fan.yaml
        - name: "Fan Low"
          entity: switch.sonoff_100092da5b_3
          icon: mdi:arrow-down-bold
      - !include
        - ceiling_fan.yaml
        - name: "Fan High"
          entity: switch.sonoff_100092da5b_4
          icon: mdi:arrow-up-bold

I just need to parameterize ‘input_boolean.living_room_motion_override’ in ceiling_light.yaml to be able to try the split files in 3 rooms.

Any help is appreciated.

You might be having issues in traversing the folders.

Use 2 periods to go out of the current folder. For example:

> folder_1
> > folder_1_1
> > > folder_1_1_1 (current folder)
> > > > folder_1_1_1_1
> > > folder_1_1_2
> > folder_1_2
> > > folder_1_2_1

From folder_1_1_1 to folder_1_1_1_1:

folder_1_1_1_1/filename

From folder_1_1_1 to folder_1_1_2:

../folder_1_1_2/filename

I hope I did that correctly.

I am not sure exactly what you are asking for. But if I understand correctly, isn’t already this the variable ‘entity_id’? You can use this in the custom fields as w noell. Just remove the quotes and write

if states(entity_id) == 'on') 

What have you tried?

I tried:

custom_fields:
  lock: >
    [[[
    if (states[{{ lock }}].state == 'on')
    return `<ha-icon
      icon="mdi:motion-sensor-off"
        style="width: 15px; height: 15px; color: red;">`
    ]]]

I also tried parameterizing the entire script:

custom_fields:
  lock: {{ lock }}

I think I got it. I re-analyzed the syntax as well as the error messages thrown. I came up with:

custom_fields:
  lock: >
    [[[
    if (states['{{ lock }}'].state == 'on')
    return `<ha-icon
      icon="mdi:motion-sensor-off"
        style="width: 15px; height: 15px; color: red;">`
    ]]]

Thanks for the nudge.

1 Like

Below are 3 renderings:

  1. Left is right on the view.yaml
  2. Middle is via a browser_mod.popup
  3. Right is via a browser_mod.popup and lovelace_gen macro

Left - observe that “Off” does not show under the “Nook Speaker” media player.

Middle - again, observe that “Off” does not show under the “Nook Speaker” media player.

Right - now, observe that “Off” shows under the “Nook Speaker” media player.

How do I remove the state text? I tried the media_player idle_view options but no luck.

Here is the code:

# lovelace_gen

{% macro tap_action_entities(entity) -%}
                    - entity: {{ entity }}
{% if media_player %}
                      type: 'custom:mini-media-player'
                      group: true
{% else %}
                      tap_action:
                        action: none
{% endif %}
{%- endmacro %}

title: Test
path: test
panel: true
icon: ''

cards:

  - type: custom:layout-card
    layout: vertical
    cards:

      - type: entities
        entities:
          - entity: 'switch.gosund_dc4f229cc629_relay_2'
            name: "Cat Water"
          - entity: 'media_player.nook_speaker'
            name: "Nook Speaker"
            group: true
            type: 'custom:mini-media-player'

      - break

      - type: entities
        entities:
          - type: 'custom:button-card'
            name: "via browser_mod.popup"
            tap_action:
              action: call-service
              service: browser_mod.popup
              service_data:
                title: " "
                card:
                  type: entities
                  show_header_toggle: false
                  entities:
                    - entity: 'switch.gosund_dc4f229cc629_relay_2'
                      name: "Cat Water"
                    - entity: 'media_player.nook_speaker'
                      name: "Nook Speaker"
                      group: true
                      type: 'custom:mini-media-player'
                deviceID:
                  - this

      - break

      - type: entities
        entities:
          - type: 'custom:button-card'
            name: "via browser_mod.popup and lovelace_gen macro"
            tap_action:
              action: call-service
              service: browser_mod.popup
              service_data:
                title: " "
                card:
                  type: entities
                  show_header_toggle: false
                  entities:
                    {{ tap_action_entities('switch.gosund_dc4f229cc629_relay_2') }}
                      name: "Cat Water"
                    {{ tap_action_entities('media_player.nook_speaker') }}
                      name: "Nook Speaker"
                deviceID:
                  - this

Well, in the first two cases you have a mini-media-player row, in the last one it’s the default entities row since media_player is undefined and thus not true.