TTS automation read from a file

Hello everyone, I’ve recently been working on my own washing machine, dryer and dishwasher automation that uses a script to announce via tts when devices are done and action is required.

I use a large number of random phrases to accomplish this so that I don’t get the same message over and over.

This has had the effect of increasing my scripts.yaml to be well over 300 lines and increasingly difficult to navigate.

What I’m hoping is possible:

create a script for TTS that calls a messages file to get the random phrases.

Something like this:


alias: Kitchen display notifications
sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0.6
    target:
      entity_id: media_player.kitchen_display
  - service: tts.cloud_say
    data:
      entity_id: media_player.kitchen_display
      language: en-AU
      options:
        gender: male
      message: "Message from a separate file"
  - wait_for_trigger:
      - platform: device
        device_id: 2d32ee35222fa2f43bd63b6a9d819269
        domain: media_player
        entity_id: media_player.kitchen_display
        type: idle
  - service: media_player.volume_set
    data:
      volume_level: 0.3
    target:
      entity_id: media_player.kitchen_display
mode: single
icon: mdi:speaker


Or if it’s possible to have the automation use a separate file to get the random phrases"


description: "test"
mode: single
trigger: []
condition: []
action:
  - service: script.kitchen_display_notifications
    data:
      message: "Message from separate file"

Any help with the above would be greatly appreciated.

How are you currently storing the phrases in the script? If you use lists, it’s fairly compact.

This is currently the solution i’m using. I’m after a way to minimize the amount of text in the automation and scripts files.

Thanks for the suggestion though

Here’s another way to do it. I’ve tested it and it works.

Create a file containing the TTS phrases and structure it as a YAML list. For example, assume we have a file called tts_phrases.yaml and it contents are:

- "Good night"
- "Sleep tight"
- "Nighty night"
- "Sweet dreams"

Create a variable in your script called phrases (or whatever you want to call it) and point it to the tts_phrases.yaml file using an !include directive. Reference the phrases variable in the tts.cloud_say service call.

alias: Kitchen display notifications
sequence:
  - variables:
      phrases: !include ./tts_phrases.yaml
  - service: media_player.volume_set
    data:
      volume_level: 0.6
    target:
      entity_id: media_player.kitchen_display
  - service: tts.cloud_say
    data:
      entity_id: media_player.kitchen_display
      language: en-AU
      options:
        gender: male
      message: "{{ phrases | random }}"
... etc ..

Warning

Don’t use the Script Editor.

Upon saving the script it replaces !include ./tts_phrases.yaml with null. I store manually created scripts (and automations) separate from the ones created by the Script (and Automation) editor.

Refer to the included file’s location correctly.

The path to tts_phrases.yaml is relative to wherever the script resides. If the script is in the same directory as the tts_phrases.yaml file then either of these will work:

!include tts_phrases.yaml
!include ./tts_phrases.yaml

If the script is in a sub-directory of config and tts_phrases.yaml is in the config directory then you must reference it like this:

!include ../tts_phrases.yaml

If the script is in the config directory but phrases is in a sub-directory called “extras” then reference it like this:

!include ./extras/tts_phrases.yaml

Bingo!! That looks exactly like what I’m looking for.

I’ll test this implementation and let you know.

Thank yoy

yes that works exactly the way I had hoped it would.

Have you ever considered using a placeholder for the phrases? this way only one script would ever be needed. The automation would call the script and pass the variables.

1 Like