Use repeat for each item separated by comma

Hi,

I’m new working with the templates, i tried searching for a way to make it work but i only get error when trying to save this automation.

The error is Message malformed: extra keys not allowed @ data[‘actions’][0][‘sequence’][2][‘repeat’][‘foreach’]

This is the code i for the automation.

alias: "Generate shopping list"
triggers:
  - trigger: conversation
    command:
      - I need to buy  {things}
conditions: []
actions:
  - sequence:
      - action: google_generative_ai_conversation.generate_content
        metadata: {}
        data:
          prompt: >-
           You are a shopping assistant, you are going to receive an order with things.

            All the items that are mentioned must be separated by a comma.

            The items are {{trigger.slots.things}}
        response_variable: response
      - repeat:
          foreach: "{{response.text.split(',')}}"
          sequence:
            - action: shopping_list.add_item
              metadata: {}
              data: {
                name: {{repeat.item}}
              }
mode: single

It’s my first post, sorry if I’m doing it wrong.

There are a few issues:

repeat:
  for_each: "{{response.text.split(',')}}"
  sequence:
    - action: shopping_list.add_item
      metadata: {}
      data:
        name: "{{repeat.item}}"
    - delay:
        milliseconds: 250

The delay may not be 100% necessary, but it’s often best to include a short one in a repeat so you don’t spam the endpoint.

2 Likes

Thank you! It worked perfectly