Cannot get repeat - for_each loopt to work: error: 'repeat' is undefined

Hi,
I’m new to HA but was able to do some basic set-up so far.
At this point I’m trying to make a script to identify the time slots for the lowest 25 percentile prices from the nordpool epex integration.
I have nordpool working perfectly fine and for my purpose I’m trying to loop through the ‘raw_today’ attribute list of the nordpool sensor.
I get to a point where I have an attribute list of values, but no way can I succeed to loop through it (for_each), I keep getting the error ‘‘repeat’ is undefined’.
I have read the script syntax manual up and down and I can’t even get the examples in there to work when I paste then in the template editor.
I’m sure the issue is a small detail, but I’ve spent many hours on it and I don’t see the problem.
The example code from the script syntax manual I’m trying to execute:

repeat:
  for_each:
    - language: English
      message: Hello World
    - language: Dutch
      message: Hallo Wereld
  sequence:
    - action: notify.phone
      data:
        title: "Message in {{ repeat.item.language }}"
        message: "{{ repeat.item.message }}!"

Error: ‘repeat’ is undefined

My code:

alias: New automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.send_webhook
conditions: []
actions:
  - action: input_number.set_value
    metadata: {}
    data:
      value: >-
           {{ state_attr('sensor.nordpool_kwh_be_eur_2_05_0', 'raw_today')  | map(attribute="value") | list}}
    target:
      entity_id: input_number.epexmin
  - repeat:
      for_each: "{{ state_attr('sensor.nordpool_kwh_be_eur_2_05_0', 'raw_today') }}"
      sequence:
        - if:
            - condition: numeric_state
              entity_id: input_number.epexmin
              above: "{{ repeat.item }}"
          then:
            - action: input_number.set_value
              metadata: {}
              data:
                value: "{{ repeat.item }}"
              target:
                entity_id: input_number.epexmin
mode: single

Error: ‘repeat’ is undefined

Looking forward to a solution, thank you!

Where are you seeing this error?


I don’t use Nordpool, so I may be missing something, but based on the template used in the first input_number.set_value action:

  1. You are setting the value of a number entity to a list… which doesn’t make any sense
  2. The for_each is being set to an object. In itself, this is fine, but…
    a. The for_each configuration key can only handle simple objects, so if your object contains complex objects like datetime objects, it will fail.
  3. The If condition is checking if a number is above an object… which doesn’t make any sense.
  4. The Then action is setting a number to an object… which doesn’t make any sense

Please describe what the actual goal is.

You may be interested in:

Hi,
Thank you for your swift reply.
I did so much testing and going back and forth between the template editor and the automation editor that I mixed up things and got confused.
My goal is to calculate the number of time slots when the prices are cheapest per day (24hrs) and daytime (12 hrs, 06:00 - 18:00) for dynamic electricity prices.
I use this information to calculate the required average charging power over these time slots (since Oct 1st this are 15min time slots) for my home battery and PHEV when there is no surplus power from the solar panels.
Meanwhile -and thanks to your message that made me rethink what I was doing- I got this working.
Below you find my test dashboard with the dynamic prices for today and tomorrow and the calculated number of 15-minute time slots for today.
I also added the Automation YAML script. This script also contains calculations for hourly averages, but this is for testing purposes only.


alias: Epex vLow data
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.send_webhook
  - trigger: time_pattern
    hours: /1
    minutes: "1"
conditions: []
actions:
  - action: input_number.set_value
    metadata: {}
    data:
      value: >-
        {{ ((state_attr('sensor.nordpool_kwh_be_eur_2_05_0',
        'max')-state_attr('sensor.nordpool_kwh_be_eur_2_05_0',
        'min'))/4+state_attr('sensor.nordpool_kwh_be_eur_2_05_0', 'min')) }}
    target:
      entity_id: input_number.epex25pctile
  - action: input_number.set_value
    metadata: {}
    data:
      value: 0
    target:
      entity_id:
        - input_number.epexvlow
        - input_number.epexvlowday
        - input_number.epexvlowdayleft
        - input_number.epexvlowleft
        - input_number.epextemp
        - input_number.epexvlowhr
        - input_number.epexvlowhrleft
  - repeat:
      for_each: "{{ state_attr('sensor.nordpool_kwh_be_eur_2_05_0', 'today') }}"
      sequence:
        - if:
            - condition: template
              value_template: >-
                {{ states('input_number.epex25pctile') | float > repeat.item |
                float }}
          then:
            - action: input_number.increment
              metadata: {}
              data: {}
              target:
                entity_id: input_number.epexvlow
            - if:
                - condition: template
                  value_template: "{{ (repeat.index > 25) and (repeat.index <= 72) }}"
              then:
                - action: input_number.increment
                  metadata: {}
                  data: {}
                  target:
                    entity_id: input_number.epexvlowday
                - if:
                    - condition: template
                      value_template: >-
                        {{ repeat.index > ((now().hour * 4) + (now().minute /
                        15) | int) }}
                  then:
                    - action: input_number.increment
                      metadata: {}
                      data: {}
                      target:
                        entity_id: input_number.epexvlowdayleft
            - if:
                - condition: template
                  value_template: >-
                    {{ repeat.index > ((now().hour * 4) + (now().minute / 15) |
                    int) }}
              then:
                - action: input_number.increment
                  metadata: {}
                  data: {}
                  target:
                    entity_id: input_number.epexvlowleft
        - if:
            - condition: template
              value_template: "{{ (repeat.index % 4) == 0 }}"
          then:
            - if:
                - condition: template
                  value_template: >-
                    {{ states('input_number.epex25pctile') | float >
                    states('input_number.epextemp') | float }}
              then:
                - action: input_number.increment
                  metadata: {}
                  data: {}
                  target:
                    entity_id: input_number.epexvlowhr
                - if:
                    - condition: template
                      value_template: >-
                        {{ repeat.index > ((now().hour * 4) + (now().minute /
                        15) | int) }}
                  then:
                    - action: input_number.increment
                      metadata: {}
                      data: {}
                      target:
                        entity_id: input_number.epexvlowhrleft
            - action: input_number.set_value
              metadata: {}
              data:
                value: 0
              target:
                entity_id: input_number.epextemp
          else:
            - action: input_number.set_value
              metadata: {}
              data:
                value: >-
                  {{ states('input_number.epextemp') | float + repeat.item/4 |
                  float }}
              target:
                entity_id: input_number.epextemp
mode: single

Assuming this is correct:

This would mean that this

is a list of dictionaries with at least a values key in each dictionary. Meaning that these are not correct:

This is the same for your most recent automation

Hi Petro,

Thank you for your reaction.
Yes, I made that mistake in the first piece of code that I sent: ‘raw_today’ is indeed a list of dictionaries with a time and a value pair.
Later I realized that for the same entity there’s also a ‘today’ list attribute,so for my purpose I could simply use thar as I don’t require the time just for a count.
So in the current version of my script I’m using that list and that appears to be working just fine.