Help with sonos TTS in automations

Hi, i tried to follow the documentation for Sonos TTS using the below but it does not do anything…

please Help.
configurations.yaml has:

script:
# > SONOS TTS
   say:
     alias: SONOS TTS
     sequence:
       - service: media_player.sonos_snapshot
         data_template:
           entity_id: "{{ where }}"
       - service: tts.google_say
         data_template:
           entity_id: "{{ where }}"
           message: "{{ what }}"
       # Add a delay in a length which you think fits best for all the things you want to say over TTS
       - delay: '00:00:07'
       - service: media_player.sonos_restore
         data_template:
           entity_id: "{{ where }}"

automations.yaml has:

- alias: test speaking'test'
  trigger:
    - platform: time
      at: '21:41:00'
  action:   
    - service: script.sonos_say
      data:
        sonos_entity: media_player.living_room
        volume: 0.5
        message: 'TTS test'
        delay: '00:00:05'


thanks,

Have a look at this thread to get an idea on how it can be implemented.

unfortunately for me thats where i copied the configs from…

In the automation you are calling script.sonos_say but your script actually is called say. And you are not using where and what in your automation.

I recommend to try these as reference:

and

thanks, i am still working on getting this “Working”… been short on time.

it looks like my formatting is screwed up… i get the below error:

Jul 25 20:02:40 hassbian systemd[1]: Started Home Assistant for homeassistant.
Jul 25 20:02:45 hassbian hass[10517]: 2017-07-25 20:02:45 ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
Jul 25 20:02:45 hassbian hass[10517]: in “/home/homeassistant/.homeassistant/scripts.yaml”, line 2, column 3
Jul 25 20:02:45 hassbian hass[10517]: expected , but found ‘-’
Jul 25 20:02:45 hassbian hass[10517]: in “/home/homeassistant/.homeassistant/scripts.yaml”, line 7, column 3
Jul 25 20:02:45 hassbian hass[10517]: 2017-07-25 20:02:45 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
Jul 25 20:02:45 hassbian hass[10517]: in “/home/homeassistant/.homeassistant/scripts.yaml”, line 2, column 3
Jul 25 20:02:45 hassbian hass[10517]: expected , but found ‘-’
Jul 25 20:02:45 hassbian hass[10517]: in “/home/homeassistant/.homeassistant/scripts.yaml”, line 7, column 3
Jul 25 20:02:45 hassbian hass[10517]: Config directory: /home/homeassistant/.homeassistant

Well, if you’d paste that part of your config and use some propper markdown it would be easier to help you. :slight_smile:

sure, it is a copy paste of what you had posted…

pi@hassbian:/home/homeassistant/.homeassistant $ cat scripts.yaml

  sonos_tts:
    sequence:
  #  alias: Sonos Text To Speech
    - service: media_player.sonos_snapshot
      data_template:
      entity_id: "{{ 'media_player.' ~ where }}"
    - service: tts.google_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: media_player.sonos_restore
      data_template:
      entity_id: "{{ 'media_player.' ~ where }}"




The formatting looks off a little.
Try this example: SONOS TTS Script

thanks, that got past the errors… still not hearing anything from sonos…

when i look at the logbook, i do see the automation getting triggered.

test speaking has been triggered, after the switch being turned on.

heres what i have in my automations.yaml

- alias: test speaking
  trigger:
    platform: state
    from: 'off'
    to: 'on'
    entity_id: switch.dinning_light_switch_2_0
  action:
    service: script.turn_on
    entity_id: script.sonos_say
    data:
      variables:
      where: 'media_player.living_room'
     # volume: 0.5
      what: 'this works'
      #delay: 00:00:05

heres the scripts.yaml copy of the example from above just changed the entitiy id name:

pi@hassbian:/home/homeassistant/.homeassistant $ cat scripts.yaml

  sonos_say:
    alias: "Sonos TTS script"
    sequence:
      - service: media_player.sonos_snapshot
        data_template:
          entity_id: "{{ sonos_entity|default('media_player.living_room') }}"
      - service: media_player.sonos_unjoin
        data_template:
          entity_id: "{{ sonos_entity|default('media_player.living_room') }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ sonos_entity|default('media_player.living_room') }}"
          volume_level: "{{ volume|default(0.5) }}"
      - service: tts.google_say
        data_template:
          entity_id: "{{ sonos_entity|default('media_player.living_room') }}"
          message: "{{ message }}"
      - delay: "{{ delay|default('00:00:00') }}"
      - wait_template: "{{ is_state(sonos_entity|default('media_player.living_room'), 'playing') }}"
        timeout: '00:00:05'
      - wait_template: "{{ not is_state(sonos_entity|default('media_player.living_room'), 'playing') }}"
        timeout: '00:02:00'
      - service: media_player.sonos_restore
        data_template:
          entity_id: "{{ sonos_entity|default('media_player.living_room') }}"

Maybe let’s try something simpler first, like this:

- alias: test speaking
  trigger:
    platform: state
    from: 'off'
    to: 'on'
    entity_id: switch.dinning_light_switch_2_0
  action:
    - service: media_player.sonos_snapshot
      data_template:
        entity_id: media_player.living_room
    - service: tts.google_say
      data_template:
        entity_id: media_player.living_room
        message: "This works now."
    - delay: "00:00:{{ states.media_player.wohnzimmer.attributes.media_duration | int }}"
    - service: media_player.sonos_restore
      data_template:
        entity_id: media_player.living_room

this works! thanks Tobi.

so what would the reason be for me to use the script ? I’ll try to go thru both line/line to see if i can identify the problem… or feel free to enlighten me.

The script wraps a lot of things and makes it reusable. I actually don’t use that script at the moment. Unfortunately I don’t know why it breaks. Maybe post in the other thread.

How would I implement this for each switch, repeat config?

This is what the script is for. It wraps most of the stuff.

Ok, I guess the next step is trying to figure out using scripts.

Thanks.

So, I created the sonos script and it works with the automation.

Is there anyway to specify the switch/light entity_id like the player so that the automation triggers for all switches/lights?
“entity_id: “{{ ‘media_player.’ ~ where }}””

Hey, the automation’s run as configured, but throw the following error after each, any ideas?

2017-08-02 23:28:49 ERROR (MainThread) [homeassistant.components.tts] Can't write 48b4c312a17150ddfb23df9d2e8d278ae3fdbbff_en-us_250172662_amazon_polly.mp3