Repeat in Script not working

Hi,
I am trying to get a google tts repeat a couple of times a message, but all I can get it to do, is do it once…

Below is my code for the script,
Can someone spot where the problem may be?

TYA

urgent_notification_alarm:
  alias: Urgent Notification Alarm
  sequence:
  - service: notify.notify
    data:
      message: '{{msg}}'
  - service: media_player.volume_set
    data:
      volume_level: 0.9
    target:
      entity_id:
      - media_player.office_display
      - media_player.kitchen_display
      - media_player.aly_google
  - repeat:
      count: 2
      sequence:
      - service: tts.google_translate_say
        data:
          entity_id:
          - media_player.office_display
          - media_player.kitchen_display
          - media_player.aly_google
          message: '{{msg}}'
      - wait_template: "{{(states('media_player.kitchen_display') == 'idle'\nand\n\
          states('media_player.office_display')== 'idle'\nand \nstates('media_player.aly_google')==\
          \ 'idle'\n)}}"
        timeout: 00:00:30
        continue_on_timeout: true
  mode: restart
  icon: mdi:alarm-light

to add: It says it only ONCE even if I remove the continue_on_timeout

I think what may be happening is that, on the first loop, the wait_template processes the media_players state before they even have time to change from idle to playing. So there’s no pausing by wait_template and the second loop occurs immediately (and sends a second TTS command before the first one even completed). What you hear is the second loop’s TTS command. Anyway, that’s my theory …

Add a 2-3 second delay before the wait_template. It will give the media_players a tiny bit of time to process the first TTS command and change their state from idle to playing.

      - delay: '00:00:02'

You nailed it, Taras!

Looking at what I want to achieve, I think I will leave my wait template out, so in case one of entities never comes back to idle, the rest will keep announcing. I added a long enough delay which shall be sufficient to allow speech to fit in.

1 Like