The flatten function provides an easy way to flatten a list of lists into a single list.
Template function properties | |
---|---|
Function | Flatten a lists of lists |
Function name | flatten |
Returns | A flattened list |
Return type | list of items |
Can be used as a filter | Yes |
Can be used as a test | No |
Spook's influence | Newly added template function |
Developer tools | Try this in the template developer tools |
Signature |
---|
|
Function parameters | |||
---|---|---|---|
Attribute | Type | Required | Default / Example |
items | list of items | Yes | ["a", ["b", ["c"]]] |
levels | None, integer | No | None |
The levels
parameter can be used to specify how many levels of lists should be
flattened. By default, all levels are flattened.
ExamplesΒΆ
Using flatten as a functionΒΆ
1
{{ flatten([1, [2, [3]], 4, [5 , 6]]) }}
Returns:
[1, 2, 3, 4, 5, 6]
Using flatten as a filterΒΆ
1
{{ [1, [2, [3]], 4, [5 , 6]] | flatten }}
Returns:
[1, 2, 3, 4, 5, 6]
Using a levelsΒΆ
You can define how many levels of lists should be flattened. By default, if no levels are specified, all levels are flattened.
Example:
1
{{ flatten([1, [2, [3]]], levels=1) }}
Returns:
[1, 2, [3]]
Features requests, ideas, and supportΒΆ
If you have an idea on how to further enhance the Home Assistant template engine, for example, by adding a new template function; feel free to let us know in our discussion forums.
Are you stuck using this new feature? Or maybe youβve run into a bug? Please check the Support page on where to go for help.