Merge action responses: merge_response

The merge_response template function takes the response dictionary from a multi-entity action call and merges it into a single flat list. When you call an action that targets multiple 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], the response is a dictionary keyed by entity ID, where each value contains the entity’s response data. merge_response flattens this structure into a single list, adding the entity_id to each item for reference.

This is useful when you call actions like calendar.get_events or weather.get_forecasts that target multiple entities at once. Without merge_response, you would need to manually loop through the response dictionary and extract items from each entity. This function handles that for you and produces a unified list that is straightforward to sort, filter, and display. Each item in the result includes an entity_id field so you can still tell which entity it came from.

Usage

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

As a function
{{ merge_response(response) }}
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].)
[
  {"start": "2024-03-15", "summary": "Meeting", "entity_id": "calendar.work"},
  {"start": "2024-03-15", "summary": "Dentist", "entity_id": "calendar.personal"},
]

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.

merge_response(
    value: dict,
) -> list

Function parameters

The following parameters can be provided to this function.

value map Required

The action response dictionary to merge. This must be a dictionary where each key is an entity ID and each value is a dictionary containing the entity’s response data.

Good to know

  • Adds an entity_id field to each item, so you can tell which entity each response came from after merging.
  • Only works on responses shaped as {entity_id: {key: [items]}}. Responses with a different shape need manual handling.

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.

Merge calendar events from multiple calendars

Call calendar.get_events targeting multiple calendars and merge all events into a single sorted list.

ActionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called *sequence*. [Learn more]
action:
  - action: calendar.get_events
    target:
      entity_id:
        - calendar.work
        - calendar.personal
    data:
      duration:
        hours: 24
    response_variable: agenda
  - action: notify.mobile
    data:
      message: >
        Today's events:
        {% for event in merge_response(agenda) | sort(attribute="start") %}
        - {{ event.summary }} ({{ event.entity_id.split(".")[1] }})
        {% endfor %}

Merge weather forecasts

Combine forecasts from multiple weather entities into one list for comparison.

ActionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called *sequence*. [Learn more]
action:
  - action: weather.get_forecasts
    target:
      entity_id:
        - weather.home
        - weather.office
    data:
      type: daily
    response_variable: forecasts
  - action: notify.mobile
    data:
      message: >
        {% for item in merge_response(forecasts) %}
        {{ item.entity_id }}: {{ item.temperature }}C
        {% endfor %}

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: