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?
petro
(Petro)
April 21, 2021, 2:34pm
2
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.
petro:
entities: *ref_0
It works of course, but I wanted to merge with new entities: old list + new items
petro
(Petro)
April 21, 2021, 2:38pm
4
You can’t merge lists, only dicts
Oh, very bad…
Thanks a lot for a quick reply!
petro
(Petro)
April 21, 2021, 2:40pm
6
All this trouble that you’re going through… you should just use lovelace_gen and be done with it.
petro:
lovelace_gen
Started googling about it, thank you!
petro
(Petro)
April 21, 2021, 2:42pm
8
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:
There is a decluttering template TEMPLATE_1 with some card, let it be “Entities card” for example.
The card contains a list of entities - let it be ONE entity:
entities:
- entity: sensor.XXXXXXXX
name: ...
.....
Now I want to create one more template TEMPLATE_2 - which contains TWO entities:
entities:
- entity: sensor.XXXXXXXX
name: ...
.....
- entity: sensor.YYYYYYYY
name: ...
.....
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…
petro
(Petro)
April 21, 2021, 2:56pm
11
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.
petro
(Petro)
April 21, 2021, 3:16pm
13
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
petro
(Petro)
April 21, 2021, 3:22pm
15
Yah, I’m telling you… there’s no way to merge lists with anchors. Only dictionaries.
petro
(Petro)
April 21, 2021, 3:24pm
16
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…
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
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
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
petro
(Petro)
April 21, 2021, 3:33pm
18
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…
petro
(Petro)
April 21, 2021, 3:37pm
20
No, because you have the list indicators…