• Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔹 Improve the lovelace yaml parser for Home Assistant

lovelace_gen

hacs_badge

Improve the lovelace yaml parser for Home Assistant.

See my floorplan card for an example of what's possible.

Installation instructions

  • Copy the contents of custom_components/lovelace_gen/ to <your config dir>/custom_components/lovelace_gen/.
  • Add the following to your configuration.yaml:
lovelace_gen:

lovelace:
  mode: yaml
  • Restart Home Assistant

Usage

This integration changes the way Home Assistant parses your ui_lovelace.yaml before sending the information off to the lovelace frontend in your browser. It's obviously only useful if you are using YAML mode.

First of all

To rerender the frontend, use the Refresh option from the three-dots-menu in Lovelace

refresh

Second of all

Any yaml file that is to be processed with lovelace_gen MUST have the following as its first line:

# lovelace_gen

Important: For some reason, which I can't seem to nail down, things stop working if you add # lovelace_gen to ui-lovelace.yaml. Adding it to any file included from ui-lovelace.yaml works, though.

Let's continue

The changes from the default generation include

Jinja2 templates

You can now use Jinja2 templates in your lovelace configuration.

This can be used e.g. to

  • Set and use variables
{% set my_lamp = "light.bed_light" %}

type: entities
entities:
 - {{ my_lamp }}
  • Loop over lists
{% set lights = ['light.bed_light', 'light.kitchen_lights', 'light.ceiling_lights'] %}

- type: entities
  entities:
  {% for l in lights %}
    - {{ l }}
  {% endfor %}

- type: horizontal-stack
  cards:
    {% for l in lights %}
    - type: light
      entity: {{ l }}
    {% endfor %}
  • Use macros
{% macro button(entity) -%}
  - type: entity-button
    entity: {{ entity }}
    tap_action:
      action: more-info
    hold_action:
      action: toggle
{%- endmacro %}

type: horizontal-stack
cards:
  {{ button("light.bed_light") }}
  {{ button("light.ceiling_lights") }}
  {{ button("light.kitchen_lights") }}

Please note that for this to work, the indentation of the code in the macro block must be equal to what it should be where it's called.

  • Add conditional parts
{% if myvariable == true %}
Do something
{% endif %}

This is NOT dynamic. The values of variables are locked down when you rerender the interface.

This might make conditions seem pointless... but they work well with the next feature.

Passing arguments to included files

Normally, you can include a file in your lovelace configuration using

view:
  - !include lovelace/my_view.yaml

lovelace_gen lets you add a second argument to that function. This second argument is a dictionary of variables and their values, that will be set for all jinja2 templates in the new file:

type: horizontal-stack
cards:
  - !include
    - button_card.yaml
    - entity: light.bed_light
  - !include
    - button_card.yaml
    - entity: switch.decorative_lights
  - !include
    - button_card.yaml
    - entity: light.ceiling_lights
      name: LIGHT!

button_card.yaml

# lovelace_gen
{% if entity.startswith("light") %}
type: light
{% else %}
type: entity-button
{% endif %}
entity: {{ entity }}
name: {{ name }}

include args

Be careful about the syntax here. Note that the arguments are given as a list and is indented under the !include statement. The second item in the list is a dictionary.

Note: If you want to pass a dictionary of values into a file, you need to convert it to json first:

{% set mydict = {"a": 1, "b": 2} %}
variable: {{ mydict | tojson }}

And then convert it back from json inside the file:

content: The value of a is {{ (variable | fromjson)['a'] }}

The fromjson filter is a feature of lovelace_gen and not normally included in jinja.

Invalidate cache of files

If you use lots of custom lovelace cards, chances are that you have run into caching problems at one point or another.

I.e. you refresh your page after updating the custom card, but nothing changes.

The answer is often to add a counter after the URL of your resource, e.g.

resources:
  - url: /local/card-mod.js?v=2
    type: module

lovelace_gen introduces a !file command that handles this for you.

resources:
  - url: !file /local/card-mod.js
    type: module

After this, lovelace_gen will automatically add a random version number to your URL every time you rerender the frontend. You won't have to worry about cache ever again.

This can also be used for pictures.

Example

ui_lovelace.yaml

# lovelace_gen
resources:
  # When you regenerate, the browser cache for this file will be invalidated
  - url: !file /local/card-mod.js
    type: module
...

views:
 - ! include lovelace/my_cool_view.yaml

lovelace/my_cool_view.yaml

# lovelace_gen
{% set my_lights = ["light.bed_light", "light.kitchen_lights", "light.ceiling_lights"] %}
title: My view
cards:
  - type: entities
    entities:
{% for light in my_lights %}
      - {{ light }}
{% endfor %}

  # Include files with arguments
  # NB: JSON format for arguments
  # And NO SPACE after the colons!
  - !include
    -floorplan.yaml
    - lamps: true
      title: With Lamps

# Use this if you want lovelace_gen to ignore the jinja
{% raw %}
  - type: markdown
    content: |
      # Coming soon(?)

      A built-inmarkdown card with jinja templating.
      So I can tell that my light is {{ states('light.bed_light') }}!
{% endraw %}

  - !include
    - floorplan.yaml
    - title: No lights

lovelace/floorplan.yaml

# lovelace_gen
{% macro lamp(entity, x, y) -%}
{% if lamps %}
- type: state-icon
  entity: {{ entity }}
{% else %}
- type: custom:gap-card
{% endif %}
  style:
    left: {{ x }}%
    top: {{ y }}%
{%- endmacro %}

type: picture-elements
title: {{ title }}
image: https://placekitten.com/800/600
elements:
  {{ lamp('light.bed_light', 25, 25) }}
  {{ lamp('light.kitchen_lights', 50, 25) }}
  {{ lamp('light.ceiling_lights', 50, 50) }}

lovelace_gen

Hidden bonus

With lovelace_gen installed, you'll be able to redefine node anchors in the same file. A feature in the YAML specification, but an error in the engine Home Assistant normally uses...

FAQ

How can I do global variables?

You can add variables to the lovelace_gen configuration in configuration.yaml and then refernce them in lovelace using {{ _global }}.

E.g.:

lovelace_gen:
  rooms:
    - living_room
    - kitchen
    - bed_room
type: entities
entities:
  {% for room in _global.rooms %}
  - type: custom:auto-entities
    card:
      type: custom:fold-entity-row
      head:
        type: section
        label: {{ room|capitalize }}
    filter:
      include:
        - area: {{ room }}
  {% endfor %}

Can I use this for my general Home Assistant configuration?

It's called lovelace_gen for a reason... That being said - it might work. Or it might not. There's really no way to tell. It depends on what parts of the configuration are loaded before or after lovelace_gen itself.

I'd advice you not to try it.

What if I WANT jinja in my lovelace interface

Use the {% raw %} and {% endraw %} tags. There's an example above.

Is there any way to solve the indentation problem for macros

Not automatically, but you can do something like

{% macro button(entity, ws) %}
{{"  "*ws}}- type: entity-button
{{"  "*ws}}  entity: {{ entity }}
{{"  "*ws}}  tap_action:
{{"  "*ws}}    action: more-info
{{"  "*ws}}  hold_action:
{{"  "*ws}}    action: toggle
{%- endmacro %}

  - type: horizontal-stack
    cards:
      {{ button('light.bed_light', 3) }}

  {{ button('light.bed_light', 1) }}

Note that included files don't have this problem.


Buy Me A Coffee

More Repositories

1

hass-browser_mod

🔹 A Home Assistant integration to turn your browser into a controllable entity and media player
TypeScript
1,314
star
2

lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
TypeScript
1,271
star
3

lovelace-card-mod

🔹 Add CSS styles to (almost) any lovelace card
TypeScript
1,143
star
4

lovelace-layout-card

🔹 Get more control over the placement of lovelace cards.
TypeScript
1,064
star
5

lovelace-slider-entity-row

🔹 Add sliders to entity cards
TypeScript
818
star
6

lovelace-fold-entity-row

🔹 A foldable row for entities card, containing other rows
TypeScript
595
star
7

lovelace-state-switch

🔹Dynamically replace lovelace cards depending on occasion
TypeScript
400
star
8

hass-fontawesome

🔹 Use icons from fontawesome in home-assistant
Python
284
star
9

lovelace-card-tools

🔹A collection of tools for other lovelace plugins to use
JavaScript
247
star
10

lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
TypeScript
221
star
11

hass-config

My Home Assistant configuration
Python
213
star
12

lovelace-more-info-card

🔹 Display the more-info dialog of any entity as a lovelace card
TypeScript
150
star
13

lovelace-hui-element

🔹 Use built-in elements in the wrong place
TypeScript
109
star
14

hass-favicon

🔹 Change the favicon of your Home Assistant instance
Python
104
star
15

lovelace-flower-card

JavaScript
102
star
16

lovelace-card-modder

JavaScript
101
star
17

lovelace-popup-card

JavaScript
94
star
18

hass-plejd

🔹 Plejd BLE integration for Home Assistant
Python
90
star
19

round-slider

JavaScript
85
star
20

lovelace-badge-card

🔹 Place badges anywhere in the lovelace layout
JavaScript
81
star
21

lovelace-toggle-lock-entity-row

JavaScript
62
star
22

lovelace-fullykiosk

Lovelace plugin for using Fully Kiosk Browser features in home-assistant
JavaScript
61
star
23

homeassistant-lovelace-gen

A lovelace configuration file generator for home assistant
Python
57
star
24

lovelace-player

Lets any browser currently viewing your lovelace interface act as an audio receiver with a media_player interface
JavaScript
50
star
25

mittos64

C
47
star
26

lovelace-theme-maker

JavaScript
42
star
27

lovelace-browser-commander

JavaScript
33
star
28

lovelace-gap-card

🔹 A lovelace card that does nothing and looks like nothing. Incredibly useful! No, really.
JavaScript
31
star
29

lovelace-useful-markdown-card

JavaScript
30
star
30

hass-custom-devcontainer

A devcontainer for developing and testing custom Home Assistant stuff
Shell
29
star
31

homeassistant-2020-presentation

18
star
32

lovelace-gui-sandbox

🔹 Lets you play around with the GUI editors even if you're using YAML mode
JavaScript
17
star
33

lovelace-markdown-mod

JavaScript
16
star
34

lovelace-color-picker

JavaScript
14
star
35

lovelace-card-loader

JavaScript
13
star
36

plejd2mqtt

JavaScript
12
star
37

lovelace-column-card

A columnizing card for lovelace ui for home-assistant.
JavaScript
12
star
38

card-tools

A collection of tools to help develop lovelace cards
JavaScript
11
star
39

os5

My attempts at developing an operating system kernel.
C
11
star
40

lovelace-dummy-entity-row

🔹 An entity row with only icon and name
JavaScript
10
star
41

lovelace-q-card

JavaScript
9
star
42

lovelace-long-press

JavaScript
9
star
43

lovelace-time-input-row

JavaScript
9
star
44

nextrevo-mod

How I use the E3D Revo system with Prusa Nextruder
8
star
45

dito

A library and some tools for handling disk image files.
C
8
star
46

mittsnap

rsnapshot-like tool that leverages btrfs snapshots
Shell
5
star
47

dotfiles

MATLAB
5
star
48

lovelace-color-glance-card

A glance card with a colorized background
JavaScript
3
star
49

lovelace-icon-headers

WIP
JavaScript
2
star
50

lovelace-wbah

Lovelace with bluejays and herons
TypeScript
2
star
51

pyplejd

Python
2
star
52

os4

C
1
star
53

new-hass-config

Python
1
star
54

z80Monitor

Assembly
1
star
55

vim-tstatus

My status line for vim
Vim Script
1
star
56

mittos64-old

C
1
star
57

hass-plant_helper

🔹 Home Assistant Helper for monitoring plant health using MiFlora devices
Python
1
star