I have a script that plays TTS on four Google Homes but has the ability to choose only speakers that aren’t playing music. Works well but now I want to start using | random to spice up the replies. I can get random working just fine in a test script when using the following:
- service: tts.cloud_say
entity_id: media_player.lounge_home
data_template:
message: >
'{{ ["OK", "Sure", "If you insist", "Done"] | random }}'
language: en-IE
However, my main script is expecting just the text being said in quotes e.g. '{{tts}}'
- service: tts.cloud_say
data_template:
entity_id: >-
{%- if states('media_player.kitchen_home') != 'playing' and
states ('media_player.lounge_home') != 'playing' and
states ('media_player.bathroom_speaker') != 'playing' and
states ('media_player.ensuite_speaker') != 'playing' %}
media_player.tts_homes
{%- else %}
{%- set players = ['media_player.kitchen_home', 'media_player.lounge_home', 'media_player.bathroom_speaker', 'media_player.ensuite_speaker'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- endif %}
message: '{{tts}}'
language: en-IE
Because the main script already has '{{ }}', all I have to do is supply something like "say this" and it passes that to the script but that is not | random friendly.
Can someone advise how to code it as this is obviously not formatted correctly even though in theory that’s what the script is expecting?
- service: script.ghm_tts
data_template:
tts: ["OK", "Sure", "If you insist", "Done"] | random
volume: 0.5
I am not currently able to check if this is possible, but maybe assigning it to a variable first could work, like {% set tts_text = ([“OK”, “Sure”, “If you insist”, “Done”] | random) %} and then in the next line use {{ tts_text }}
Turns out my issue is actually related to the apostrophe in the text and nothing more.
"I've stopped spinning and I'm a bit dizzy. Time to hang".
Turns out you can’t type it that way even though it works in the developer tools. It appears you have to type it this way to work unless someone has a better suggestion.
"I''ve stopped spinning and I''m a bit dizzy. Time to hang".
My original issue is that VSCode was giving me YAML errors when I tried using text that had apostrophes. I incorrectly suspected it was due to the formatting of placeholder tts when it was just the apostrophes. Now that I’ve used double apostrophes, there are no code errors and the text is spoken as intended.