Script repeat question - using repeat.index gives an error

Does this look right?

    sequence:
      - repeat:
          until:
            - condition: template
              value_template: >
                {{ is_state('input_number.irrigation_number_of_zones') | int == repeat.index }}

Because I get an error:

irrigation_run_a_cycle: Error executing script. Unexpected error for repeat at pos 1: 'mappingproxy' object does not support item assignment
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 181, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 489, in _async_repeat_step
    set_repeat_var(iteration)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 443, in set_repeat_var
    self._variables["repeat"] = repeat_vars
TypeError: 'mappingproxy' object does not support item assignment

If it looks right I must have something else wrong…
:thinking:

As shown in (you were there) : -

sequence: goes level with while:
In general, copy working examples and adapt until you get used to it (the story of my life with HA)
:rofl:

Are you using 0.113 or 0.113.1?

There’s a correction in 0.113.1 for the repeat variable (repeat.index).

I’m not using while, I’m using until but yeah, in this case it doesn’t matter.
However I am doing this:

  my_script:
    sequence:
      - repeat:
          until:
            - condition: template
              value_template: >
                {{ is_state('input_number.irrigation_number_of_zones') | int == repeat.index }}

          sequence:
            - service: a service
            - service: another service

      - service: a service to do when repeat finished

@123
I’m on 113.1

EDIT Maybe you can’t have a service after the repeat block?

I think I see the problem now. The is_state function takes two arguments and you only supplied one. In fact, I think you meant to use the states function, not is_state.

    sequence:
      - repeat:
          until:
            - condition: template
              value_template: >
                {{ states('input_number.irrigation_number_of_zones') | int == repeat.index }}

Aarrrggghhh…!!!
Too busy thinking about the new scripting syntax.
Thanks!!

:blush:

Except…

Same error!

  irrigation_run_a_cycle:
    sequence:
      - repeat:
          until:
            - condition: template
              value_template: >
                {{ states('input_number.irrigation_number_of_zones') | int == repeat.index }}

          sequence:
              etc....
irrigation_run_a_cycle: Error executing script. Unexpected error for repeat at pos 1: 'mappingproxy' object does not support item assignment
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 181, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 489, in _async_repeat_step
    set_repeat_var(iteration)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 443, in set_repeat_var
    self._variables["repeat"] = repeat_vars
TypeError: 'mappingproxy' object does not support item assignment

Darn! I believe this is indeed a bug. Are you calling the script with any variables, or indirectly in an automation?

Looks like I didn’t consider that the variables passed into a script may not be a regular dict object.

I’ll open an issue based on this and get to work on a fix.

THANKS!!

3 Likes

I am calling the script from an automation and passing it variables:

      - service: script.irrigation_run_a_cycle
        data_template:
          cycle: >
            {% set start_time_cycle1 = states('sensor.irrigation_cycle1_start_time_in_seconds') | int %}
            {% set start_time_cycle1 = start_time_cycle1 | timestamp_custom('%H:%M', false) %}

            {% if 'cycle3' in trigger.entity_id %}
              cycle3
            {% elif states('sensor.time') == start_time_cycle1 %}
              cycle1
            {% else %}
              cycle2
            {% endif %}
          pause: 0

Let me know if you want to see any more of the ‘offending’ script but I don’t think it would be relevant (and it is quite long).

1 Like
2 Likes

More than one thing wrong and the use of is_state wasn’t the biggest problem! Your code exposed a bug and, hopefully, pnbruckner will have a correction available in an upcoming patch release.

Really big changes in scripting so a few little bugs are bound to have slipped by the testers.

3 Likes

@pnbruckner Hi, The issue seems to be persisting after updating to 0.113.2

Can you provide an example of what’s not working for you?

When running the the package, which was written by the original author (klogg), the irrigation automation kicks off as expected then gets stuck on the script which repeats through the configured zones.

It is the same script mentioned in the posts above - I have tried adding mode: parallel before the sequence entry, but it does not resolve the issue.

This script functioned on 0.112.4, only change to it was the testing of mode:parallel to try and resolve the issue. The package can be found at https://github.com/kloggy/HA-Irrigation-Version2

Thank you for the quick response.

I’m sorry, but I can’t try to fix some giant package someone else wrote. If you can provide a small, reproducible example that shows that something in HA is not working, then I’m more than happy to try and reproduce the problem, and if I can, fix it.

1 Like

Thank you - I will try and extract the exact piece of script and reproduce the error.

FWIW, I just tried this:

test1:
  sequence:
  - repeat:
      sequence:
      - event: abc
      until:
      - condition: template
        value_template: "{{ repeat.index == last }}"

I called it via the SERVICES tab, passing in the variable last. As best I can tell that should simulate the problem noted in the OP. It fails w/ 0.113.1, but works just fine for me in 0.113.2.

1 Like

I just added the debug logging to my HA and restarted, will see if I can find any helpful information in there to assist in finding the cause.

Appreciate your effort! :pray:

UPDATE: I checked the logs on debug, but cannot see any errors while running the script. Will remove the mode:parallel fix and see if that is perhaps now throwing things over.

UPDATE2: Still not working correctly, but I will try and pin point where the issue might be.

1 Like

Just because something doesn’t cause errors doesn’t mean it works correctly. Have you asked the author of the “package” you’re trying to use?