Looking into how to automate your Sonos speakers with home assistant ?
In this project I’m going to explain how you can integrate Sonos in home assistant, what type of controls you have and an example of an automation which you can achieve. By integrating Sonos into Home Assistant you can you it as an announcer with the Text to Speech functionality, you can also play your favourite playlist as part of your good morning routine.
Table of Contents
New to Home Assistant?
If you are just getting started with home assistant I suggest you enroll in my Free Home Assistant Course !
Prerequisites
- Home Assistant
- Sonos Speakers
- HACS (Home assistant Community Store)
- Songs / Playlist added as favourites in Sonos (My Sonos)
Don’t have HACS follow this video –> HACS YouTube Video
Configuration
Go to Configurations, integrations
Click the plus button
Find the Sonos integration, now if you have your Sonos on the same network and subnet if should just connect!
Reboot home assistant and you are ready!
My setup is comprised of three Sonos speakers:
1 Playbase in my living room, a Sonos play 1 in the kitchen and in the bathroom.
If you wish, you could rename the device settings and assign them to an Area in home assistant.
Now we can add in some custom mini players in our dashboard, the custom mini player I installed via the HACS store.
With this media player we can:
- Group and Ungroup Sonos Speakers.
- Play music (we can only pick for our favourites list).
- Change music, pause and play.
Edit your dashboard and find the manual code and copy paste the code below:
type: 'custom:mini-media-player'
entity: media_player.lounge
hide:
power: true
speaker_group:
platform: sonos
show_group_count: true
entities:
- entity_id: media_player.lounge
name: Sonos Lounge
- entity_id: media_player.kitchen
name: Sonos Kitchen
- entity_id: media_player.bathroom
name: Sonos Bathroom
entities:
- type: 'custom:mini-media-player'
entity: media_player.multiroom_player
group: true
source: icon
artwork: cover
info: short
hide:
volume: true
power: true
- type: 'custom:mini-media-player'
entity: media_player.lounge
group: true
hide:
controls: true
- type: 'custom:mini-media-player'
entity: media_player.kitchen
group: true
hide:
controls: true
- type: 'custom:mini-media-player'
entity: media_player.bathroom
group: true
hide:
controls: true
Text To Speech
Sonos is compatible with text to speech that means that you can make your Sonos speaker talk, you could use this as a Halloween prank or just to broadcast messages across the house (dinner is ready ?)
This is how you can set it up:
Go to your file editor and open the scripts.yaml file:
sonos_say:
alias: "Sonos TTS script"
sequence:
- service: sonos.snapshot
data_template:
entity_id: "{{ sonos_entity }}"
- service: sonos.unjoin
data_template:
entity_id: "{{ sonos_entity }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ sonos_entity }}"
volume_level: "{{ volume }}"
- service: tts.google_say
data_template:
entity_id: "{{ sonos_entity }}"
message: "{{ message }}"
- delay: "{{ delay }}"
- service: sonos.restore
data_template:
entity_id: "{{ sonos_entity }}"
This script allows you to do several things:
- Create a snapshot of what Sonos is currently playing
- Set the volume
- Restore a queue
- Add text to speech with google_say
Go back to your dashboard in your manual card and add this piece of code, indent this code correctly until you see the green tick. The entity ID will be the one you wish to use to broadcast your message
tts:
entity_id: media_player.lounge
platform: sonos
volume: 0.25
In order for Sonos to keep playing messages after the TTS message then do the following:
Create an input text (in configuration.yaml) to store the message, we will display this in the dashboard later:
input_text:
sonos_message:
name: Sonos TTS
initial: Some Text
Now we can add the automation , each time the value changes of the input text we will trigger the sonos_say service.
Copy the following code to your automations.yaml and reload
- id: tts_sonos
alias: Sonos TTS
trigger:
- platform: state
entity_id: input_text.sonos_message
action:
- service: script.sonos_say
data_template:
sonos_entity: media_player.kitchen, media_player.lounge, media_player.bathroom
volume: 0.2
message: "{{ states.input_text.sonos_message.state }}"
delay: '00:00:04'
In my example I’m going to broadcast to all my speakers, setting to the same volume (0.2) and with a delay of 4 seconds. The message can be hardcoded with any value, however I’m reading the state of the Sonos message.
Automation
I’m going to create a simple automation to trigger a playlist in the morning when my kitchen led lightstrip turns on in the morning.
This is what you will need to configure:
Your entity id of the “thing” that is going to trigger the automation (could be a light or a switch, motion sensor).
In the action I’m using my Kitchen Sonos called media_player.kitchen.
I’m playing a specific source called “Dark Necessities” (you will need to add this as a favourite in Sonos). I’m setting the volume at 0.10 in a specific window between 6:30 am and 7:00 am between Monday to Friday. This means that the automation will not run every time the light is turned on.
The code currently would restart the playlist if the automation is triggered again in that time window.
- id: morning_routine
alias: Morning Kitchen Routine
trigger:
- platform: state
entity_id: light.hue_lightstrip_plus_1
to: 'on'
action:
- service: script.turn_on
entity_id: media_player.kitchen
- service: media_player.select_source
entity_id: media_player.kitchen
data:
source: "Dark Necessities"
- service: media_player.volume_set
entity_id: media_player.kitchen
data:
volume_level: 0.10
condition:
condition: time
# At least one of the following is required.
after: ’06:30:00'
before: ’07:00:00'
weekday:
- mon
- tue
- wed
- thu
- fri
DIY Alarm Siren
In this video I show you how you can setup a DIY Alarm from Home Assistant by using your Sonos Speaker as a Siren!