Controlling Sonos via LightwaveRF Mood Controllers

I had a request to write about how I control my Sonos players via my LightwaveRF Mood Controllers.  Actually, it’s a good call: re-engineering the mood controllers from simple light switches to pads which can control both lighting moods and audio more closely reflects the ‘built-in’ panels found in expensive custom installs.  In fact, using the technique below there’s nothing to stop you from controlling any number of home control activities from your mood controllers or handheld remotes.

I can say with authority that without these wall-mounted controllers all over the flat, I would never have been allowed to continue developing the system.  Having a familiar controller in each room ensures that users don’t need to run to the central controller or get out their smartphones whenever they want to make quick and regular changes to the light, heating or audio settings.

What you’ll need:

A summary is that node-sonos-http-api does the vast majority of the work, by ‘listening’ to all connected Sonos controllers and controlling them by very simple http requests.  These http requests are triggered by Domoticz once a signal is received from the mood controller.

How long this will take:

  • If you already have the Pi running Domoticz, and have been using a Sonos player already, and maybe have just bought a LigtwaveRF mood controller or other wall controller compatible with Domoticz, the whole thing will take about 30-40 minutes if you take your time.

Add the mood controller to Domoticz

From the Domoticz interface select the Switches screen, then select Learn Light/Switch at the top.  You should imagine the mood controller as two distinct switches stuck together: as far as Domoticz is concerned the two largest buttons are a completely different device compared to the four smaller buttons.

Immediately press the large OFF button (marked with a zero) on the mood controller.  In the box that appears give the mood controller a name – remembering that this is only the name given to the top two buttons of the controller (e.g Bathroom Lights).

This time we’re going to learn the lower row of the mood controller (the four smaller buttons). Repeat the above by pressing Learn Light/Switch and this time immediately press any one of the smaller buttons on the controller.  Name these too (e.g. Bathroom Audio).

Back up your Domoticz database

Just in case the next step causes problems for you it might be good to back up your system.  I’ve only needed to restore the system once – when I made a complete mess of installing node and npm.

Install node-sonos-http-api

I did this to install and it worked.  You may have a different setup or you may want to select another way of doing this.

Make sure you have node and npm installed.  If you don’t, search for and follow the instructions on how to install these carefully as I have messed this up more than once, by being all “I know how to do this, I’ll just skip this step”.  When will I ever learn?!

Get to the Domoticz folder and create a folder called sonos.

In the sonos folder, clone the node-sonos-http-api program by typing


git clone https://github.com/jishi/node-sonos-http-api.git

Navigate to the new node-sonos-http-api folder and fix the dependencies by typing


npm install

You can now run the program by typing


node server.js

Now the magic starts!  Open a browser and navigate to http://192.0.0.0:5005/Kitchen/play, where 192.0.0.0 is the address of your Pi and Kitchen is the name of your Sonos speaker.

The browser will ‘open’ a blank page, but the speaker in the room will start playing (assuming something is in the play queue.  If not use http://192.0.0.0/Kitchen/playlist/My%20Playlist.  This time, the playlist “My Playlist” is selected (make sure you use hex codes such as %20 instead of spaces in the URL- this includes rooms, so Living Room becomes Living%20Room).

Use the commands listed here to control your Sonos players.

Create scripts

Consider the commands you will want to send to Sonos via the mood controllers.

In this example my room is called Bathroom and the functions I want to add to the mood controller are play, pause, Radio 4 and a playlist called Harry 1.

I’ll need to create four scripts.  The easiest way to create scripts on the Pi  is to use nano, or off the Pi I use WinSCP.

Make sure the scripts are stored in the domoticz folder (not any subfolder).

The scripts will be called Sonos-BR-Play,.sh Sonos-BR-Pause.sh, Sonos-BR-R4.sh and Sonos-BR-Harry1.sh

Sonos-BR-Play will include the following text (just one line is needed to send an http request via a command called curl):

curl http://192.0.0.0:5005/Bathroom/play

Where 192.0.0.0 is the address of the Pi and ‘Bathroom’ is the exact name of the Sonos player.

The other 3 files contain very similar commands:

curl http://192.0.0.0:5005/Bathroom/pause

curl http://192.0.0.0:5005/Bathroom/favorite/BBC%20Radio%204

and

curl http://192.0.0.0:5005/Bathroom/playlist/Harry%201

So now I have these files in the domoticz folder.  I need to make them executable by Domoticz, so in the domoticz folder type


chmod u+x *.sh

We’re coming to the last part now!

Create LUA scripts for Domoticz

We need to run the scripts we created when Domoticz detects the button being pressed on the mood controller.  Let’s imagine the 4 small buttons on the mood controller are going to control the 4 functions we’ve just created scripts for.  The four buttons change the Domoticz device to Group Off, Group Mood 1, Group Mood 2 or Group Mood 3.  You can test this by pressing the buttons and waiting to see Domoticz change the status of the device.

I’m going to imagine that the mood controller’s set of 4 buttons is called ‘Bathroom Audio’ in Domoticz.

Go to the domoticz/scripts/lua folder and create a new file called script_device_BathroomAudio.lua

In the script I’m going to put this text in (you should see by the contents how you can change this):


commandArray = {}

if devicechanged['Bathroom Audio'] == 'Group Off' then

os.execute('./Sonos-BR-Play.sh')
print ('Bathroom Play via mood controller')

elseif devicechanged['Bathroom Audio'] == 'Group Mood 1' then
os.execute('./Sonos-BR-Pause.sh')
print ('Bathroom Pause via mood controller')

elseif devicechanged['Bathroom Audio'] == 'Group Mood 2' then
os.execute('./Sonos-BR-R4.sh')
print ('Bathroom Radio 4 via mood controller')

elseif devicechanged['Bathroom Audio'] == 'Group Mood 3' then
os.execute('./Sonos-BR-Harry1.sh')
print ('Bathroom Harry Playlist via mood controller')

end

return commandArray

That’s it! You should be able to control your Sonos anywhere you can stick a LightwaveRF mood controller.  Let me know how you get on!

Advertisements

2 thoughts on “Controlling Sonos via LightwaveRF Mood Controllers”

  1. This is great, and super helpful, so thank you. Now up and running stably, it makes a big difference to how Sonos gets used. The next obvious step in our setup would be getting some logic to check whether the request was already playing elsewhere (via a JSON state), and to join that zone, rather than starting a separate stream. This is likely beyond my scripting skills for now, but some intriguing possibilities.

    Liked by 1 person

    1. Great! If you figure out how to do that please let me know… We always end up playing the same radio station (but via new streams) all over the flat especially at the weekend. Glad you found the post useful.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s