The third identical ESPHome device is where copy-paste starts to hurt. Five temperature sensors around the house share the same board, Wi-Fi block, API settings, logger and uptime sensor. Only the name and maybe a calibration offset differ. Then you change the Wi-Fi power-save mode or the update interval and have to edit five files, and one of them always gets missed.
ESPHome has two built-in features for this. Substitutions are variables you can use anywhere in the YAML. Packages pull other YAML files into a device config and merge them. Together they let each device file shrink to the few lines that are actually unique.
Substitutions: variables in YAML
A substitution is defined under the top-level substitutions: key and used as $name or ${name}. ESPHome replaces them before it validates the config.
substitutions:
name: kitchen-climate
friendly_name: Kitchen climate
temp_offset: "-0.8"
esphome:
name: ${name}
friendly_name: ${friendly_name}
sensor:
- platform: bme280_i2c
temperature:
name: Temperature
filters:
- offset: ${temp_offset}
Values can be any YAML type, including dictionaries and lists, and you can reach into them with ${device.name} or ${unused_pins[2]}. Simple Jinja expressions also work inside ${ ... }, for example ${native_width * 2 if high_dpi else native_width}. ESPHome uses ${...} rather than Jinja's usual {{ ... }} to avoid clashing with Home Assistant templates. If you need a literal ${...} in a string, prefix the value with !literal.
A substitution value can itself be a secret, which is how you keep per-device keys out of shared files:
substitutions:
api_key: !secret kitchen_climate_api_key
Packages: split the config into files
Packages are listed under packages: and usually loaded with !include. Here is a layout that works well with the ESPHome dashboard:
esphome/
secrets.yaml
kitchen-climate.yaml
bedroom-climate.yaml
common/
base.yaml
climate-sensor.yaml
The dashboard only lists files in the top-level directory, so anything in common/ stays out of the device list. Prefixing a file name with a dot (.base.yaml) hides it as well.
common/base.yaml holds what every device shares:
esphome:
name: ${name}
friendly_name: ${friendly_name}
esp32:
board: esp32dev
logger:
api:
encryption:
key: ${api_key}
ota:
- platform: esphome
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
sensor:
- platform: uptime
id: uptime_sensor
name: Uptime
update_interval: 5min
A device file then looks like this:
substitutions:
name: kitchen-climate
friendly_name: Kitchen climate
api_key: !secret kitchen_climate_api_key
temp_offset: "-0.8"
packages:
base: !include common/base.yaml
climate: !include common/climate-sensor.yaml
packages: accepts either a list or a mapping. With a mapping, the keys (base, climate) are labels for you; ESPHome ignores them.
How the merge works
This is the part that decides whether packages behave the way you expect. According to the documentation, package contents are merged into the main config without destroying it, and the rules are:
- Dictionaries are merged key by key. If the package defines
api:withencryption:and your device file addsapi:withactions:, you end up with both. - Lists of components are merged by ID, if an ID is set.
- Other lists are concatenated.
- All other values are replaced by the later one. Values in the main configuration take precedence over package values.
- Substitutions in the main file override substitutions with the same name in a package.
The second and third rules cause most of the surprises. If the package has a sensor without an id and you add "the same" sensor in the device file, you get two sensors. To change one item from a package, the item needs an id in the package, and the device file should point at it with !extend:
packages:
base: !include common/base.yaml
sensor:
- id: !extend uptime_sensor
update_interval: 10s
!remove works the same way in the other direction. It can drop a whole list entry (- id: !remove uptime_sensor), a whole component (captive_portal: !remove), or a single option inside an extended entry (update_interval: !remove). Give every component in a shared package an id, even if you do not need one today. It costs one line and makes it possible to adjust that component per device later.
Packages as templates with vars
!include can pass variables to the file it loads. Inside that file they behave like substitutions and take priority over global substitutions with the same name. A defaults: block in the package supplies values for any variable the caller leaves out. This is how you repeat a block several times on one device.
The documentation uses three garage doors on one board as its example. The same idea fits a board with several relay channels:
# common/relay-channel.yaml
defaults:
channel_name: Unnamed
restore: RESTORE_DEFAULT_OFF
switch:
- platform: gpio
id: relay_${channel_id}
name: ${channel_name}
pin: ${pin}
restore_mode: ${restore}
# garage-relays.yaml
packages:
base: !include common/base.yaml
ch1: !include
file: common/relay-channel.yaml
vars:
channel_id: 1
channel_name: Garage light
pin: GPIO26
ch2: !include
file: common/relay-channel.yaml
vars:
channel_id: 2
channel_name: Garden pump
pin: GPIO27
Each channel gets its own id, so the two switch: lists do not collide, and you can still !extend relay_2 in the device file. Pin numbers and channel IDs here are just example values; use the pins your board actually exposes.
An !include can also carry a condition:. ESPHome evaluates it after expanding substitutions and loads the file only if it is true; otherwise it includes an empty dictionary. Substitutions are also expanded in the file name itself, so !include hardware-${platform}.yaml picks a hardware file from one variable. The documentation notes that file-name substitutions do not work with the YAML merge key (<<: !include ...).
Remote packages from Git
Packages can also come from a Git repository. This suits anyone who keeps a shared library of configs in one place, and many ready-made ESPHome project configs pull their base files the same way.
packages:
shared:
url: https://github.com/your-user/esphome-common
files: [base.yaml, climate-sensor.yaml]
ref: v1.2.0
refresh: 1d
There is also a shorthand, github://user/repo/path/file.yaml@ref, and the same form works for gitlab:// and codeberg://. files: entries can carry their own vars:, and the same file can be listed several times with different values. For private repositories there are username: and password: options.
Two limits matter in practice:
- Remote packages cannot use
!secret. The documentation's advice is to use substitutions in the remote file, ideally with a default, and set them in the local device file from local secrets. That is whyapi_keyin the example above is a substitution rather than a secret insidebase.yaml. ref:decides what you build. Without it you get the repository's default branch, andrefresh:sets how often ESPHome pulls changes. Pointingref:at a tag means a push to the shared repo does not quietly change the next firmware you compile.
See the merged result before you flash
When a device behaves differently from what its YAML seems to say, look at the merged config rather than the source files:
esphome config kitchen-climate.yaml
The config command validates the configuration and prints the result with all packages and substitutions applied. Secrets are redacted unless you add --show-secrets, and --no-defaults hides the values that validation fills in, which makes it easier to compare two devices. You can also override a substitution for one run with -s, for example esphome -s name test-node config generic.yaml. Command-line substitutions take precedence over the ones in the file.
Mistakes that come up often
- Duplicated sensors after an "override". The package entry had no
id, so the device file added a second one instead of changing the first. Add theidin the package and use!extend. - Two packages define the same
id. Lists merge by ID, so two different components with one ID end up merged into one entry. Prefix IDs in templates with a variable, as withrelay_${channel_id}. - A secret in a remote package. It fails, by design. Move it to a substitution.
- The device name hard-coded in
base.yaml. Every device then gets the same hostname. Keepname:as${name}and set it in each device file. - Forgetting that a change in a shared file affects every device. Editing
base.yamlchanges the next build of all devices that include it. Runesphome configon one of them first, and flash a test device before the rest.