I recently decided to experiment with alternate TTS components. This turned out to be a chore because I had service calls all over the place that needed to be updated.
So I created a script that I now run all TTS calls through. This allows me to easily make global changes in how TTS is processed. Want to change the TTS engine? Want to choose a different target for TTS playback? This’ll make it easy to make changes.
In my case, this also simplified my HA code. I use input_booleans to keep track of who’s awake so that I only play TTS when nobody’s sleeping. This method centralized all of the condition code that I had peppering my configuration.
Start by creating a script:
speak:
alias: TTS Handler
sequence:
- condition: and
conditions:
- condition: state
entity_id: input_boolean.person_one_awake
state: 'on'
- condition: state
entity_id: input_boolean.person_two_awake
state: 'on'
- service: tts.google_say
entity_id: media_player.squeezelite
data_template:
message: "{{ message }}"
In my case, I’m now using Google’s TTS and routing it through media_player.squeezelite. You can aim that at a Google Home, etc. Whatever you use. Drop the conditions clause if you don’t need that.
Then, in your automations/scripts/etc, you use your newly-created script like this:
- service: script.speak
data_template:
message: "The garage door has closed."
You can also use variables in the message. Here’s a script that speaks the current “feels like” temp from Dark Sky:
speaktemperature:
alias: 'Speak current temperature'
sequence:
- service: script.speak
data_template:
message: "The temperature feels like {{ states.sensor.dark_sky_apparent_temperature.state | int }} degrees."
I also had some code that lives outside HA (Tasker on my phone) that can route through this script. This’d also let you easily create TTS for IFTTT applets.
https://ih8gates.dyndns.org/api/services/script/speak?api_password=apipassword
{"message":"This is only a test."}