Say a random phraze

I have been thru a lot of very old responses but none are up to date.
I am just trying to say a random phraze via a script.
I am a novice at YAML so I only do scripts and automations. I use the visual editor or jump into the yaml edit ( not Studio Code Server as it never looks the same on my screen with samples people give)

I know the scippting has changed a lot but I need a 2025 solution as I think this would be really cool.

I compiled some code into a script. It runs but does nothing.

sequence:
  - alias: Say it again Sam
    target:
      entity_id: script.google_home_resume
    data:
      variables:
        action:
          - alias: “Send TTS Message”
            service: tts.google_translate_say
            target:
              entity_id: media_player.spanish_hub
            data:
              message: >
                {{ [ "Hey there PERSON Welcome Home, during your stay, don't be
                a asshole!", "Sometimes you feel like a nut, Sometimes you
                don't, Sometimes you feel like PERSON, l o l  Welcome Back",
                "Hello PERSON, damn, I wonder, how many people, we can fit in
                this, tiny house?"

                   ] | random }}
              extra:
                volume: 0.4
    action: script.turn_on
alias: aa random speech i
description: ""

I don’t even get some of the services, action, targets in the above code.

Can anyone help get this small script working, then I can have a lot of fun replicating it.

Developer Tools - Actions.
Select tts, the rest is done with the mouse.
Insert the template expression at the end, after that you will be switched to yaml and you will get the required code. Just paste it into a new script.

Nothing in the posted script is out of date… though some of it looks fishy.

I’ve never tried it so I could be wrong, but I don’t think passing an entire action object as a variable has never been a thing.

What do you mean?

Please follow Questions Guideline #8 and clearly describe what you are trying to achieve.

You may actually find it easier to dip your toe into templates and define your random expression separately. For example, this generates a random phrase to insert at the beginning of a TTS statement. It changes once a minute:

  - trigger:
      platform: time_pattern
      minutes: /1
    sensor:
      - name: Starter phrase
        unique_id: d08142bc-4f58-41fe-8588-244cc97f0f6b
        state: >
          {{ [
          "Well.",
          "",
          "Okay.",
          "",
          "Right. well.",
          "",
          "So.",
          "",
          "Okay. So.",
          ""
          "Okay then.",
          ""
          ] | random }}

Half the “phrases” are empty strings, so the starter phrase is only sometimes heard (making it even more random).

This makes the script very simple:

action: tts.picotts_say
data:
  cache: false
  entity_id: media_player.living_room
  message: "{{ states('sensor.starter_phrase') }} This is a test"

I copied this into my Studio Code Servers >> Sensors.yaml file

And i get the following errors

Plus it fails the Configuration check.

Am I putting this into the right place or do I need to change the code?

And where do I put. I am assuming it is a script.

Except what is tts.picotts_say as I can only find tts.speak?

action: tts.picotts_say
```data:
  cache: false
  entity_id: media_player.living_room
  message: "{{ states('sensor.starter_phrase') }} This is a test"

On my system, in the configuration file I have

sensor: !include sensors.yaml
template: !include templates.yaml

The sensors defined in sensors.yaml are all from integrations and require a platform. The template sensors in templates.yaml don’t.

You are correct. This is just an example - Pico TTS is a local TTS integration. The important bit is the message: line.

I already have those two lines in my Configuration.yaml file.

It is the formatting erros I am trying to address.

I can get the following code to have no formatting errors, but it has a configuration error from Developer tools

Configuration warnings

Platform error ‘sensor’ from integration ‘time_pattern’ - Integration ‘time_pattern’ not found.

Is there a differenet way of formatting the script?


  - platform: time_pattern
    name: Starter phrase
    unique_id: d08142bc-4f58-41fe-8588-244cc97f0f6b
    state: >
      {{ [
      "Well.",
      "",
      "Okay.",
      "",
      "Right. well.",
      "",
      "So.",
      "",
      "Okay. So.",
      ""
      "Okay then.",
      ""
      ] | random }}

So how can I progress as your code for the sensor does not work in Studio Code Server.

Has the formatting changed over the different versiuons?

You are getting the error because template sensors do not have a platform - if you have both sensors.yaml and templates.yaml in your configuration, the code I posted should go in templates.yaml.

I do have templates.yaml and sensors.yaml as various youtube videos have recommended this.

I copied the code into the templates.yaml - i still get errors.

Any suggestions or does the code need changing?

I will read that template and watch a video I found on creating templates.

I managed to get this script into the templat.yaml to be error free

        - trigger: time_pattern
          minutes: /1
        sensor:
          - name: Starter phrase
            unique_id: d08142bc-4f58-41fe-8588-244cc97f0f6b
            state: >
              {{ [
              "Well.",
              "",
              "Okay.",
              "",
              "Right. well.",
              "",
              "So.",
              "",
              "Okay. So.",
              ""
              "Okay then.",
              ""
              ] | random }}

I added this into a script. But I get the error tts.google_en_com cannot be found?

And it does nothing but HA will configure ok in Developer tools.

Can you suggest anything in either script I need to change to get this to work?


sequence:
  - action: tts.google_en_com
    data:
      cache: false
      entity_id: media_player.spanish_hub
      message: "{{ states('sensor.starter_phrase') }} This is a test"
alias: aa Play it again Sam
description: ""

Well i have gotten closer with this script which looks similar to yours. The only problem is the sensor.starter-prrase is announced as unknown by google.

Betweem this script and the template I documented earlier, can you spot a problem?

sequence:
  - action: tts.speak
    target:
      entity_id: tts.google_en_com
    data:
      cache: false
      media_player_entity_id: media_player.spanish_hub
      message: "{{ states('sensor.starter_phrase') }} This is a test"
alias: aa Google speaks again
description: ""

Finally got something basic to work

But I would prefer to get the code linked to the template if you can assist

sequence:
  - action: tts.speak
    target:
      entity_id: tts.google_en_com
    data:
      cache: false
      media_player_entity_id: media_player.spanish_hub
      message: '{{ ["1", "2", "3", "4", "5"] | random }}'
alias: aa Google speaks again (Duplicate)
description: ""