Packages vs insertion operator

Currently a lot of my device configs are copies of each other. I’m looking to extract common configuration for particular devices into separate files that are then used by the device config files.

There seem to be two ways:

With packages:

packages:
  foo: !include <file>

( I don’t know what foo represents here by the way.)

With the YAML insertion operator:

<<: !include <file>

Are there differences between the two that I should know about? What are you using in practice?

I tend to use the packages: way as it will merge duplicated blocks from included files together. For instance if you have multiple sensor: blocks in your package included files and in the base yaml file they will all get combined. This can be done with the insertion operator but included files get added inline to the base yaml file. So if the base yaml file has a sensor: block, the insertion operator adding a file with more sensors would need to be in this block and the included file could only have sensors to get added. Using packages with the !include substitutions becomes very handy.

Oooh, I had actually tested if I could have sensors in an include file and in the including file and since I got no error message I assumed that it works. Prompted by your response I checked the combined output file and found out that the sensors in the include file are silently discarded.

Packages it is then.

By the way, can I somehow flag the package files so they aren’t treated as devices (shown as “OFFLINE”) in the dashboard?

Yeah. Name the file starting with a period or make a subdirectory.

Tip

To hide these base files from the dashboard, you can

  • Place them in a subdirectory (dashboard only shows files in top-level directory)
  • Prepend a dot to the filename, like .base.yaml

I read that but the way I understood it, that makes them disappear completely and I cannot edit them any more.

Good point. They don’t show up in the dashboard to use the ESPhome editor. They can be opened using the web based file editor add-on but you loose some of the error checking.

This might be a good feature request to submit. Add option for dashboard to show hidden files or add option for editor to open up files referenced by an !include.

I know it’s a year later, but I just wanted to chime in to say that I’ve been using VS Code for a while, now, and am happy with it. It sees all files, including dotted and in subdirectories.
I can have multiple files (i.e. packages and includes) open at the same time, search across multiple files, and more. It also has, imo better error checking, and auto-completion. There’s more, but I’m not here to ‘sell’ VSC (I don’t hold MSFT stock, lol)

As an aside I had started by naming my ‘included’ files with a leading period, but now place them in a subdirectory named “common”. Seems cleaner, to me.

When using packages You can pass parameters to package, like a pin number f.e. or something else.