SONOS TTS Script

As is often the case, HA develops and scripts described by users no longer work due to syntactic changes. So here is a working version (Oct 2019) of the great script developed by @domlen and shared with us a couple of years ago.

test_tts:
  alias: Test for TTS
  sequence:
    - service: script.turn_on
      entity_id: script.say
      data:
        variables:
          where: 'office'
          what: 'Test.'


say:
  alias: Sonos Text To Speech
  sequence:
  - service: sonos.snapshot
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"
  - service: tts.google_translate_say
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"
      message: "{{ what }}"
  - delay:
      seconds: 1
  - 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 %}
      {{ [hours, minutes, seconds]|join(':') }}
  - service: sonos.restore
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"


3 Likes