Yaml anchors: what is wrong?

All this trouble that you’re going through… you should just use lovelace_gen and be done with it.

1 Like

Started googling about it, thank you!

I use lovelace gen to create my entire interface. No reused code.

Roughly 4000 lines of code that generates up to 300K lovelace lines.

Let me explain you my original need:

  1. There is a decluttering template TEMPLATE_1 with some card, let it be “Entities card” for example.
  2. The card contains a list of entities - let it be ONE entity:
entities:
  - entity: sensor.XXXXXXXX
    name: ...
    .....
  1. Now I want to create one more template TEMPLATE_2 - which contains TWO entities:
entities:
  - entity: sensor.XXXXXXXX
    name: ...
    .....
  - entity: sensor.YYYYYYYY
    name: ...
    .....
  1. It is what I wanted to do:
template_1:
  card:
    type: entities
    entities: &entities_list
      - entity: XXXX
        ...

template_2:
  card:
    type: entities
    entities:
      - <<: *entities_list
      - entity: YYYY
        ...

And now I realized that it is not possible…

You can do this with lovelace_gen and Jinja

BTW - please, look at this:

type: vertical-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: binary_sensor.updater
  - type: entities
    entities: *ref_0
  - type: entities
    entities:
      - <<: *ref_0
      - sun.sun

It works:


I just added "entity: " word.

this works because it’s a dictionary and the <<: is merging the dictionary into the single listed item. It wouldn’t work if you were to add more listed items into ref_0

Yes, there is something not working here:
added one more entity to the 1st list:

type: vertical-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: binary_sensor.updater
        name: Updater-1
      - entity: binary_sensor.updater
        name: Updater-2
  - type: entities
    entities: *ref_0
  - type: entities
    entities:
      - <<: *ref_0
      - entity: sun.sun
        name: Sun-1

image

Yah, I’m telling you… there’s no way to merge lists with anchors. Only dictionaries.

list:

mystuff:
- item1
- item2

dictionary:

mystuff:
  item1: a
  item2: b
  item3: c

list of dictionaries

mystuff:
- item1: a
  item2: b
  item3: c
- item1: a
  item2: b
  item3: c

You can only merge with dictionary. Not list or list of dictionaries.

So far I am not getting…

  1. This works:
    list of dicts (2 dicts in the list) copied into another list of dicts:
type: vertical-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: binary_sensor.updater
        name: Updater-1
      - entity: binary_sensor.updater
        name: Updater-2
  - type: entities
    entities: *ref_0
  1. This works:
    list of dicts (1 dict in the list) merged with another list of dicts:
type: vertical-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: binary_sensor.updater
        name: Updater-1
  - type: entities
    entities:
      - <<: *ref_0
      - entity: sun.sun
        name: Sun-1
  1. This does not work:
    list of dicts (2 dicts in the list) merged with another list of dicts - and only the 1st dict is merged:
type: vertical-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: binary_sensor.updater
        name: Updater-1
      - entity: binary_sensor.updater
        name: Updater-2
  - type: entities
    entities:
      - <<: *ref_0
      - entity: sun.sun
        name: Sun-1

Yaml naturally takes a single item list and treats it like the list doesn’t exist. So you’re actually just dealing with a single dictionary when you create &ref_0

You should check out the yaml anchor rules. There’s plenty of them.

I thought that when this dict:

- entity: aaa
  name: bbb

is merged with this dict:

- entity: ccc
  name: ddd

it must be:

- entity: aaa
  name: bbb
  entity: ccc
  name: ddd

and must cause an error, but instead it creates this:

- entity: aaa
  name: bbb
- entity: ccc
  name: ddd

ad it works…

No, because you have the list indicators…

Could you give me a good doc for this?
So far I only read these:
http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/
http://blogs.perl.org/users/tinita/2019/05/reusing-data-with-yaml-anchors-aliases-and-merge-keys.html

There really isn’t a good single resource, just google yaml anchors and click through links.

But be warned, you need a firm understanding of yaml

To clarify further with your example. Your configuration is this:

entities: &entities
- entity: aaa
  name: bbb

So yaml only sees this in the anchor *entities:

entity: aaa
name: bbb

if you were to merge them the way they are supposed to be merged, it would look like this:

entities:
- <<: *entities
  entity: ccc
  name: ddd

which WILL produce errors because this is not valid:

- entity: aaa
  name: bbb
  entity: ccc
  name: ddd

But you are not doing that, you’re providing this as your configuration (which is valid)

entities:
- <<: *entities
- entity: ccc
  name: ddd

Petro, thank you very much for explanations!

I found a way to add a new entity to the list (my original task):

type: vertical-stack
cards:
  - &my_card
    type: entities 
    title: Some title
    state_color: true
    entities: &ref_1
      - entity: binary_sensor.updater
        name: Updater-1
    style: |
      ha-card {
        background: green;
      }

  - <<: *my_card
    entities: &ref_2
      - entity: binary_sensor.updater
        name: Updater-2
      - <<: *ref_1

  - <<: *my_card
    entities: &ref_3
      - entity: binary_sensor.updater
        name: Updater-3
      - <<: *ref_2
      - <<: *ref_1

  - <<: *my_card
    entities: &ref_4
      - entity: binary_sensor.updater
        name: Updater-4
      - <<: *ref_3
      - <<: *ref_2
      - <<: *ref_1


Each new card inherits the card’s properties (like title, style) & entities.
But every new entity must be added to the top of the list - that is why the order is reverse (4,3,2,1).

I think this is more elegant:

type: vertical-stack
cards:
  - &my_card
    type: entities 
    title: Some title
    state_color: true
    entities: 
      - &ref_1
        entity: binary_sensor.updater
        name: Updater-1
    style: |
      ha-card {
        background: green;
      }

  - <<: *my_card
    entities: 
      - *ref_1
      - &ref_2
        entity: binary_sensor.updater
        name: Updater-2

  - <<: *my_card
    entities:
      - *ref_1
      - *ref_2
      - &ref_3
        entity: binary_sensor.updater
        name: Updater-3

  - <<: *my_card
    entities: 
      - *ref_1
      - *ref_2
      - *ref_3
      - &ref_4
        entity: binary_sensor.updater
        name: Updater-4