How do I pass random speech using my TTS script

Hi there,

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 }}

How do I pass ransom speech

Asking for a ransom is illegal in most countries :joy:

2 Likes

That is what I thought as well :joy:

1 Like

Good spotting!!! :grin::grin::grin::grin: Fixed now

Thank you but that advanced stuff is a bit beyond me. Could you explain a little further?

Turns out my issue is actually related to the apostrophe in the text and nothing more. :roll_eyes:

"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".

Do you really need the " there?
Is that read out?

As I see it, the reason is that you use both quotations in the sentence. If you only used one then it would work fine.

"I've stopped spinning and I'm a bit dizzy. Time to hang".

Because the dot is after the " the sentence is read as:

'"I've stopped spinning and I'm a bit dizzy. Time to hang".'

And that means you use both quotations " and '.

If you instead make the sentence:

"I've stopped spinning and I'm a bit dizzy. Time to hang."

Now the dot is inside the " " and because of that the string should work.

Sorry, that’s just my punctuation madness. Obviously the full stop is not necessary and was just a typo.

I’m not following exactly what the problem is here apart from something with the quotation marks but…

In the example above you could (maybe even should as it is a template) do this:

  - service: script.ghm_tts
    data_template:
      tts: >
        {{ ["OK", "Sure", "If you insist", "Done"] | random }}
      volume: 0.5

Or am I barking up completely the wrong tree?

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.