Whole home audio with Sonos - Part 2

In my last post, I ran out of time (and column inches!) to cover everything I wanted to, so in this article we’ll look at connecting the speakers to Hubitat so you can control them along with the rest of your home automation setup, and we’ll take the time to look at implementing some rules to enhance how we wake up and go to sleep each night accompanied by music.

Connecting to Hubitat

Connecting the devices in Hubitat couldn’t be easier.

Log in to the web interface for your Hubitat, select “Apps” from the menu on the left, click “Add Built In App”, search for Sonos, and click “Install”.

If the installation and setup does not start automatically, click on the Sonos app in the “Installed Apps” menu. Hubitat will automatically discover your sonos devices and add them to your setup. That was easy wasn’t it!

Automating the Sonos Speakers

Managing media devices such as Sonos is a mixed bag within Hubitat, and it’s one of the few places that a tiny bit of disappointment with the platform creeps in for me.

Adding the devices was trivial, and yet when you add a “media player” widget to the built in dashboards (more on those in a future post!), you only get play/pause/next/last and the text of the title printed to the screen. It would be far nicer if we could get the album artwork or similar, but for now this is what we get.

Getting the device to play a particular track can also be an issue, so let’s go through the steps that we need to follow in order to get Hubitat to play a particular track in the style of an “alarm clock”.

  1. Use the Sonos app (or your favourite media player such as Spotify) to select the track you want and play it on the appropriate speaker
  2. In the Hubitat console, select “Devices”, then select the speaker that is playing that particular song
  3. Look at the “Current States” data on the right of the screen, and then find the “trackUri”
  4. Copy this URI and keep it somewhere safe so we can use it. In our case, the trackUri is x-sonos-spotify:spotify:track:6eTGxxQxiTFE6LfZHC33Wm?sid=9&flags=0&sn=3 which tells Sonos to talk to Spotify and load Hoppípolla by Sigur Rós.
  5. Now go to “Apps” and click on “Rule Machine”, then click “Create new rule”
  6. Give the new rule a name of “Morning Alarm”, and then click “Select Trigger Events”
  7. From the “Select capability for new Trigger Event " drop down, select “Certain Time”, and then click “At a certain time” to set the clock
  8. Set the time you want the song to start playing, and then click “done”, “done with trigger”, and “done with trigger events”. You should now have a screen that looks like this:
  9. Now select “Select actions to run”, and from the dropdown that appears choose “Control music player, volume, sounds”
  10. From the “action” drop down that appears, select “Control music player”, then select your sonos device from the “Control media player” button and click “update”
  11. From “select player command”, choose “play track”, and then paste the uri from step 4 into the box, then click “done with this action”
  12. Click “done with actions”, and then “done” to save it - congratulations, your music will now play at the selected time!

Once you’ve tested the rule and confirmed that it works, why not have a play with some of the other settings you can manage such as the volume controls and have the music fade in over time?

The best place to get support on the Rules engine is within the “Rule Machine” section Hubitat community, so head over there to learn more about just how powerful this feature of Hubitat can be!

Summary

In this post, we’ve seen how we can use Hubitat to automatically play media from Spotify on Sonos Speakers as an “alarm clock”, however the trigger event could be anything from someone switching on a particular light to motion detected in a particular zone. You can also get the system to “speak” via the text to speech functionality, or adjust the volume based on the time of day.

In future posts, we’ll cover more on the power of RuleMachine and add more functionality that takes advantage of the Sonos setup but for now have fun, and post your creations in the comments or tag us on social media, and I’ll see you next time!

Share Comments
comments powered by Disqus