In this blog post, we will set up text-to-speech (TTS) in Home Assistant using Node-RED. This is a powerful feature that allows your smart home system to announce important alerts, messages, or notifications. If you’re new to Home Assistant, it is an open-source home automation platform that focuses on privacy and local control.
First, let’s enable text-to-speech for Home Assistant.
Enabling Text-to-Speech in Home Assistant
- In Home Assistant, go to the File editor.
- Add the following code snippet to your
configuration.yaml
file.
text_to_speech:
- platform: google_translate
cache: true
cache_dir: tts_cache
time_memory: 300
service_name: google_say
base_url: http://<IP:PORT_OF_HOME_ASSISTANT>
This snippet enables text-to-speech using Google Translate as the platform. It also caches some of the sentences to speed up the process and sets up the google_say
service.
Don’t forget to replace <IP:PORT_OF_HOME_ASSISTANT>
with the IP and port of your Home Assistant instance.
- Save the changes and restart Home Assistant.
Now that we have text-to-speech enabled, let’s use it in Node-RED.
Using Text-to-Speech in Node-RED
Node-RED is a flow-based visual programming tool for wiring together hardware devices and APIs. It’s a popular choice for creating automations in Home Assistant. In this example, we will set up text-to-speech to announce when someone rings the doorbell.
- Open Node-RED and create a new flow.
- Drag a call service node onto the canvas.
- Configure the call service node as follows:
- Domain:
tts
- Service:
google_say
- Entity:
media_player.<YOUR_MEDIA_PLAYER>
- Data (JSON):
{ "message": "Ding dong! There is someone at the gate.", "language": "en" }
- Domain:
- Make sure to replace
<YOUR_MEDIA_PLAYER>
with the entity ID of your media player, likemedia_player.office_display
. - Connect your doorbell node to the call service node.
Now, whenever someone rings the doorbell, your media player will announce that there is someone at the gate.
Adding Dynamic Data to Text-to-Speech
Let’s say you want to announce the current water level of your water tank when the pump is turned off. Well, at least that is what I want ๐ We can achieve this by including dynamic data in our text-to-speech message.
- In Node-RED, create a new flow, or use the existing flow you created earlier.
- Drag a current state node onto the canvas.
- Configure the current state node to get the water level from your desired entity, e.g.,
sensor.water_tank_level
. - Next, drag a call service node onto the canvas.
- Configure the call service node as follows:
- Domain:
tts
- Service:
google_say
- Entity:
media_player.<YOUR_MEDIA_PLAYER>
- Data (JSON):
{ "message": "Water tank is off and it has {{ payload }} liters.", "language": "en" }
- Domain:
- Make sure to replace
<YOUR_MEDIA_PLAYER>
with the entity ID of your media player, likemedia_player.office_display
. - Connect your water tank turn-off node to the current state node, and the current state node to the call service node. Make sure you set the format of the data as JSON and not J-Expression. This will ensure the
{{payload}}
variable is replaced with the actual value.
Now, when the water pump is turned off, the current water level will be announced along with the notification.
Conclusion
In this blog post, we covered how to set up text-to-speech in Home Assistant using Node-RED. By following these steps, you can create custom notifications and alerts to be announced by your smart home system. Text-to-speech is a powerful tool that can enhance your home automation experience, and with the right setup, you can make your smart home even smarter. The full video on which this was based is below.