The shuffle function provides an easy way to randomly shuffle any list of items. Either fully random, or with a seed to make the randomization reproducable.
Template function properties | |
---|---|
Function | Randomly shuffles a list of items |
Function name | shuffle |
Returns | The shuffled list |
Return type | list of items |
Can be used as a filter | Yes |
Can be used as a test | Yes |
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"] |
seed | None, integer, float, string | No | None |
The items
parameter can be a list of anything. The items in the list will be randomly shuffled and returned.
ExamplesΒΆ
Using shuffle as a functionΒΆ
1
{{ shuffle([1, 2, 3]) }}
Returns:
[2, 3, 1]
Calling the function above multiple times will always return the list in a different random order.
Using shuffle as a filterΒΆ
1
{{ [1, 2, 3] | shuffle }}
Returns:
[3, 2, 1]
Using a seedΒΆ
A seed can be used to initialize the random number generator. The same seed will always result in the same randomization, like a randomization with a memory. This is useful if you want to have more reproducable control over the randomization.
Example:
1
{{ shuffle([1, 2, 3], seed=1) }}
Returns:
[2, 3, 1]
Calling the function above multiple times will always return the list in the same random order.
The seed can, of course, also be used when using shuffle as a filter:
1
{{ [1, 2, 3] | shuffle(seed=1) }}
Returns:
[2, 3, 1]
Shuffle anythingΒΆ
The examples above use a list of numbers that get shuffled, but shuffle can be used on any list of items.
1
{{ shuffle(["Not", "your", "homie", 1, 2, 3]) }}
Returns:
["homie", 2, "Not", 3, 1, "your"]
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.