Skip to content

Commit 0085830

Browse files
committed
document include filename with substitutions
1 parent eb025f7 commit 0085830

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

content/components/packages.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ switch:
168168
# ...
169169
```
170170

171+
It is also possible to use substitutions/Jinja in include filenames, allowing to dynamically select what file is loaded:
172+
173+
```yaml
174+
substitutions:
175+
platform: esp32
176+
177+
packages:
178+
hardware: !include device-${platform}.yaml
179+
180+
light:
181+
- platform: status_led
182+
name: "Board Status"
183+
pin: LED_GPIO
184+
```
185+
186+
```yaml
187+
# device-esp32.yaml
188+
substitutions:
189+
LED_GPIO: 2
190+
esp32:
191+
board: esp32dev
192+
```
193+
171194
{{< anchor "config-packages_extend" >}}
172195

173196
## Extend

content/components/substitutions.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ In the above example, the value of the `text` property will be, literally, `This
169169

170170
## Substitute !include variables
171171

172-
ESPHome's `!include` accepts a list of variables that can be substituted within the included file.
172+
ESPHome's `!include` accepts a list of variables that can be substituted within the included file. The file path may itself be constructed out of any expression that evaluates to a valid path.
173173

174174
```yaml
175175
binary_sensor:
@@ -180,14 +180,13 @@ binary_sensor:
180180
- platform: gpio
181181
id: button2
182182
pin: GPIOXX
183-
on_multi_click: !include
184-
# multi-line syntax
183+
on_multi_click: !include # multi-line syntax
185184
file: on-multi-click.yaml
186185
vars:
187186
id: 2
188187
```
189188

190-
`on-multi-click.yaml` :
189+
`on-multi-click.yaml` :
191190

192191
```yaml
193192
- timing: !include click-single.yaml
@@ -202,6 +201,22 @@ binary_sensor:
202201
payload: double
203202
```
204203

204+
You can also use substitutions to build the path of the file to be loaded:
205+
206+
```yaml
207+
substitutions:
208+
platform: esp32
209+
210+
binary_sensor:
211+
- platform: gpio
212+
id: button2
213+
pin: GPIOXX
214+
on_multi_click: !include
215+
file: on-multi-click-${platform}.yaml
216+
vars:
217+
id: 2
218+
```
219+
205220
{{< anchor "command-line-substitutions" >}}
206221

207222
## Command line substitutions
@@ -253,12 +268,12 @@ esphome:
253268
# ...
254269
255270
sensor:
256-
- platform: dht
257-
# ...
258-
temperature:
259-
name: Temperature
260-
humidity:
261-
name: Humidity
271+
- platform: dht
272+
# ...
273+
temperature:
274+
name: Temperature
275+
humidity:
276+
name: Humidity
262277
```
263278

264279
```yaml
@@ -269,6 +284,8 @@ substitutions:
269284
<<: !include common.yaml
270285
```
271286

287+
NOTE: using substitutions in the `!include` filename of a YAML insertion operator is not supported yet.
288+
272289
> [!TIP]
273290
> To hide these base files from the dashboard, you can
274291
>

0 commit comments

Comments
 (0)