Dynamic delay in script?

Hi all,
For my morning routine script I have configured google TTS to tell me the weather, traffic info and some other small stuff, then HASS turn on my radio. I currently use a delay to make sure that the radio turns on after the TTS is finished. However, as the content in my TTS change daily, sometimes the delay before the radio turn on is too long, sometimes too short.

Is there a way to delay it programatically until the previous action is finished instead of a fixed number of seconds ?

Yes, that’s possible. Take a look at this thread which implements just that for TTS and SONOS.

It boils down to something like this:

- delay: >-
    {% set duration = states.media_player[where].attributes.media_duration %}
    {% if duration > 0 %}
      {% set duration = duration - 1 %}
    {% endif %}
    {% set seconds = duration % 60 %}
    {% set minutes = (duration / 60)|int % 60 %}
    {% set hours = (duration / 3600)|int %}
    {{ "%02i:%02i:%02i"|format(hours, minutes, seconds)}}

neat ! thank you very much !

Couldn’t the wait_template just be used to wait for the TTS to finish?