Unexpected problem when using "while" with TTS

I have an automation to speak (tts) and notify devices if a sensor detects humidity. Sensor is irrelevant (Ikea, MQTT) as it works.
I want it to repeat the message “Water leak in…” every 30 seconds until sensor gets dry.
I first set it to speak/notify, then enter a “while (sensor wet)” loop to speak/notify then wait 30 seconds.
That works.
Then I thought the first speak/notify is redundant, as the one inside the while loop should be enough. Here’s the rub: if I disable the speak/notify before the “while” then the one inside the “while” doesn’t work.
I suspect that the next call happens before the tts finishes but why would that depend on what’s before the loop?
Log includes things like “SpeakScript turned off triggered by automation AutomationName triggered by state of SensorName”
I tried direct tts “calls”, separate scripts to call the TTS, even script for the 30s delay (instead of just the delay) in the hope that scripts would wait for each other.
The code below works, but doesn’t if I disable the “scriptandnotifydevices” before the “repeat”:

alias: ZigIkeaWater1-Wet_Test
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.zigikeawater1_water_leak
    to: "on"
conditions: []
actions:
  - action: media_player.volume_set
    metadata: {}
    data:
      volume_level: 0.31
    target:
      entity_id: media_player.cat_s_bedroom_speaker
    enabled: true
  - action: script.scriptspeakandnotifydevices
    metadata: {}
    data:
      message: Water leak in "{{ location }}"
    enabled: true
  - repeat:
      sequence:
        - action: script.speakmessage
          metadata: {}
          data:
            message: Water leak in "{{ location }}"
          alias: "Call script SpeakMessage: \"Water leak in...\""
        - action: script.scriptwait30s
          metadata: {}
          data: {}
      while:
        - condition: state
          entity_id: binary_sensor.zigikeawater1_water_leak
          state: "on"
  - action: script.scriptspeakandnotifydevices
    metadata: {}
    data:
      message: No more water leak in "{{ location }}"
    alias: "Script SpeakAndNotify: \"No more Water leak in...\""
mode: single
variables:
  location: the laundry room

What’s the difference between script.scriptspeakandnotifydevices and script.speakmessage?

I only need notification once when wetness starts, so inside the loop it just speaks again and again, every 30s without new notifications.

It’s hard to say without the code, but the only way I can think of to explain what you’re seeing is if the script called inside the loop somehow depends on the other script to do something.

In fact, to analyze further, the way it works, I should hear it speak twice, one after another: the one before the loop, and the one inside the loop, and only then there should be repeats after 30 seconds. Instead, I hear “Water leak…” at 30s intervals from the beginning, as long as the sensor is on.
The “No more water leak…” always works.

ScriptSpeakAndNotifyDevices:

sequence:
  - action: notify.mobile_app_cat_sgalaxyphone
    metadata: {}
    data:
      message: "{{message}}"
  - action: notify.mobile_app_m10_11
    metadata: {}
    data:
      message: "{{message}}"
  - action: notify.mobile_app_huapad
    metadata: {}
    data:
      message: "{{message}}"
    enabled: false
  - action: media_player.play_media
    target:
      entity_id: media_player.espvoice2_espvoice2_media_player
    data:
      media_content_id: >-
        media-source://tts/tts.piper?message="{{ message
        }}"&language=en_GB&voice=en_GB-alba-medium
      media_content_type: provider
    metadata:
      title: "{{ message }}"
      thumbnail: https://brands.home-assistant.io/_/tts/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: >-
            media-source://tts/tts.piper?message="{{ message
            }}"&language=en_GB&voice=en_GB-alba-medium
    enabled: false
    alias: >-
      Play {{ message }} on ESPvoice2 ESPvoice2 Media Player, this only works
      sometimes
  - action: tts.speak
    target:
      entity_id: tts.piper
    data:
      cache: true
      media_player_entity_id: media_player.cat_s_bedroom_speaker
      message: "{{message}}"
  - action: tts.speak
    target:
      entity_id: tts.piper
    data:
      cache: true
      media_player_entity_id: media_player.espvoice2_espvoice2_media_player
      message: "{{ message }}"
alias: ScriptSpeakAndNotifyDevices
description: ""
fields:
  message:
    selector:
      text: null
    name: Message
    default: Unknown notification.

SpeakMessage:

sequence:
  - action: media_player.play_media
    target:
      entity_id: media_player.espvoice2_espvoice2_media_player
    data:
      media_content_id: >-
        media-source://tts/tts.piper?message="{{ message
        }}"&language=en_GB&voice=en_GB-alba-medium
      media_content_type: provider
    metadata:
      title: "{{ message }}"
      thumbnail: https://brands.home-assistant.io/_/tts/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: >-
            media-source://tts/tts.piper?message="{{ message
            }}"&language=en_GB&voice=en_GB-alba-medium
    enabled: false
    alias: >-
      Play {{ message }} on ESPvoice2 ESPvoice2 Media Player, this only works
      sometimes
  - action: tts.speak
    target:
      entity_id: tts.piper
    data:
      cache: true
      media_player_entity_id: media_player.cat_s_bedroom_speaker
      message: "{{message}}"
  - action: tts.speak
    target:
      entity_id: tts.piper
    data:
      cache: true
      media_player_entity_id: media_player.espvoice2_espvoice2_media_player
      message: "{{ message }}"
alias: SpeakMessage
description: ""
fields:
  message:
    selector:
      text: null
    name: Message
    default: Unknown notification.
    required: true

Hold on. The following two statements seem to be in tension with one another:

Can you clarify what exactly you are observing, and whether either scenario actually works the way you expect it to?

I don’t know how to say it differently. Neither works as “theoretically” expected, but if the speak/notify before the loop is enabled then the result is speak every 30 seconds, EVEN THOUGH there should be 2 speaks at the beginning: the one before the loop and the first inside loop.

The play media TTS calls don’t wait for the TTS to complete (at least in the Sonos integration); so doing two in a row just causes the second one to play; because the first one will play for 1ms, before getting replaced by the second one. Change the text and hear what happens.

What I do is have an automation the reads off an event queue and it then sends the announcement to the speaker; delays for 10 seconds then gets the next one. All my other automations send events to this automation to have stuff announced.

Thanks Pete,
That may be so, but why doesn’t it speak if I disable the speak before the loop?

AND it gets worse.
It feels like Home Assistant keeps something about deleted automations somewhere, or else I can’t explain the following.
I was testing the 2 ways above in “ZigIkeaWater1-Wet” and “ZigIkeaWater2-Wet”. the 2 was the one that didn’t work.
Well, after I got 1 to work, I deleted “ZigIkeaWater2-Wet”, duplicated “ZigIkeaWater1-Wet”, edited the "1"s into "2"s, so it became the new “ZigIkeaWater2-Wet”. Regardless, 2 doesn’t work.
I know the duplication should work because I also made “ZigIkeaWater3-Wet” and “ZigIkeaWater4-Wet” which work fine.
So what can I think now?

Yesterday my Home Assistant died.
I restored a backup on a new system and it seems (only tested once for now) that the problem is gone.
I don’t know how but I suspect disk problems for my strange happenings, so thank youse all for trying to help.