Automation Repeat for Each item in action response

I am open to ideas on the best way to achieve this. I am using the HA-EPG integration to remind me about live sports events. The integration has a service/action which searches all channels for a phrase and outputs into the response variable as an array/list I am imaginatively calling response.

I have been able to iterate through that response variable to send notifications to my mobile each day with the below YAML

  - action: notify.mobile_app_galaxy_s22
    metadata: {}
    data:
      message: >-
        The following live motorsport is on today: <p> {% for result in
        response.results %} {{ result.start_time }} {{ result.title }}<br> {%
        endfor %} </p>
      title: Live Motor Racing Alert

This works perfectly. I am then thinking surely it would make sense to pass that information into specifc events in my calandar, obviously I need to use the create_event action and I assume I can call out the variables in the same way I have done above.

What I am struggling with is how to repeat through each item in the response variable, basically what I need to put in the for_each section. I have tried

{{result in response.results}}

and many others. Can anyone suggest what the value should be, if this is even possible? It feels like it should be possible but all I get is malformed errors when I try and save. Is there a better way to do this which I am missing?

repeat:
  for_each: "{{ response.results }}"
  sequence:
    - action: calendar.create_event
      target:
        entity_id: calendar.your_calendar
      data:
        summary: "{{ repeat.item.title }}"
        start_date_time: "{{ repeat.item.start_time }}"
        ... etc ...

NOTE

If start_time only contains the time but not the date then you’ll need to use a template to add the date.

Thanks for the response however my issue isn’t with pulling the data from the response, its getting the repeat loop syntax correct. If it helps below is a response from the EPG integration action. What I am trying to work out is for the given example how can I get the automation code to repeat the same number of times as there are results (in the below example twice).

response:
  results:
    - channel_name: SkySp F1 HD
      title: 'Live Monaco F2: Feature Race'
      description: >-
        Formula 2 race weekend concludes with a Feature Race in Monte-Carlo.
        First-time F2 polesitter Alexander Dunne leads the way from Victor
        Martins and Leonardo Fornaroli.
      start_time: '08:35'
      end_time: '09:55'
      date:
        __type: <class 'datetime.date'>
        isoformat: '2025-05-25'
      start_datetime_iso: '2025-05-25T08:35:00'
    - channel_name: SkySp F1 HD
      title: Live Monaco F1 Grand Prix
      description: >-
        On the streets of Monte Carlo, the one-of-a-kind Monaco Grand Prix
        arrives, as the 2025 field of Formula 1 stars navigate their way around
        the Principality.
      start_time: '13:55'
      end_time: '16:00'
      date:
        __type: <class 'datetime.date'>
        isoformat: '2025-05-25'
      start_datetime_iso: '2025-05-25T13:55:00'

The example I posted uses correct repeat syntax for processing each item in response.result’s list (two items in what you posted).

That’s what for_each does. It processes each item in a list. Each list item is represented by repeat.item.

Your remaining challenge is to create appropriate values for the calendar.create_event action’s start_date_time and end_date_time using what’s available in response.results.

According to its documentation, you will need to combine the date and time into a single string in this format: YYYY-MM-DD HH:MM:SS

Perhaps something like this:

repeat:
  for_each: "{{ response.results }}"
  sequence:
    - action: calendar.create_event
      target:
        entity_id: calendar.your_calendar
      data:
        summary: "{{ repeat.item.title }}"
        start_date_time: "{{ repeat.item.start_datetime_iso[:10] }} {{ repeat.item.start_time }}"
        end_date_time: "{{ repeat.item.start_datetime_iso[:10] }} {{ repeat.item.end_time }}"

thanks @123 sorry for the miss understanding. I did try

for_each: "{{ response.results }}"

When the automation runs I get the error below

Error: Repeat ‘for_each’ must be a list of items

Any further ideas?

The example you posted, of the response from your EPG integration, indicates the following data structure:

  • response contains a dictionary with a single key named results.

  • The results key contains a list of two items.

  • Each list item contains a dictionary with seven keys (channel_name, title, description, etc).

To access the list, you would use response.results or response['results']. This is what you did in the action example you shared in your first post (the template uses {% for result in response.results %} to iterate through the list).

The example I posted iterates through the same list (response.results). However, the error message indicates response.results does not contain a list. It implies the data structure of response is no longer the same as described above.

Please post a link to the EPG integration you’re using and post your entire automation so I can see how it defines the response variable.

I agree with what you are saying. I wonder if its some sort of formatting or variable type issue with response variables in general? Like when you have a number saved as a string and try and do a calculation on it?

The integration I am using is GitHub - yohaybn/HomeAssistant-EPG: EPG (Electronic Program Guide) sensor for HomeAssistant using open-epg.com data.

I appreciate the effort in trying to help me solve this. FYI, I have tried both response.results and response['results'] with the same behaviour. The response is obviously not always two items but with my tests so far it is always more than one.

The for_each configuration variable will only accept simple objects, so if your response contains datetime objects, you will need to remove them or convert them to datetime strings.

Try something like the following to filter out the datetime object being supplied in the date value from the integration:

variables:
  results: |
    {% set r = response.results %}
    {{ zip(r|map(attribute='title')|list, r|map(attribute='start_datetime_iso')|list,
    r|map(attribute='end_time')|list) }}
repeat:
  for_each: "{{ results }}"
  sequence:
    - action: calendar.create_event
      target:
        entity_id: calendar.your_calendar
      data:
        summary: "{{ repeat.item[0] }}"
        start_date_time: "{{ repeat.item[1] }}"
        end_date_time: "{{ repeat.item[1][:11] ~ repeat.item[2] }}"

According to the example in the EPG integration’s documentation, the value of the date key should contain a date string.

However, in your case, it’s a datetime.date object (or at least an attempt to express it in YAML format).

I have tried to confirm Didgeridrew’s assertion that the presence of datetime objects in the response date is responsible for causing for_each to report:

Error: Repeat ‘for_each’ must be a list of items

Unfortunately, I have been unsuccessful in replicating your response data. In other words, I cannot force it to contain datetime.date objects (see Note below if you’re interested in knowing what I tried).

All this to say that the onus is on you to test Didgeridrew’s suggestion because I can’t replicate the conditions needed to test it.

In addition, I suggest you contact the author of the EPG integration and report that it can produce a response containing datetime object values. Ideally, it should only produce datetime strings.


NOTE

Here’s how I tried to produce response data containing datetime objects (specifically datetime.date objects).

  actions:
    - variables:
        response:
          results:
            - channel_name: SkySp F1 HD
              title: 'Live Monaco F2: Feature Race'
              description: >-
                Formula 2 race weekend concludes with a Feature Race in Monte-Carlo.
                First-time F2 polesitter Alexander Dunne leads the way from Victor
                Martins and Leonardo Fornaroli.
              start_time: '08:35'
              end_time: '09:55'
              date:  "{{ ('2025-05-25' | as_datetime).date() }}"
              start_datetime_iso: '2025-05-25T08:35:00'

In the Template Editor, this produces a datetime.date object:

{{ ('2025-05-25' | as_datetime).date() }}

However, when used in the response data shown above, the value is converted to a string as can be seen in this portion of the automation’s trace file:

The resulting response data doesn’t contain datetime objects so it fails to replicate your data for (my) testing purposes.

Thanks both for you response. I have raised with the Integration dev first as they seem quite responsive

Interesting I found that the Dev Tools response is simple a date like in the example on the github page but the automation response is more complex. I am not sure if this is something specific to my environment, the integration or is expected in HA? Either way I will see if the dev can resolve before continuing but I expect you have saved me a some time with the rest of the automation. much thanks

1 Like