Script repeat question - using repeat.index gives an error

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?

He is the OP of this thread, so he is aware of the issue.

I’m rolling back now to 0.112.4 and will build a test environment where the logs are less “chatty” to try and pin point the issue.

I see on the package thread (My Garden Irrigation) that other users are also experiencing this.

I have not yet finished the changes needed for the ‘new scripting’.

As he said, pnbruckner

Please don’t expect him to!

I will be fixing the package I wrote as soon as I can. I hope that will be tomorrow.
See the thread for the package for further info.

2 Likes

@klogg Thank you - Never was my intention for pnbruckner to do it :flushed:

I was under the impression after the bugfix was applied that the package will continue to work - I apologise if I offended anyone, was not my intention. I appreciate both your & pnbruckner’s assistance with all of this.

2 Likes

Hi! Is there still a problem with this variable, or is my code at fault? I’m still getting the value undefined error. I’m running CORE 2024.3.3 AND OS 12.1. I can do this another way, but was curious if this is still a bug. Thanks for any help.

alias: Reminder Announcement
sequence:
  - repeat:
      sequence:
        - service: tts.cloud_say
          data:
            cache: false
            entity_id: media_player.nestmini9041
            message: "{{message}}.  When finished, tell google, \"Mischief Managed!\"."
        - delay:
            hours: 0
            minutes: 10
            seconds: 0
            milliseconds: 0
      while:
        - condition: state
          entity_id: input_boolean.reminderanouncementswitch
          state: "on"
        - condition: template
          value_template: "{{repeat.index <= 30}}"
mode: single
fields:
  message:
    selector:
      text: null
    name: Message
    description: The Message to speak
    default: Reminder Announcement
    required: true
`Preformatted text`pe or paste code here