Set union: union

The union template function combines two lists and returns all unique items from both. This is the set union operation: every item that appears in either list is included once in the result, with duplicates removed.

This is useful when you want to merge two collections without getting duplicate entries. For example, you might want to combine 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] from two different groups into one list, merge detected objects from multiple sensorsSensors return information about a thing, for instance the level of water in a tank. [Learn more], or build a complete list of all devicesA device is a model representing a physical or logical unit that contains entities. across multiple rooms. Since it operates as a set operation, each item appears only once in the result regardless of how many times it appeared in the input lists.

Usage

Here’s how to use this template function. Copy any example and adjust it to your setup.

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]
{{ union([1, 2, 3], [3, 4, 5]) }}
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].)
[1, 2, 3, 4, 5]

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.

union(
    value: list,
    other: list,
) -> list

Function parameters

The following parameters can be provided to this function.

value list Required

The first list. Must be a list or collection.

other list Required

The second list. Must be a list or collection.

Good to know

  • Duplicates within either input list are removed in the result.
  • The order of items is not guaranteed, so do not rely on a specific ordering.

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.

Combine entities from multiple groups

Build a complete list of all unique 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] across two groups.

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 group_a = expand("group.downstairs_lights")
   | map(attribute="entity_id") | list %}
{% set group_b = expand("group.upstairs_lights")
   | map(attribute="entity_id") | list %}
{{ union(group_a, group_b) }}
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].)
["light.kitchen", "light.hallway", "light.bedroom", "light.bathroom"]

Merge detected objects from multiple cameras

Combine detection lists to get a complete picture of what has been detected across all cameras.

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 cam1 = state_attr("sensor.camera_1", "detected_objects") %}
{% set cam2 = state_attr("sensor.camera_2", "detected_objects") %}
{{ union(cam1, cam2) }}
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", "cat"]

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: