Simple Example of using YAML Node Anchors in Home Assistant

Simple Example of using YAML Node Anchors in Home Assistant 1

I’ve seen the repos with all the fancy Node anchors in the YAML scripts and honestly, I could never really figure out how to use them right. I also couldn’t really find a good use case in my Smart Home to use this DRY (Don’t Repeat Yourself) method. Today, I jumped both those hurdles.

Here is a REALLY simplistic example using a snippet of my scene file using Node Anchors (Crawl before walking right?) [First code block without and second WITH]

This is without ANCHORS

The lines we are targeting are the ‘rgb_color’ lines. Prior to using the anchors, I had RGB values which didn’t really mean much to me but in the second example, using anchors, I know have much more readable code.

The first instance of the key [rgb_color] is where you define the label (using &) and then the value after ward. So &red is the label and [255.0.0] is the value.

Once the label is defined, you can then use it afterward in the file using *red. When Home Assistant sees the alias, it will just swap out the value from the label.

In our case, rgb_color: *red becomes rgb_color: [255.0.0].

It’s a really simplistic example but perfect for my use case where I don’t want to comment what each RGB code translates to. If I need to change the color slightly, I just need to change the one value. All the rest of the aliases will reference that changed value.

If you would like to watch me do this on my system, check out the video below:

Happy Anchoring!