You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/substitutions.rst
+104-1Lines changed: 104 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,19 @@ validating your configuration, ESPHome will automatically replace all occurrence
27
27
by their value. The syntax for a substitution is based on bash and is case-sensitive: ``$substitution_key`` or
28
28
``${substitution_key}`` (same).
29
29
30
-
Two substitution passes are performed allowing compound replacements.
30
+
Substitution variables can be of any valid YAML type, for example:
31
+
32
+
.. code-block:: yaml
33
+
34
+
substitutions:
35
+
device:
36
+
name: Kitchen AC
37
+
port: 12
38
+
enabled: true
39
+
color: "yellow"
40
+
unused_pins: [12, 23, 27]
41
+
42
+
Two substitution passes are performed allowing compound replacements:
31
43
32
44
.. code-block:: yaml
33
45
@@ -39,6 +51,97 @@ Two substitution passes are performed allowing compound replacements.
39
51
something:
40
52
test: ${bar_${foo}_value}
41
53
54
+
The above is supported for backward compatibility. It is recommended that
55
+
you use key-value dictionaries going forward:
56
+
57
+
.. code-block:: yaml
58
+
59
+
substitutions:
60
+
foo: yellow
61
+
bar:
62
+
yellow: !secret yellow_secret
63
+
green: !secret green_secret
64
+
65
+
something:
66
+
test: ${bar[foo]}
67
+
68
+
.. _jinja-expressions:
69
+
70
+
Jinja expressions
71
+
-----------------
72
+
73
+
Simple Jinja expressions and filters can be used inside ``${ ... }`` syntax.
74
+
75
+
All substitution variables become accessible within Jinja expressions by their name.
76
+
77
+
If the substitution variable is a key-value dictionary, you can access members with a dot notation: ``${ device.name }``, or indexed ``${ device["name"] }``
78
+
79
+
Lists can be indexed: ``${ unused_pins[2] }``
80
+
81
+
.. code-block:: yaml
82
+
83
+
substitutions:
84
+
native_width: 480
85
+
native_height: 320
86
+
high_dpi: true
87
+
scale: 1.5
88
+
sensor_pin:
89
+
number: 3
90
+
inverted: true
91
+
debug_label:
92
+
width: 200
93
+
height: 20
94
+
enabled: true
95
+
96
+
display:
97
+
- platform: ili9xxx
98
+
dimensions:
99
+
width: ${native_width * 2 if high_dpi else native_width}
100
+
height: ${native_height * 2 if high_dpi else native_height}
101
+
102
+
lvgl:
103
+
widgets:
104
+
- label:
105
+
id: debug_info
106
+
hidden: ${not debug_label.enabled}
107
+
width: ${ (debug_label.width * scale) | round | int }
108
+
height: ${ (debug_label.height * scale) | round | int }
109
+
text: |
110
+
High DPI is ${high_dpi and "enabled" or "disabled"}.
111
+
112
+
binary_sensor:
113
+
- platform: gpio
114
+
name: Binary sensor on pin ${sensor_pin.number}
115
+
pin: ${sensor_pin}
116
+
117
+
Note that in other projects Jinja uses the ``{{ ... }}`` syntax for expression delimiters.
118
+
In ESPHome we have configured Jinja to use ``${...}`` instead, so it is the same as the
119
+
existing substitution syntax and to avoid conflicts with Home Assistant's own use of Jinja.
120
+
121
+
To understand what types of expressions and filters can be used,
122
+
refer to `Jinja Expressions <https://jinja.palletsprojects.com/en/stable/templates/#expressions>`_ documentation.
123
+
124
+
Mathematical operations
125
+
^^^^^^^^^^^^^^^^^^^^^^^
126
+
127
+
In addition to Jinja's native operators such as ``+``, ``-``, ``*``, ``/``, ... Python's math
128
+
library is exposed as a module:
129
+
130
+
.. code-block:: yaml
131
+
132
+
substitutions:
133
+
x: 20
134
+
y: 50
135
+
lvgl:
136
+
widgets:
137
+
- label:
138
+
x: $x
139
+
y: $y
140
+
text: Distance is ${math.sqrt(x*x+y*y)}.
141
+
142
+
To see what mathematical functions ara available,
143
+
refer to `Python math library <https://docs.python.org/3/library/math.html>`_ documentation.
0 commit comments