Gladys is already capable of speaking through Sonos speakers, but at home I have Google equipment: a Google Nest audio and a Google mini. I’ll explain how I use these speakers from scenes to bring Gladys to life ![]()
The operation is not just local, however: I rely on Google Cloud to generate the audio file from text. I also use node-red integrated in Gladys. And I recommend setting the speaker’s IP address in the box settings to ensure it doesn’t change and that the speaker is always reachable.
To avoid scenes being too monotonous, the operation I have set up allows defining different possible phrases, and a random draw determines which one is sent to the speaker. The volume is determined based on the time (to be more discreet in the evening or at night), with a mechanism to force a certain volume if needed (an alert that must be heard regardless of the time).
Here are the configuration steps:
- Set the speaker’s IP
On my side, I am behind a Freebox Revolution. On this device, you need to connect to the page http://mafreebox.freebox.fr/, then open the « Network Device » menu to locate your Google device (it is manufactured by « Google Inc. »), and right-click on this device choosing the action « Configure a DHCP lease ». Choose an IP address, and note it. It will be used in step 4 of this tutorial. At my place, it is http://192.168.1.130
On other Free boxes, I think the page http://mafreebox.freebox.fr/ also exists, but I am not sure… Maybe someone can confirm?
If you are with another operator, I don’t know if it is possible to set the IP… And if you cannot set the address, we will assume that it will not change too often
You must find the IP address of the Google device in your box…
- Create a Google Cloud account and retrieve the key
You first need to go to https://console.cloud.google.com and log in with a Google account. Then, create a new project on the page https://console.cloud.google.com/projectcreate. Once the project is created, go to the page https://console.cloud.google.com/apis/credentials to create an API identifier with the menu « + Create credentials » and the choice « API key ». Keep this API key which will be used in step 4 of this tutorial. In the rest of the tutorial, I will note it GOOGLE-API-KEY
- Create an MQTT device in Gladys
In Gladys, I assume you have already configured the MQTT integration (MQTT | Gladys Assistant). In the ‹ configuration › page of this integration, note the identifier and password. You will need them in step 4.
In the ‹ Devices › page, add a new device, then a feature of type « Text ». Leave the box « Yes, this sensor only sends data to Gladys » checked. Note the MQTT topic, it will be used in step 4 of this tutorial. At my place, it is « gladys/master/device/mqtt:annonce-google-home/feature/mqtt:annonce-google-home/text »
- Set up the Node-red flow
In Gladys, I assume you have already configured the Node-red integration (Node-RED | Gladys Assistant).
Use the « Manage palette » menu to add the module ‹ node-red-contrib-castv2 ›.
Use the « Import » menu, and copy-paste the following JSON configuration :
[
{
"id": "d37077f6ab6cbc16",
"type": "tab",
"label": "Google Home TTS",
"disabled": false,
"info": "",
"env": []
},
{
"id": "7e1b425bf5db62f7",
"type": "mqtt in",
"z": "d37077f6ab6cbc16",
"name": "Text reception to announce",
"topic": "gladys/master/device/mqtt:annonce-google-home/feature/mqtt:annonce-google-home/text",
"qos": "2",
"datatype": "auto-detect",
"broker": "1b5d1d2ce48e32e1",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 150,
"y": 60,
"wires": [
[
"093a1fe42d9a1c37",
"36cebd3192c88c48"
]
]
},
{
"id": "36cebd3192c88c48",
"type": "function",
"z": "d37077f6ab6cbc16",
"name": "Volume setting",
"func": "// 3 volume settings depending on the time of day. The value is between 0 and 100.\nconst daytimeMessageVolume = 50;\nconst nightMessageVolume = 10;\nconst morningAndEveningMessageVolume = 30;\n\nvar parts = msg.payload.split(\"|\");\n\nmsg.payload={};\nmsg.payload.type = \"VOLUME\";\n\nif (parts.length > 1){\n // if there is a '|' in the message, the first part indicates the volume to apply\n var vol = parts[0].slice(3);\n msg.payload.volume = 10 * Number(vol);\n} else {\n // otherwise, a volume is applied depending on the day and time\n // Mon-Fri: nightVol - 6am - morningAndEveningVol - 8am - daytimeVol - 8pm - morningAndEveningVol - 10pm - nightVol\n // Sat-Sun: nightVol - 6am - morningAndEveningVol - 10am - daytimeVol - 8pm - morningAndEveningVol - 10pm - nightVol\n var now = new Date();\n var hour = now.getHours();\n var day = now.getDay() // Sunday=0, Monday=1, ..., Saturday=6\n\n if (hour<6){\n msg.payload.volume = nightMessageVolume\n } else if ( (hour<10 && (day===0 || day===6)) || (hour<8 && day>=1 && day<=5) ) {\n msg.payload.volume = morningAndEveningMessageVolume;\n } else if (hour<20){\n msg.payload.volume = daytimeMessageVolume;\n } else if (hour<22){\n msg.payload.volume = morningAndEveningMessageVolume;\n } else {\n msg.payload.volume = nightMessageVolume;\n }\n}\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 480,
"y": 60,
"wires": [
[
"ae0fa7da259de071"
]
]
},
{
"id": "093a1fe42d9a1c37",
"type": "function",
"z": "d37077f6ab6cbc16",
"name": "Random choice if multiple sentences",
"func": "// only the last part is kept (the first, if it exists, is the volume)\nvar parts = msg.payload.split(\"|\");\nvar info = parts[parts.length-1];\n\n// If there are multiple sentences (separated by a ';'), one is chosen at random\nvar sentences = info.split(\";\");\nvar random = Math.floor(Math.random() * sentences.length);\nmsg.payload = sentences[random]\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 260,
"y": 140,
"wires": [
[
"19d1b62062ded3e5"
]
]
},
{
"id": "19d1b62062ded3e5",
"type": "function",
"z": "d37077f6ab6cbc16",
"name": "Adding TTS parameters",
"func": "var google_api_key = \"GOOGLE-API-KEY\"\nvar gender = \"male\"\n\nvar text = msg.payload;\n\nmsg.payload = {};\nmsg.payload.app = \"DefaultMediaReceiver\";\nmsg.payload.type = \"MEDIA\";\nmsg.payload.media = {};\nmsg.payload.media.url = \"https://www.google.com/speech-api/v1/synthesize?enc=mpeg&client=chromium&key=\"+google_api_key+\"&text=\"+text+\"&lang=fr-fr&gender=\"+gender+\"&speed=0.50&pitch=0.5\";\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 230,
"y": 200,
"wires": [
[
"ae6886112f208b88"
]
]
},
{
"id": "ae6886112f208b88",
"type": "delay",
"z": "d37077f6ab6cbc16",
"name": "200ms delay to allow volume modification",
"pauseType": "delay",
"timeout": "200",
"timeoutUnits": "milliseconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 550,
"y": 200,
"wires": [
[
"ae0fa7da259de071"
]
]
},
{
"id": "ae0fa7da259de071",
"type": "castv2-sender",
"z": "d37077f6ab6cbc16",
"name": "GGH Entrance",
"connection": "c8c6f114a3b8515b",
"x": 890,
"y": 200,
"wires": [
[]
]
},
{
"id": "1b5d1d2ce48e32e1",
"type": "mqtt-broker",
"name": "MQTT Gladys Broker",
"broker": "mqtt://192.168.1.100",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "c8c6f114a3b8515b",
"type": "castv2-connection",
"name": "GoogleHome Entrance",
"target": "",
"host": "192.168.1.130",
"port": "8009"
}
]
Edit the node I called « GGH Entrance », and click the pencil to specify in the ‹ Host › field the IP address of your Google device (retrieved in step 1 of this tutorial).
Edit the node I called « Text reception to announce ». Click the pencil to configure the link with the MQTT broker: In the « Connection » tab, enter in the ‹ Server › field the IP address of your MQTT-Gladys broker (this is Gladys’s IP address). In the « Security » tab, enter the username and password of your MQTT-Gladys broker (retrieved in step 3). Click « Save » to return to the configuration of the node « Text reception to announce », and enter in the « Topic » field the label of the MQTT topic created in Gladys (this is what you noted in step 3).
Edit the node I called « Adding TTS parameters », and modify the value of the variable google_api_key to enter the value GOOGLE-API-KEY noted in step 2. You can also modify the variable gender so that its value is « male » or « female ».
In the « Volume configuration » node, you can adjust the default volume choice. What I coded applies the following rule: volume 5/10 during the day, volume 3 in the morning and evening, volume 1 at night. The night lasts from 10 p.m. to 6 a.m., the evening from 8 p.m. to 10 p.m., the morning from 6 a.m. to 8 a.m. on weekdays and from 6 a.m. to 10 a.m. on weekends.
- Make Gladys speak from a scene
In a scene in Gladys, to use what has been configured in the previous steps, you must add an action « Send an MQTT message », enter the MQTT topic retrieved in step 3, then define the message. Here are some possible values for the message (note that you should not enter the « » in the field):
« I can speak. »
=> With this message, Gladys will broadcast the sentence on the Google device. The volume will be the one defined according to the time slots.
„Bonjour!; How are you, dear friend!; Hello! I hope you had a good night…‟
=> With this message, Gladys will randomly select one of the three phrases and broadcast it on the Google device. Default volume here as well.
„vol10|You are entering a secure home!‟
=> With this message, the message will be read at maximum volume, regardless of the time.
„vol8|I’m doing well.; I’m happy!‟
=> With this message, one of the two messages will be read at volume 8/10.
There, I hope I’ve been clear. I’m open to your feedback, because it’s my first tutorial ![]()