Convert to set: set
The set template function converts a collection (like a list) into a set. A set is an unordered collection of unique values, so any duplicate items in the input are automatically removed. This gives you a quick way to deduplicate a list.
This is useful when you need to work with unique values only. For example, if you collect entity states from multiple sources and some appear more than once, converting to a set removes the duplicates. You can also use it to count the number of distinct values, or to prepare data for set operations like intersect, union, and difference. Note that sets are unordered, so the items may not be in the same order as the original list.
Usage
Here’s how to use this template function. Copy any example and adjust it to your setup.
{{ set([1, 2, 2, 3, 3, 3]) }}
{1, 2, 3}
Function signature
The signature is a technical summary of this template function. It shows the name of the function, the values (called parameters) it accepts, and what type of data each parameter expects (for example, a piece of text or a number).
Function parameters that have a = with a value after them are optional. If you leave them out, the default value shown is used automatically. Function parameters without a default are required.
set(
value: list,
) -> set
Function parameters
The following parameters can be provided to this function.
Good to know
- Sets are unordered, so add
| list | sortwhen you need a stable ordering. - All items must be hashable. Lists and dicts inside the input raise an error.
- A quick way to count distinct values is
| set | list | count.
Try it yourself
Ready to test this? Open Developer tools > Template, paste the example into the Template editor, and watch the result update on the right. Edit the values to see how the function adapts to your own entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more].
More examples
Real scenarios where this function comes up in automations and templates. Copy any example and adapt it to your setup.
Count unique values
Find out how many distinct states exist among a group of entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more].
{{
expand("group.all_lights")
| map(attribute="state")
| set
| list
| count
}}
2
Deduplicate a list
Remove duplicates from a list of detected objects.
{{ set(["person", "car", "person", "dog", "car"]) | list }}
["person", "car", "dog"]
Still stuck?
The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with your template and expected result, or share on our subreddit /r/homeassistant.
AI assistants like ChatGPT or Claude can also explain or fix templates when you describe what you want in plain language.
Related template functions
These functions work well alongside this one:
-
Convert to tuple: tuple - Converts an iterable to a tuple.
-
Set intersection: intersect - Returns items common to both lists (set intersection).
-
Set union: union - Returns all unique items from both lists (set union).
-
Set difference: difference - Returns items in the first list but not in the second (set difference).