A quick Sonos automation

Now that I’ve got multiple Sonos speakers, I want to make the most of them by looking at some simple automations.

The first challenge was to play the radio while I’m getting ready for work.

There were several approaches I could take. I did think about alarms, but I wanted to keep my alarm separate. Nine times out of ten I wake up before the alarm – it’s really just a failsafe to stop me oversleeping. Since I can’t predict when I’ll wake up, I thought the best solution would be to have a simple voice command to turn the radio on. I also wanted it to play in all rooms, so that I can keep listening as I move around the house getting ready.

Sonos does not have voice support for grouping speakers, but luckily it’s supported by the API. Home Assistant came to the rescue.

I started off by creating a script to group all of my speakers, using the bedroom speaker as the master (it didn’t really matter which was the master):

  alias: "Group all Sonos"
    - service: sonos.join
        master: media_player.bedroom
        - media_player.kitchen
        - media_player.study

The service was media_player.sonos_join, and this changed to sonos.join in v0.93 of Home Assistant. Thanks to Robert for pointing this out in the comments below.

I then wanted to set the volume level. I sometimes use the bedroom speaker to play music to help me get to sleep and that’s often at a very low volume, so for my morning radio routine I created a script to set the right volume. I found the right level by experimenting and checking the values shown in the HA entities:

  alias: "Morning Sonos"
    - service: media_player.volume_set
        - media_player.bedroom
        - media_player.kitchen
        - media_player.study
        volume_level: 0.20

If need be I could have set a different level for each speaker.

The final step was to play the desired station. For some media players that would involve hunting around for the URL of the streaming source, and that URL is subject to change. There’s a much easier way with Sonos and Tunein radio.

I used the Sonos app to find radio stations and add them to my favourites:

Those favourites then show up as sources for the Sonos media player in Home Assistant (you may need to restart home assistant to refresh the source list):

Then all I had to do was call the scripts to set the volume and group the speakers before setting the source to the desired station:

  alias: "Sonos Morning Radio"
    - service: script.turn_on
        - script.sonos_morning_volume
        - script.sonos_group_all
    - service: media_player.select_source
      entity_id: media_player.bedroom
        source: "BBC Radio 1"

Finally, to add voice control, I added the script to my exposed entities for the emulated Hue component:

  type: alexa
  listen_port: 8300
  expose_by_default: false
    - script
    - scene
      name: "Group speakers"
      hidden: false
      name: "Ungroup speakers"
      hidden: false
      name: "Morning radio"
      hidden: false

I also added the scripts to group and ungroup speakers so that I could call them separately if I chose to.

With all of that in place I can now say “Alexa, turn on morning radio” when I wake up – whatever time that is, regardless of any alarms I have set. To stop the music, all I have to do is say “Alexa, stop” – there’s no need for a separate script to turn the music off.

It’s not perfect and I may tweak the setup. Although I initially intended to use it in the morning, I’ve found it’s useful any time I want the radio playing throughout the house, so I may change it to a more generic name.

As it stands, stopping the music leaves the speakers grouped, which isn’t always what I want. I have to remember to ungroup them.

I could have implemented it as a switch that ran the script when it was turned on and then ran a script to stop the music and ungroup the speakers when it was switched off. The problem with that approach is it’s easy to forget. If I set it up as a switch and just said “Alexa, stop” instead of “Alexa, turn morning radio off” it would leave the switch on – and inconsistent with reality.

Capturing the right logic to know when to ungroup the speakers automatically is fairly complex and may not always work – I might, for example, want to stop the radio playing but leave the speakers grouped to play some other music. For now it’s easier to simply have an ungroup command. If I find that, in practice, I’m always ungrouping them at certain times, I can think about further automation.

in Home Automation


  1. ronschaeffer 19th September 2018
  2. seanbAuthor 20th September 2018
  3. Lance 15th November 2018
  4. seanbAuthor 15th November 2018
  5. Robert C Blackwell 19th January 2021
  6. seanbAuthor 19th January 2021

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts