How to create multiple phrases to send at random to TTS?

Also looking for a solution to this.
I want to pass the outputs of some blueprinted automations to a notification so want to mixup the messages a bit.

Yes I did!
I have make several automations. They are using TTS with random phrases and values from sensors.

Here are some examples of yaml code:

  alias: HOME PIR TTS at Attic
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d000445d9ea
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.tts_attic_status
    state: 'off'
  action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.tts_attic_status
  - service: notify.alexa_media_attic
    data:
      data:
        type: tts
      message: '{{ [ ''The magic thing about home is that it feels good to leave,
        and it feels even better to come back'', ''Home sweet home!.'', ''Mi casa
        su casa.'', ''Home is where the WiFi connects automatically.''    ] | random
        }}'
  mode: single

And here is another one with variables and sensors

- id: '1609286769614'
  alias: HOME PIR TTS at Kitchen
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.tts_kitchen_status
    state: 'off'
  action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.tts_kitchen_status
  - service: notify.alexa_media_living_room
    data:
      data:
        type: tts
      message: "{% set tts_sensors = { 'temperature': states('sensor.temperature_outdoor'),\
        \ 'humidity': states('sensor.humidity_outdoor'), 'forecast': states('weather.spiti'),\
        \ 'washer': states('sensor.washer_state') } %} \n{% set tts_windows_status\
        \ = 'closed' %} {% if is_state('group.window_status', 'off') %} {% set tts_windows_status\
        \ = 'closed' %} {% else %} {% set tts_windows_status = 'open' %} {% endif\
        \ %}\n{{ [ 'Hello! What shall we eat tonight?', 'Hi! I was bored here all\
        \ day alone.The washer state is '+tts_sensors.washer+'.', 'Glad to have you\
        \ back. Hope you had a busy day.', 'Hello again. Its getting dark and the\
        \ temperature is pretty low. Consider turning on the heating.', 'Hello. You\
        \ are late and I was about to make you dinner.', 'Hello beautiful! The outdoor\
        \ humidity is ' +tts_sensors.humidity+'%. The windows are '+tts_windows_status+'.',\
        \ 'Good evening. Nice to have you back. The outdoor temperature is '+tts_sensors.temperature+'\
        \ Celsius.', 'Hello! A brief weather report. Its '+tts_sensors.forecast+'\
        \ and the temperature is '+tts_sensors.temperature+' Celsius'      ] | random\
        \ }}"
  mode: single


We just love it!
Hope it helps you!

2 Likes

Thanks so much for sharing that! Looks great!

Is there any doco on how all this works?

  message: "{% set tts_sensors = { 'temperature': states('sensor.temperature_outdoor'),\
        \ 'humidity': states('sensor.humidity_outdoor'), 'forecast': states('weather.spiti'),\
        \ 'washer': states('sensor.washer_state') } %} \n{% set tts_windows_status\
        \ = 'closed' %} {% if is_state('group.window_status', 'off') %} {% set tts_windows_status\
        \ = 'closed' %} {% else %} {% set tts_windows_status = 'open' %} {% endif\
        \ %}\n{{ [ 'Hello! What shall we eat tonight?', 'Hi! I was bored here all\
        \ day alone.The washer state is '+tts_sensors.washer+'.', 'Glad to have you\
        \ back. Hope you had a busy day.', 'Hello again. Its getting dark and the\
        \ temperature is pretty low. Consider turning on the heating.', 'Hello. You\
        \ are late and I was about to make you dinner.', 'Hello beautiful! The outdoor\
        \ humidity is ' +tts_sensors.humidity+'%. The windows are '+tts_windows_status+'.',\
        \ 'Good evening. Nice to have you back. The outdoor temperature is '+tts_sensors.temperature+'\
        \ Celsius.', 'Hello! A brief weather report. Its '+tts_sensors.forecast+'\
        \ and the temperature is '+tts_sensors.temperature+' Celsius'      ] | random\
        \ }}"

So I think I get some of this now

"{% set tts_sensors = { 'temperature': states('sensor.temperature_outdoor'),\
        \ 'humidity': states('sensor.humidity_outdoor'), 'forecast': states('weather.spiti'),\

So here, you set a kind of friendly name to call later in the automation? EG

'+tts_sensors.temperature+'

This part is saying call windows that are off, closed?

%} \n{% set tts_windows_status\
        \ = 'closed' %} {% if is_state('group.window_status', 'off') %} {% set tts_windows_status\
        \ = 'closed' %} {% else %} {% set tts_windows_status = 'open' %} {% endif\

I don’t really understand all the / what does that do?

Hello again. Its getting dark and the\
        \ temperature is pretty low. Consider turning on the heating.',

Don’t get this as well, how does it know its cold outside?

I know that’s a lot to ask HAHA sorry, if its just some doco, id appreciate it :slight_smile:

Thanks

This is also a slightly different thing that I’m trying to achieve. I know I’m on the right track. JI just have completely no idea of the syntax.

If anyone can help, would be amazing :slight_smile:

\ just mean move to next line but ignore in the script?

service: notify.alexa_media
data_template:
  target:
    - media_player.bedroom_echo_show
    - media_player.bathroom
    - media_player.kitchen
  data:
    type: announce
  message: {{% if is_state('binary_sensor.balcony_door_contact', 'open') and if is_state('binary_sensor.bedroom_window_contact', 'open') \
  \ %} Please close the balcony door and bedroom window? {%- else -%} {% if is_state('binary_sensor.bedroom_window_contact', 'open') \
  \ Please close the bedroom window} {%- else -%}{{% if is_state('binary_sensor.balcony_door_contact', 'open') \
  \ Please close the Balcony Door}{% endif %}

I will try to help but I am telling you that you will have to excuse my English.

Basically, based on the docs to call TTS service you need to:

data:
   type: tts
message: Your text to speech message

if you need to add sensors you need to do the following:

  - data:
      data:
        type: tts
      message: Attention. The baby room temperature is ( {{ states('sensor.temperature_baby_room_calibrated')
        }} Celsius)
    service: notify.alexa_media_living_room

So basically the main problem when you want to use the random function is that you can’t have

{{ }} within {{ }}

So this is why I declare the sensors before I call the random function.
Then inside the function I use the friendly names and I don’t have to repeat the {{ }}

Hope that make sense!
If not please someone that knows proper English to explain it a bit better :slight_smile:

1 Like

I appreciate you giving it a go! I think your english is better then you think lol :slight_smile:

I get what your saying, what I’m trying to is only mention the sensor if its open

  message: {{% if is_state('binary_sensor.balcony_door_contact', 'open') and if is_state('binary_sensor.bedroom_window_contact', 'open') \
  \ %} Please close the balcony door and bedroom window? {%- else -%} {% if is_state('binary_sensor.bedroom_window_contact', 'open') \
  \ Please close the bedroom window} {%- else -%}{{% if is_state('binary_sensor.balcony_door_contact', 'open') \
  \ Please close the Balcony Door}{% endif %}

If both sensors are open, then say “Please close the balcony door and bedroom window?”
else
if one sensor is open then say “Please close the bedroom window”
else
If other sensor is open say “Please close the Balcony Door”

From what i’ve read, you can do that. I just have no idea of the syntax?

that is exactly what that script is doing.

What really? I got it right LOL

Ill have to wait for a hot day to test :slight_smile:

The logic at you example is correct but the syntax is wrong and it will raise an error at logs when it runs.

The example bellow is correct and I have tested it. Use it as reference.

{% if is_state('binary_sensor.balcony_door_contact', 'open') and is_state('binary_sensor.bedroom_window_contact', 'open')%}
Please close the balcony door and bedroom window.
{%- endif -%} 
{% if is_state('binary_sensor.bedroom_window_contact', 'open') %}
Please close the bedroom window} 
{%- endif -%} 
{% if is_state('binary_sensor.balcony_door_contact', 'open') %}
Please close the Balcony Door}
{% endif %}

The only consideration that is missing is that when everything is closed(doors and windows), alexa will have nothing to say. Create a group for everything and then add the following:

{% if is_state ('group_everything'), 'closed') %}
everything is closed ... weldone

{% else %}

my code......

{% endif %}

I think you should add it to your scenario.
Maybe is not the best way to do it, but it works!

That’s brilliant thank you! Just on indenting ect, \ at the beginning and end of a line makes it ignore any line breaks?

Like so?


      message: \{% if is_state('binary_sensor.balcony_door_contact', 'open') and is_state('binary_sensor.bedroom_window_contact', 'open')%}\
               \Please close the balcony door and bedroom window.\
               \{%- endif -%} \
               \{% if is_state('binary_sensor.bedroom_window_contact', 'open') %}\
               \Please close the bedroom window} \
               \{%- endif -%} \
               \{% if is_state('binary_sensor.balcony_door_contact', 'open') %}\
               \Please close the Balcony Door}\
               \{% endif %}\

I love this little trick as well… But its fine, because if everything is closed… It just turns on the aircon. But Ill put that in the bank, im sure something like that will come in handy :slight_smile:

Ok, got home and tested it out and I can save it and execute it, but nothing happens.

Im guessing I have the indentation wrong somewhere?

alias: Close Doors
sequence:
  - service: notify.alexa_media
    data_template:
      target:
        - media_player.bedroom_echo_show
        - media_player.bathroom
        - media_player.kitchen
      data:
        type: announce
      message: >-
        {% if is_state('binary_sensor.balcony_door_contact', 'open') and
        is_state('binary_sensor.bedroom_window_contact', 'open')%}Please close
        the balcony door and bedroom window.{%- endif -%}{% if
        is_state('binary_sensor.bedroom_window_contact', 'open') %}Please close
        the bedroom window}{%- endif -%}{% if
        is_state('binary_sensor.balcony_door_contact', 'open') %}Please close
        the Balcony Door}{% endif %}
mode: single

It doesn’t happen anyhting if doors and windows are closed.
That’s the reason I adviced to add the else statement in case everything is closed.

If something is open and the TTS is not been executed, please paste the error from logs.

Thanks for helping out mate, got it working in the end with a slightly different approach :slight_smile:

service: notify.alexa_media
data_template:
  target:
    - media_player.bedroom_echo_show
    - media_player.bathroom
    - media_player.kitchen
  data:
    type: announce
    message: >
     {% set door = is_state('binary_sensor.balcony_door_contact', 'on') %}
     {% set window = is_state('binary_sensor.bedroom_window_contact', 'on') %}
     {% if door and window %}  Please close the balcony door and bedroom window?
     {% elif window %} Please close the bedroom window.
     {% elif door %} Please close the balcony door.
     {% else %} Everything is closed.
     {% endif %}
1 Like

Great! I am glad you solve it.
TTS is just great and since I was young, I was always amazed … :slight_smile:

Edit: Finally this does work, but not from the Dev tool, that throws the error. Works fine in the automation though.

So does anyone have any idea how I can get this to play messages sequentially, rather than randomly?

This doesn’t seem to work anymore. I keep getting this error:

Failed to call service tts/cloud_say. extra keys not allowed @ data['data_template']

If you want to do it in sequence, put the options in an input_select and say the one that is currently in it and then move the input_select to the next item ready for the next time it’s used.

Ah, of course! Great idea! Thanks @anon43302295! I’ll try that.

1 Like

Wow! Tons of great information on here. Hoping someone can assist on this topic and I apologize if I missed it through the threads. I too would like to set multiple phrases at random however I would like for it to read a variable as part of the messages.

At a high level, I have an automation that triggers based on presence for my family. Upon entering my neighborhood the automation kicks off several things like setting thermostats, lights etc and upon entering the front door and it closing it says “Welcome home, “variable”” where variable equals whoever’s presence triggered the automation.

What Im hoping to achieve is to change my simple message into a more complex one. Below is what I currently have but I cant seem to get it to work. Could somebody assist with where my error may be? Thanks so much for your help.

service: notify.alexa_media_kitchen
data:
  data:
    type: tts
  message: >
    {{ [ 'The magic thing about home is that it feels good to leave, and it
    feels even better to come back, Welcome home {{
    trigger.to_state.attributes.friendly_name }}!',
         'Welcome {{ trigger.to_state.attributes.friendly_name }}! to Home sweet home!.',
         'Mi casa su casa, Welcome, {{ trigger.to_state.attributes.friendly_name }}!',
         '{{ trigger.to_state.attributes.friendly_name }}! The Master of the domain, Keeper of this domain, Heir of this castle',
         'Welcome {{ trigger.to_state.attributes.friendly_name }}!, Whats Up.',
         'Welcome {{ trigger.to_state.attributes.friendly_name }}! Long time no see, Nice to see you!',
         'Welcome {{ trigger.to_state.attributes.friendly_name }}! Its been ages since I have seen you!',
         'Welcome {{ trigger.to_state.attributes.friendly_name }}! Where have you been hiding?',
         'Roll out the red carpet for {{ trigger.to_state.attributes.friendly_name }}!',
         'Home is where the WiFi connects automatically. Welcome, {{ trigger.to_state.attributes.friendly_name }}!', ] | random }}

Nice use of ChatGPT.

Can I have 20 random good morning phrases with each phrase surrounded by single quotes and separated by commas?

Sure, here you go:

'Good morning, beautiful!', 'Happy morning!', 'Up and at em!', 'Rise and shine!', 'Good day, sunshine!', 'Mornin', 'Good morning, world!', 'Good morning, handsome!', 'Morning, my love!', 'Good morning, sleepyhead!', 'Morning, sunshine!', 'Wakey wakey!', 'Good morning, friend!', 'Top of the morning to you!', 'Its a new day, make it count!', 'Good morning, champ!', 'Morning, boss!', ' Rise and grind!', 'Morning, tiger!'
2 Likes

Was able to solve this (with the help of a friend) by creating a helper text that updates upon arrival at home. I simplified it with just a single person so it inserts their name. The only problem I see at the moment is that it will only announce a single person. Would love to welcome multiple ppl at home. Posting here in hopes it helps someone else. New automation below:

alias: "Mode: I'm Back (kids home from school)"

description: ""

trigger:

  - platform: zone

    entity_id: person.Kid1_lastname

    zone: zone.home

    event: enter

    id: Kid1

condition: []

action:

  - choose:

      - conditions:

          - condition: trigger

            id: Kid1

        sequence:

          - service: input_text.set_value

            data:

              value: Kid1

            target:

              entity_id: input_text.arrivee

  - service: media_player.volume_set

    target:

      entity_id: media_player.kitchen

    data:

      volume_level: 0.6

  - service: notify.alexa_media_kitchen

    data:

      data:

        type: tts

      message: >

        {{ [ 'The magic thing about home is that it feels good to leave, and it

        feels even better to come back, Welcome home ' ~

        states('input_text.arrivee') ~ '!',

         'Welcome ' ~ states('input_text.arrivee') ~ '! to Home sweet home!.',

         'Mi casa su casa, Welcome, ' ~ states('input_text.arrivee') ~ '!',

         'Sometimes you feel like a nut, Sometimes you feel like, ' ~ states('input_text.arrivee') ~ '!, Welcome Back!',

         ''~ states('input_text.arrivee') ~'! The Master of the domain, Keeper of this domain, Heir of this castle.',

         'Welcome ' ~ states('input_text.arrivee') ~ '!, Whats Up.',

         'Welcome ' ~ states('input_text.arrivee') ~ '! Long time no see, Nice to see you!',

         'Welcome ' ~ states('input_text.arrivee') ~ '! Its been ages since I have seen you!',

         'Welcome ' ~ states('input_text.arrivee') ~ '! Where have you been hiding?',

         'Hello ' ~ states('input_text.arrivee') ~ '!, damn, I wonder, how many peopl, we can fit in this, tiny house?',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, A bank is a place that will lend you money, if you can prove that you do not need it',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, A computer once beat me at chess, but it was no match for me at kick boxing',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, Why does someone believe you when you say there are four billion stars, but check when you say the paint is wet?',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, Whenever I fill out an application, in the part that says, If an emergency, notify, I put doctor,  Whats my mother going to do?',

         'Roll out the red carpet for ' ~ states('input_text.arrivee') ~ '!',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, The voices in my head may not be real, but they have some good ideas',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, Crowded elevators smell different to midgets',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, Everyone has the right to be stupid, but you are abusing the privilege',

         'Welcome home ' ~ states('input_text.arrivee') ~ '!, If a turtle doesn’t have a shell, is he homeless or naked',

         'Home is where the WiFi connects automatically. Welcome, ' ~ states('input_text.arrivee') ~ '!' ] | random  }}

mode: single