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: content/components/mapping.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,8 @@ description: "Mapping Component"
3
3
title: "Mapping Component"
4
4
---
5
5
6
-
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to values. This enables e.g. mapping a string to a number or vice versa, or mapping a string such as a weather condition to an image.
6
+
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to
7
+
values. This enables e.g. mapping a string to a number or vice versa, or mapping a string such as a weather condition to an image.
7
8
8
9
```yaml
9
10
# Example configuration entry
@@ -43,9 +44,14 @@ You can also map to a class. This is useful when you want to map to a more compl
43
44
- `color` : Maps to a predefined [Color](#config-color). The values should each be a color ID.
44
45
- The name of a C++ class defined by ESPHome, e.g. `Component`. The values should each be a ID of that class.
45
46
47
+
46
48
## Using a mapping
47
49
48
-
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using the `id` function, and the value can be looked up using the `[]` operator as per the above example.
50
+
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using
51
+
the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example, or the ``get`` function.
52
+
A map may be updated at run time using a lambda call, e.g. ``map.set("key", value)``.
53
+
54
+
Maps are stored in RAM, but will use PSRAM if available.
49
55
50
56
A more complex example follows:
51
57
@@ -84,8 +90,14 @@ display:
84
90
- platform: ...
85
91
# update the display drawing random text in random colors
86
92
lambda: |-
87
-
auto color = color_map[random_uint32() % 3];
93
+
auto color = color_map.get(random_uint32() % 3]); # Uses get() to index the color_map
0 commit comments