Help with Repeat Until Script

Hello to everyone, i need some help with this Script:

01notifiche_alexa_allarme_test_novr:
  sequence:
    - delay:
        milliseconds: 1
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 1
    - delay: 00:00:02
    - repeat:
        sequence:
          - data_template:
              data:
                method: all
                type: announce
              title: 'Prova'
              message: 'Prova'
            service: notify.alexa_media_ovunque
            # Give it time to complete
          - delay: 00:00:05
        until:
          # Did it work?
          - condition: state
            entity_id: alarm_control_panel.allarme
            state: 'armed_away'
    - delay:
        milliseconds: 1
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 0.5

If the alarm is in armed_away state the code run only one timeā€¦ what is wrong?
Thanks a lot
Stefano

Well thatā€™s the point of an until loop.
For/counted loop: Repeat X times
While loop: Repeat while X is true
Until loop: Repeat until X is true

Or am I missing something?

Damnā€¦ sorry for my stupid question
Regards
Stefano

No worries, report back if it works :slight_smile:

Perfect works very well now :smiley:
I have confused until with doā€¦while
the code that work is this:

01notifiche_alexa_allarme_test_novr:
  sequence:
    - delay:
        milliseconds: 1
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 1
    - delay: 00:00:02
    - repeat:
        sequence:
          - data_template:
              data:
                method: all
                type: announce
              title: 'Prova'
              message: 'Prova'
            service: notify.alexa_media_ovunque
            # Give it time to complete
          - delay: 00:00:05
        while:
          # Did it work?
          - condition: state
            entity_id: alarm_control_panel.allarme
            state: 'triggered'
    - delay:
        milliseconds: 1
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 0.5

Again thanks a lot :smiley:
Stefano

1 Like

A couple thingsā€¦

First, Iā€™m wondering, why the 1 ms delays?

Second, you do know that with a while loop itā€™s possible the actions will never run, right? I.e., if the state of the alarm is not triggered when the script is called then it will not run the sequence in the loop.

If you always want it to run the sequence at least once, then use until:

- repeat:
    sequence:
      - ...
  until:
    - condition: not
      conditions:
        - condition: state
          entity_id: alarm_control_panel.allarme
          state: 'triggered'

Thanks a lot for your reply @pnbruckner, 1st for the beautiful implementation and news about scripting and automations :slight_smile:
I have the 1 Ms second because I have a series of script and the delay permit at the automation to call other script asynchronous.
I also will wait a patch for the variables, now I receive an error if I use the variables.

Thanks a lot
Stefano

Not anymore. That was a workaround that relied on the buggy behavior of scripts/automations.

Please read Waiting for Script to Complete for the proper way to do that.

Tanks a lot Again
But can i use this also in automation?
I have an automation that call a list of script so if i use script.turn_on in the automation action works like the wiki?
An works also if part of the script fail? for example service: media_player.volume_set
The new script

01notifiche_alexa_allarme_test_novr:
  sequence:
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 1
    - delay: 00:00:02
    - repeat:
        sequence:
          - data_template:
              data:
                method: all
                type: announce
              title: 'Prova'
              message: 'Prova'
            service: notify.alexa_media_ovunque
            # Give it time to complete
          - delay: 00:00:05
        until:
          - condition: not
            conditions:
              - condition: state
                entity_id: alarm_control_panel.allarme
                state: 'triggered'
    - delay:
        milliseconds: 1
    - service: media_player.volume_set
      data:
        entity_id: media_player.ovunque
        volume_level: 0.5

Iā€™m not sure I understand what you mean by that.

What do you mean by ā€œworks like the wikiā€?

That is not a script, that is a service.

Iā€™m sorry, Iā€™m really not following what youā€™re asking.

You still have one 1ms delay in your script that you probably donā€™t need.

Thanks again for your patience @pnbruckner really appreciate :slight_smile:
Sorry if Iā€™m not so clear :frowning:
This is my automation:

- id: '1564681991925'
  alias: 0.2Allarme Triggered
  trigger:
  - entity_id: alarm_control_panel.allarme
    platform: state
    to: triggered
  condition: []
  action:
  - data:
      entity_id: script.03notifica_telegram_cameracucina_immagine
    service: script.turn_on
  **- data:**
**      entity_id:**
**      - switch.0x000d6ffffedd6f8e_switch**
**      - switch.0x000d6ffffee3a23f_switch**
**    service: switch.turn_on**
  - data:
      entity_id: script.03notifica_telegram_cameracucina_video
    service: script.turn_on
  - data:
      entity_id: script.04cameracucina_immagine
    service: script.turn_on
  - data:
      entity_id: script.04cameracucina_video
    service: script.turn_on
  - data_template:
      entity_id: script.02notifica_telefonata
      variables:
        message: 'Attenzione!!! ALLARME ATTIVATO!! Sensori Attivi: {% for state in
          states.binary_sensor -%} {% if state.state == ''on'' and state.entity_id
          not in states.group.noalarm.attributes.entity_id %}  {{ state.name }} {%-
          endif -%} {%- endfor -%}'
    service: script.turn_on

If for example the bold part of the action fail the automation call also this?

  - data:
      entity_id: script.03notifica_telegram_cameracucina_video
    service: script.turn_on

There is also some fix for variables in the repeat script?

Thanks a lot
stefano

First, Iā€™m not sure how turning on a switch can fail or cause an error. But if it did, that would cause the automation to stop running its actions so the following steps that call other scripts would not be executed. Also, since script.03notifica_telegram_cameracucina_immagine was called via script.turn_on, it would not be affected by the error and would continue to run independent of the automation.

Next, this:

  - data:
      entity_id: script.03notifica_telegram_cameracucina_video
    service: script.turn_on
  - data:
      entity_id: script.04cameracucina_immagine
    service: script.turn_on
  - data:
      entity_id: script.04cameracucina_video
    service: script.turn_on

can be reduced to this:

  - data:
      entity_id:
      - script.03notifica_telegram_cameracucina_video
      - script.04cameracucina_immagine
      - script.04cameracucina_video
    service: script.turn_on

@pnbruckner
Thanks again a lot for your reply and for suggestion to use only one script.turn_on more clear :smiley:
Thanks for all your help :smiley:
Stefano

1 Like

@pnbruckner another question:
Based on what you have written here i can also do this?
From:

  - data:
      entity_id: script.00notifiche_app_stefano
      variables:
        critical: 1
        messages: "Attenzione!!! ALLARME SCATTATO!! Sensori: {% for state in states.binary_sensor\
          \ -%}\n{% if state.state == 'on' and state.entity_id not in states.group.noalarm.attributes.entity_id\
          \ %}\n {{ state.name }}\n {%- endif -%}\n {%- endfor -%}\n"
        names: Alarm Alert Effect.wav
        volume: 1
    service: script.turn_on
  - data:
      entity_id: script.00notifiche_app_micol
      variables:
        critical: 1
        messagem: "Attenzione!!! ALLARME SCATTATO!! Sensori: {% for state in states.binary_sensor\
          \ -%}\n{% if state.state == 'on' and state.entity_id not in states.group.noalarm.attributes.entity_id\
          \ %}\n {{ state.name }}\n {%- endif -%}\n {%- endfor -%}\n"
        namem: Alarm Alert Effect.wav
        volume: 1
    service: script.turn_on

to:

  - data:
      entity_id: 
        - script.00notifiche_app_stefano
        - script.00notifiche_app_micol
      variables:
        critical: 1
        messages: "Attenzione!!! ALLARME SCATTATO!! Sensori: {% for state in states.binary_sensor\
          \ -%}\n{% if state.state == 'on' and state.entity_id not in states.group.noalarm.attributes.entity_id\
          \ %}\n {{ state.name }}\n {%- endif -%}\n {%- endfor -%}\n"
        names: Alarm Alert Effect.wav
        volume: 1
    service: script.turn_on

Is this correct?
Thansk
Stefano

Unless messagem & namem arenā€™t typos, yes that should work.

Thanks a Lot :smiley:

I cant seem to get my repeat script to work. Any advice please?

image

  1. For future reference, please post your question in a separate topic and not part of a thread that has been inactive for 2 months.
  2. Please post your code and not a screenshot of your code.
  3. Does the script pass Configuration > Server Controls > Check Configuration?
  4. Are you certain the condition is correct?
  5. What do you mean exactly when you say it doesnā€™t work? Does it produce an error message in the log or does it not behave the way you expected?

Hi. Apologies NB here. Really struggeling. Please see answers to your questions:

  1. For future reference, please post your question in a separate topic and not part of a thread that has been inactive for 2 months. - Will Do.
  2. Please post your code and not a screenshot of your code. - Really struggling to get the indentation correct when I dot hat.
  3. Does the script pass Configuration > Server Controls > Check Configuration? Cant seem to find this in that directory
  4. Are you certain the condition is correct? I think so yes.
  5. What do you mean exactly when you say it doesnā€™t work? Does it produce an error message in the log or does it not behave the way you expected?Let me try again:
besproeing_repeat:
  mode: single
  sequence:
  - repeat:
      until:
      - condition: state
        state: 'on'
        entity_id: '{{besp}}'
      sequence:
      - service: switch.turn_on
        data:
          entity_id: '{{besp}}'

Error I get:
Invalid config for [script]: Entity ID {{besp}} is an invalid entity id for dictionary value @ data[ā€˜scriptā€™][ā€˜besproeing_repeatā€™][ā€˜sequenceā€™][0][ā€˜repeatā€™][ā€˜untilā€™][0][ā€˜entity_idā€™]. Got ā€˜{{besp}}ā€™. (See /config/configuration.yaml, line 12).

Home Assistant has started!

The error message indicates {{besp}} is an invalid entity_id. Thatā€™s because a State Condition does not permit the use of a template for entity_id.

Change the State Condition to a Template Condition.

besproeing_repeat:
  mode: single
  sequence:
  - repeat:
      until:
      - condition: template
        value_template: "{{ is_state(besp, 'on') }}"
      sequence:
      - service: switch.turn_on
        data:
          entity_id: '{{besp}}'

That should eliminate the error message.

You should know that this automation is not likely to ever perform the repeat for more than one iteration. The moment it turns on the switch, the condition is satisfied and there are no additional repetions.