Trying out the new Watson TTS Component

I am playing with the new Watson TTS component and rather than write a script I wanted to see if I could get Watson to interact through my Sonos setup using Node-Red.

Here is what I have so far: https://pastebin.com/Yr3WGnym

It seems to interact with the selected Sonos speaker, playback of existing content stops but I and not getting any speech.

Has anyone else tried TTS through HA - am I better off just bypassing HA for this and going direct from Node-Red to TTS?

Try this script, it TTS and resume playback after:

sonos_say:
  sequence:
  - service: sonos.snapshot
    data_template:
      entity_id: media_player.sonos_speaker
  - service: sonos.unjoin
    data_template:
      entity_id: media_player.sonos_speaker
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.sonos_speaker
      volume_level: "{{ volume }}"
  - service: tts.watson_tts_say
    data_template:
      entity_id: media_player.sonos_speaker
      message: "{{ message }}"
  - delay: '00:00:01'
  - delay: >-
      {% set duration = states.media_player.sonos_speaker.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 %}
      {{ [hours, minutes, seconds]|join(':') }}
  - service: sonos.restore
    data_template:
      entity_id: media_player.sonos_speaker

Thats pretty much what this flow is doing - its sort of based on various existing TTS scripts that are on the forums.

Watson TTS doesn’t work for me at all. Can anyone shed some light?

  - platform: watson_tts
    watson_apikey: !secret watson_tts_api_key
    voice: en-GB_KateVoice

Proof I have an api :wink:
[from the BM Cloud site]


But I get this ‘Access is denied’ error,

ibm_cloud_sdk_core.api_exception.ApiException: Error: Unauthorized: Access is denied due to invalid credentials, Code: 401

Here is the full error log

2019-06-23 10:28:03 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/tts/__init__.py", line 143, in async_say_handle
    options=options
  File "/usr/src/homeassistant/homeassistant/components/tts/__init__.py", line 312, in async_get_url
    engine, key, message, use_cache, language, options)
  File "/usr/src/homeassistant/homeassistant/components/tts/__init__.py", line 324, in async_get_tts_audio
    message, language, options)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/watson_tts/tts.py", line 134, in get_tts_audio
    voice=self.default_voice).get_result()
  File "/usr/local/lib/python3.7/site-packages/ibm_watson/text_to_speech_v1.py", line 294, in synthesize
    accept_json=False)
  File "/usr/local/lib/python3.7/site-packages/ibm_cloud_sdk_core/base_service.py", line 413, in request
    raise ApiException(response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Unauthorized: Access is denied due to invalid credentials, Code: 401

I read something somewhere about using a different regional URL - I can’t remember where but I think logins may be geographical.

Mine is not working either.

Ahh… yes the docs mention a URL as a configuration variable. There is no indication of what it is or that it is required though…

Look on your Watson TTS dashboard and it does give you a URL.
I’ve added mine to my config but don’t have time to restart and try it out now.

If this is the solution the docs are seriously deficient. In which case I’ll suggest an improvement.

Let me know if you try this and if it works.

EDIT: It worked for me but so far in the very few tests I’ve done the Watson voice is a little underwhelming.

I’ve found my problem I am running under docker and the watson component was using the internal address of the docker container to pass the stream to sonos. Sonos could not route to that address. I have used the http: comoponent to route all external calls through the traefik load balance using https:// . I can now play watson tts.

I am trying to use a wait until node to check if the media player has finished playing - this does not seem to be working yet.