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

Yes, you’re right, sorry.

So, I’m back looking at this and whilst this works:

title: Garden
cards:
  - type: horizontal-stack
    cards:
      #=== Cycle 1 Title
      - !include
        - includes/titles.yaml
        - title: MORNING CYCLE SCHEDULE

This gives an error

# lovelace_gen
title: Garden
cards:
  - type: horizontal-stack
    cards:
      #=== Cycle 1 Title
      - !include
        - includes/titles.yaml
        - title: {{ states('input_text.irrigation_cycle1_name') }}

image

image

Ah, yes.

Templates generally require quoting when they’re along on a line, because { } is a json (and thus yaml) object.

Try title: "{{ states('input_test.irrigation_cycle1_name') }}".

Yes, I already tried that but it gives the same error.

      #=== Cycle 1 Title
      - !include
        - includes/titles.yaml
        - title: "{{ states('input_text.irrigation_cycle1_name') }}"

image

I also tried

      #=== Cycle 1 Title
      - !include
        - includes/titles.yaml
        - title: >
            {{ states('input_text.irrigation_cycle1_name') }}

Did you check your hass log file?

Ahh
 no, but I have now

It seems to be objecting to the states


Error handling message: Unknown error
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 20, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/lovelace/__init__.py", line 187, in send_with_error_handling
    result = await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/lovelace/__init__.py", line 212, in websocket_lovelace_config
    return await hass.data[DOMAIN].async_load(msg["force"])
  File "/usr/src/homeassistant/homeassistant/components/lovelace/__init__.py", line 145, in async_load
    self._load_config, force
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/lovelace/__init__.py", line 164, in _load_config
    config = load_yaml(fname)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 62, in load_yaml
    return yaml.load(conf_file, Loader=SafeLineLoader) or OrderedDict()
  File "/usr/local/lib/python3.7/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 43, in get_single_data
    return self.construct_document(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 47, in construct_document
    data = self.construct_object(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 196, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 147, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 227, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 399, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in construct_sequence
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in <listcomp>
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/config/custom_components/lovelace_gen/__init__.py", line 54, in _include_yaml
    return loader._add_reference(load_yaml(fname, args), ldr, node)
  File "/config/custom_components/lovelace_gen/__init__.py", line 37, in load_yaml
    return loader.yaml.load(config_file, Loader=loader.SafeLineLoader) or OrderedDict()
  File "/usr/local/lib/python3.7/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 43, in get_single_data
    return self.construct_document(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 47, in construct_document
    data = self.construct_object(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 196, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 147, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 227, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 399, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in construct_sequence
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in <listcomp>
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 196, in _ordered_dict
    nodes = loader.construct_pairs(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 147, in construct_pairs
    value = self.construct_object(value_node, deep=deep)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/usr/src/homeassistant/homeassistant/util/yaml/loader.py", line 227, in _construct_seq
    (obj,) = loader.construct_yaml_seq(node)
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 399, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in construct_sequence
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 122, in <listcomp>
    for child in node.value]
  File "/usr/local/lib/python3.7/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/config/custom_components/lovelace_gen/__init__.py", line 54, in _include_yaml
    return loader._add_reference(load_yaml(fname, args), ldr, node)
  File "/config/custom_components/lovelace_gen/__init__.py", line 32, in load_yaml
    stream = io.StringIO(jinja.get_template(fname).render({**args, "_global": llgen_config}))
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/config/lovelace/includes/irrigation/zone_duration_line.yaml", line 27, in top-level template code
    name: "{{ states('input_text.irrigation_zone1_name') }}"
jinja2.exceptions.UndefinedError: 'states' is undefined

Oh my! How did I miss that for so long?

Lovelace_gen regenerates your static lovelace configuration when you ask it to rebuild. It cannot be used to update the design dynamically when you change a value.
As such, you can’t use states, because that just wouldn’t make sense.

Yes, ok.
But I was hoping to use it to apply user created friendly names to entities.

e.g. The underlying entity names are
input_number.zone1_duration, input_number.zone2_duration... etc

I have corresponding input_text values which I was hoping to use as the name: so allowing the UI to show a user created name.

Is there any way to do this if I don’t need them to dynamically update?

EDIT: The title example I have been using in this post was just a simple example to prove the concept (or not!)

Hi thomas,
I started using lovelace_gen, and I think it’s great!

I have one problem with the default functionality: I cannot make it work


I’ve tried to setup both string and int variable with the default instruction, but it just won’t work.
Do you have any suggestion?
Any of these below worked

icon: {{ icon|default(’“mdi:lightbulb”’) }}
icon: {{ icon|default(‘mdi:lightbulb’) }}
vertical_size: {{ vertical_size|default(‘1’) }}

Hi all,

I’m using the !file as described in the documentation. It works well and it got rid of all cache related issues since I’ve been using it. However, there seem to be a side effect: errors pop in chrome’s console.

image
(1 error for each !file statement. Since I use this for everthing in my lovelace resources, I get 30 errors at each reload).

It’s a bit annoying because it makes debugging errors less noticable.
-> For those who use the !file command, are you facing the same issue ? Have you foudn a way to get rid of this ?

I’m slowly rolling out fixes for that as I update my cards. It’s annoying indeed, but can be safely ignored.

Thanks for your answer, that is not a big deal compared to what your plugins offer. Cheers :slight_smile: !

@thomasloven Hi Thomas, I came across your add-on and I’m now doing a big overhaul of my Lovelace files. This works pretty great so far, thanks a lot for making this!

Question: is there any possibility to define macros for using in multiple views, to have some sort of global templates? I tried defining macros in the ui-lovelace.yaml, but that didn’t work out (I couldn’t use the macros in the views). It seems that the !include statement does not pass variables from the parent file.

I read in the Jinja manual that you should use the import statement to use externally defined macros in the local scope. `my attempts resulted in the following error:

  File "/srv/homeassistant/lib/python3.7/site-packages/jinja2/loaders.py", line 199, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: my_templating_file.yaml

Any thoughts on this?

Unfortunately that’s not quite possible. Allowing macros in that way requires keeping track of the jinja context in a way that would require too much changing of the Home Assistant functionality to guarantee stable operation. Believe me, I’ve tried.

The best solution I can offer is to include a file with arguments: https://github.com/thomasloven/hass-lovelace_gen#passing-arguments-to-included-files

I accomplished what I wanted (more or less) using the include arguments now, works really good!
Thanks for the tip!

could you post an example?

Here’s an example from my own configuration:

This code:

Includes the same floorplan twice, once with only lights, and once with only things that are not lights.
In another view, I have the same floorplan with everything included.

Floorplan code here:

1 Like

Hi Thomas.
The !file facility causes errors in 0.107.x (with resources moved from ui-lovelace.yaml to configuration.yaml for dashboards). I created an issue in the repository.

Hi there,

I’m having a hard time getting a card working where it generates the card from a list of entities based on what selected in an input_select.
My VSCode addon gives me this:

Anny suggestions?

Several:

  • Don’t post pictures of code. Especially with this much noise. 4/5 of what’s shown in that picture has nothing to do with your problem, and thus it takes me five times as long time to find the problem as it should have to.
  • Strip the problem down to the bare essentials. Remove as much as possible, or - better yet - start from something very very simple and then expand from that. This will help you with the next suggestions.
  • Explain what you are trying to do and why you think your method is right
  • Explain what you expected to happen
  • Explain what happens instead
  • Explain what you have already tried in order to fix it and what happened then

The errors from vscode, btw, are probably because you are mixing jinja and yaml, and the plugin can’t handle that. It’s a very uncommon combination outside of hass.

Hi Thomas,

I totally agree on you points.
My initial thought was that the issue lied in the syntax/errors i could see in the VS Code.
As these red line/red comments was not copyable i had to do a screenshot. Again I agree, never use screenshot for code.

But since the issue is not the red lines i will try to explain which goal i trying to achieve.

I would like a card on my lovelace, where i have a drop down meny where i can select one of my room, and the it should show only what entities i have in that room.
The example i’m following is from a guy who put his configuration on github, but he’s unable to help be because of family related thing.

Maybe there a smarter way to do this`?