Yaml anchors: what is wrong?

Started learning yaml anchors.

Here is a code which works:

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

But this code causes an error:

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

What am I doing wrong?

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

EDIT: try it out, I might have it wrong. It’s been a while and I’m going off memory. If that doesn’t work then

  - type: entities
    entities: *ref_0

should work.

No, does not work.

It works of course, but I wanted to merge with new entities: old list + new items

You can’t merge lists, only dicts

Oh, very bad…
Thanks a lot for a quick reply!

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

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…