Sonos Notifcation Automation not playing tts

Hi,

I’m attempting to make a automation that will snapshot the sonos queue, play a tts message and then resume the music when the laundry has finished. I think this should be simple but the music will stop, no notification is played and then the music does not resume. If I complete each step manually by calling services it works as expect but does not work via the automation. Hoping someone can help.

id: '1622989376151'
alias: Laundry Notification
description: ''
trigger:
  - platform: state
    entity_id: sensor.washer_washer_machine_state
    from: run
    to: stop
condition: []
action:
  - service: sonos.snapshot
    data:
      entity_id: media_player.office
  - service: tts.google_translate_say
    data:
      entity_id: media_player.office
      message: Test
  - service: sonos.restore
    data:
      entity_id: media_player.office
mode: single

I think this error log might be related.

  File "/usr/src/homeassistant/homeassistant/core.py", line 392, in async_create_task
    task: asyncio.tasks.Task = self.loop.create_task(target)
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 431, in create_task
    task = tasks.Task(coro, loop=self, name=name)
TypeError: a coroutine was expected, got None

Just chiming in here. Same issue after upgrading from 2021.5 to 2021.6.
This is the relevant log in my case:

Logger: homeassistant.components.automation.mattis_fixar_frukost
Source: core.py:392
Integration: Automation (documentation, issues)
First occurred: 05:32:58 (6 occurrences)
Last logged: 05:46:52

Mattis fixar frukost: Error executing script. Unexpected error for call_service at pos 2: a coroutine was expected, got None
While executing automation automation.mattis_fixar_frukost
Mattis fixar frukost: Error executing script. Unexpected error for call_service at pos 3: a coroutine was expected, got None
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 368, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 571, in _async_call_service_step
    await self._async_run_long_action(service_task)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 532, in _async_run_long_action
    long_task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 278, in service_handler
    await script_entity.async_turn_on(
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 382, in async_turn_on
    await coro
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 404, in _async_run
    return await self.script.async_run(variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1216, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 350, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 368, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 568, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 750, in check_permissions
    return await service_handler(call)
  File "/usr/src/homeassistant/homeassistant/components/sonos/media_player.py", line 172, in async_service_handle
    await SonosSpeaker.restore_multi(
  File "/usr/src/homeassistant/homeassistant/components/sonos/speaker.py", line 802, in restore_multi
    groups = await hass.async_add_executor_job(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/sonos/speaker.py", line 767, in _restore_groups
    hass.async_create_task(speaker.soco.pause())
  File "/usr/src/homeassistant/homeassistant/core.py", line 392, in async_create_task
    task: asyncio.tasks.Task = self.loop.create_task(target)
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 431, in create_task
    task = tasks.Task(coro, loop=self, name=name)
TypeError: a coroutine was expected, got None

The same “a coroutine was expected, got None” here as well

This is fixed in Fix Sonos restore calls by jjlawren · Pull Request #51565 · home-assistant/core · GitHub, hopefully will make it to 2021.6.3.

1 Like

I figured it out, I had to add some delays to the automation.

Is anyone still else having issues getting the notification to play when part of an automation?

The snapshot and restore is working however the tts message fails to play.

Hi there!

Smart move with the delay’s

I have theoretical solution to your issue, but first a simple try

Simple Try:

Add a 10 second delay before and after the tts is suppose to play, if that solves the issue then sick! Work on shortening the delays

If it didn’t work,

Theoretical Solution:

  1. Use the supervisor Visual Studio addon to clear the files in the tts cache folder, it should just be called tts (Note: don’t delete the folder, just the files in the folder)

  2. Use the developer tools to call your tts.google_translate_say on the speaker media_player.office with the words This is a test (It doesn’t have to be those words but it makes it easier)

Then check if it worked, if it did sick!

If not,

1b. Check that the tts folder has a new file is it with some long odd name, if it has a new file then its not a issue with making the audio file. (Also download the file and play it to verify the file has sound in it, otherwise your just playing silence which means there’s a problem with the tts)

Assuming you have the audio file downloaded and its is playable, Continue bellow

1.c Save the audio file to your media folder as test.mp3 to the path of media/test.mp3 then use the code snippet bellow to play it in the automation

service: media_player.play_media
data:
  media_content_type: music
  media_content_id: >-
    media-source://media_source/local/test.mp3
target:
  entity_id: media_player.office

If that didn’t work I don’t know what to do. But, did you make changes to your external / internal url? that gave me issues with something else a while back but was fixed when I set them correctly

Confirmed fixed in 21.6.3. Impressive. Reported and fixed in less than 7 days. Salute to the Devs.

2 Likes

Hi @nairb,
It’s still silent to me when I try to send TTS command to my Sonos speakers :frowning:
I see in SONOS APP that the speaker try to play a MP3, but it don’t provide any sound at all…

Here is the script I used as test:

alias: Sonos TTS script
sequence:
    - delay:
      seconds: 4
    - service: tts.google_translate_say
    data:
      entity_id: media_player.salon
      language: fr
      message: Alarme activée
mode: single

And you can find bellow my tts configuration.yaml part:

tts:
  - platform: google_translate
    base_url: https://mars.mydomain.com:8123```

In Sonos APP I see a quick message that say the connexion with HA has been lost… I don’t understand why…

Can you share your working code ?