Amazon Polly TTS fails on first iteration

Good afternoon,

I am running home assistant core 2024.3.1. My initial implementation was with HassOS on a Raspberry Pi 4. I am using various SONOS speakers as media players.

I am trying to run the following script, which is part of a much larger script, but I have narrowed my issue down to this piece

alias: Testing
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.den
    data:
      media_content_id: >-
        media-source://tts/amazon_polly?message= Good day sunshine. It is now
        {{now().strftime('%-H %M on %A %-d %B %Y')}}
      media_content_type: music
mode: single

When I run the script for the first time I get a message “Triggered Testing” but there is no output to the media player. Looking through the log files the following is generated

Logger: homeassistant
Source: components/tts/legacy.py:244
First occurred: 13:34:29 (1 occurrences)
Last logged: 13:34:29

Error doing job: Exception in callback SpeechManager._async_get_tts_audio.<locals>.handle_error(<Task cancell...nit__.py:691>>) at /usr/src/homeassistant/homeassistant/components/tts/__init__.py:757
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/components/tts/__init__.py", line 759, in handle_error
    if audio_task.exception():
       ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tts/__init__.py", line 697, in get_tts_data
    extension, data = await engine_instance.async_get_tts_audio(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tts/legacy.py", line 244, in async_get_tts_audio
    return await self.hass.async_add_executor_job(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError

Looking at the trace files it shows all is in order.

This is where it gets weird. If I run the same script again it works perfectly. And will continue running perfectly until I leave it for about 3 hours, when it will again not work for the first iteration, but will continue working after that.
As a test I added a notification immediately before the play media sequence, and the notification was created, but not the play media part. I have also added additional play media sequences before and after this piece, and they will execute correctly, but not this particular one.
I have also tried

alias: Testing
variables:
  sunrise: >-
    {% set ynow = now().strftime('%-H %M on %A %-d %B %Y') %} Good day sunshine. It is now {{ ynow }}
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.den
    data:
      media_content_id: >-
        media-source://tts/amazon_polly?message= {{ sunrise }}
      media_content_type: music
mode: single

with the same results.
For what its worth the actual issues lies in the now() call. The good day sunshine and the strftime() are there to make it more palatable.

I have the same response from any of my media players.

Please can someone help me figure out what is going on and why my script will not work properly on the first iteration.

Thank you,

Mark

This is not much help… but I do the same thing (Amazon Polly plus Sonos) like this:

  - service: tts.amazon_polly_say
    metadata: {}
    data:
      entity_id: media_player.bedroom
      message: It's {{ states('sensor.speaking_clock') }}
      cache: true
    enabled: true

sensor.speaking_clock is an elaborate if-then template to make him talk in colloquial phrases (“It’s half past four”). The TTS service mutes any music that’s playing. (I say “him” because I use the Brian voice - sounds like the butler’s been at the sherry. :grin:)

What is the state of the media_player before the first call to media_player.play_media?
If it is off, perhaps the first service call wakes it up, allowing the second call to play the notification?
If this is the case you could try adding the media_player.turn_on service call to your script.

I have the same response regardless of if the media player status is ‘paused’ or ‘playing’.

Thank you for this suggestion but the system then works the same as if you add announce: true to the sequence.
This will work but then the media player status is not changed, and I use the status change in a wait in a wait template before the next sequence is triggered.