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.

As a function
{{ set([1, 2, 2, 3, 3, 3]) }}
Result (set)
{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.

value list Required

The collection to convert to a set. Duplicate values are removed.

Good to know

  • Sets are unordered, so add | list | sort when 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].

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{
  expand("group.all_lights")
  | map(attribute="state")
  | set
  | list
  | count
}}
Result (integerA whole number without decimal places, like 1, 42, or -5. Used for counts, indices, and whole values.)
2

Deduplicate a list

Remove duplicates from a list of detected objects.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ set(["person", "car", "person", "dog", "car"]) | list }}
Result (listAn ordered collection of values, like a list of entity IDs or a list of numbers. Written with square brackets in templates, for example [1, 2, 3].)
["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.

Tip

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: