Passing text from automation to TTS in script

Apologies for another noob question. My HA (docker on qnap) has been up for about a week, and I’ve just installed my first 4 Shelly devices and more or less have them doing what I want - but all in Automatons with a phenomenal amount of repetition in the code. It already starting to get a bit laggy.

So following advice on the forum, I’m trying to break out the repeated bits into scripts.

The first target was to move out all the TTS calls as there are several pre- and post- TTS steps to get the Sonos speaker ready. I’ve tried about four different approaches (suggested in various forum posts) so far without success. Yaml is not my native tounge, so while I can tweak it once the basic structure is there - I am still heavily reliant on the visual editor to create it in the first place.

The most recent attempt was to have a set-up script, execute the TTS in the automation, then run the clean-up script afterwards…

alias: set_up_study
sequence:
  - service: sonos.snapshot
    data:
      entity_id: media_player.study
  - service: sonos.unjoin
    target:
      entity_id: media_player.study
  - service: media_player.volume_set
    target:
      entity_id: media_player.study
    data:
      volume_level: 0.5
mode: single
alias: clean_up_study
sequence:
  - wait_for_trigger:
      - platform: state
        entity_id: media_player.study
        from: playing
        to: paused
  - service: sonos.restore
mode: single

While this does sort of work for the TTS, the status quo of the media player that is supposed to be stored by the sonos.snapshot service, does not survive to the second script so the sonos.restore services does not work.

Ideally, I would like to write the media player target device, and the text to be processed by the TTS command both into helper variables and then have the script read the relevant data directly from them. That way I could use a single script that could be called for every TTS task. I have attempted many lines trying to achieve that, but I still don’t grasp the syntax - and get perpetual errors when I try to do things in a text editor - and there is no option to do such things in the visual editor (so far as I can tell).

action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.study
      message: {{ input_text.text_to_speak }}
      message: ' input_text.text.to.speak '
      message: '' input_text.text_to_speak ''
      message: {{ states( 'input_text.text_to_speak' ) }} 
mode: single

… these are sample message syntax formats I have tried in a text editor, but the system would not allow a HA restart with any of them in the scripts.yalm file.

I’ve run out of annual leave now - so I’ll have a lot less time going forward to experiment with permutations. What have I done wrong?

I use another tts service, but a templated message has to be wrapped:


message: "{{ states( 'input_text.text_to_speak' ) }}"

Or use


message: |
  {{ states( 'input_text.text_to_speak' ) }} 

1 Like

It’s so easy when you know how. :blush: Thank you @pedolsky

I’m trying to do something similar with this code in an action but it doesn’t work, please help

service: tts.cloud_say
data:
  cache: false
  entity_id: media_player.vardagsrum
  message: " {{ states('input_text.tts_send_out') }} "