How do I make a script wait until TTS is done speaking?

I’m trying to replicate my Google Home “Bedtime” script in HA, which reads out the weather, turns off lights and locks doors, and then plays some white noise.

Most of this is no problem, except that I can’t seem to figure out how to get the script to wait for TTS to complete before proceeding. As this is a “forecast” with an indeterminate amount of text to read, using delay seems like a bad solution. I tried moving the TTS portion to its own script, call it from the “Bedtime Routine” script, but as this new script also immediately turns OFF while the TTS is still active, this option also just skips the TTS altogether.

Here’s my script(s) right now.

nightly_forecast_tts:
  alias: Nightly Forecast TTS
  sequence:
  - service: tts.cloud_say
    data:
      entity_id: media_player.master_bedroom_soundmachine_mpd
      message: Turning off lights and locking doors. {{states.sensor.forecast.state}}
      options:
        gender: female
      language: en-GB
  mode: restart
  icon: mdi:text-to-speech

master_bedroom_sleep:
  alias: Master Bedroom Sleep
  sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0.4
    entity_id: media_player.master_bedroom_soundmachine
  - service: script.nightly_forecast_tts
  - service: homeassistant.turn_off
    data:
      entity_id:
      - light.back_entry_patio_lights
      - switch.front_porch_sconces
      - light.front_foyer_main_lights
      - light.kitchen_main_lights
      - light.master_bedroom_brendan_table_lamp
      - switch.rear_entry_lights_6
  - service: script.master_bedroom_sleep_noise
  mode: restart
  icon: mdi:sleep

master_bedroom_sleep_noise:
  alias: Master Bedroom Sleep Noise
  sequence:
  - service: media_player.select_source
    data:
      source: Sleep
    entity_id: media_player.master_bedroom_soundmachine
  mode: restart

Use the wait for trigger and wait for the media player to become idle from playing.

Thanks @tom_l. I tried it with both wait_template and wait trigger, and both seem to accomplish the goal. As I’m using an mpd instance on top of my volumio player, the state changes from ‘playing’ to ‘off’ instead of ‘idle’.

Is there a random “wait time” that can be implemented? If I have multiple scripts that trigger and all want to say something?

Use queued mode instead of restart and they will play one after the other.

1 Like

Can I modify the yaml of a blueprint for queued mode? or do I just edit automations after creation?

No idea sorry, I don’t use blueprints.