Sonos + raspberry pi + domotocz

  • 16 December 2019
  • 1 reactie
  • 5320 Bekeken

Reputatie 2
Badge +2

Zoals gevraagd door Tim S, een tijdje geleden heb ik me een tijdje bezig gehouden met de integratie van enkele van mijn Sonos speakers met Domotocz & Hue sensors.

 Omdat de SD kaartjes van de Raspberry af en toe wel eens kunnen in de soep draaien maakte ik volgende handleiding voor mezelf.

Al deze dingen zijn wel ergens op het web te vinden, ik heb ze alleen samengevoegd tot een voor mij werkend geheel, Suggesties zijn welkom !


Target :
1) Project Alarm
Start an alarm on a pre-defined time with a pre-defined group & sound level
2) Project WC
Switch on player “WC” when movement is detected on a Hue sensor.
If no music was playing in the room “Kitchen” then a sonos playlist will be selected, if music is playing in kitchen, player will be grouped with kitchen After some time, player “WC” will stop.

1) Sonos speakers
2) Hue hub + sensor
3) Raspberry PI (I am using the 4) with Domotics

What to do :

1) Install Noobs & Domotics on the Raspberry
or Raspbian Buster
enable SSH to raspberry
Do a full upgrade of the Raspberry
→Chris made a great video on this !

  1. Install Domoticz rry_Pi
curl -L | bash
Follow the installation procedure as in Wiki to get Domoticz working


  1. Install Hue hub  - should be easy motion sensors& lights should be popping up 
  2. Install SONOS interface on raspberry

git clone node-sonos-http-api
cd node-sonos-http-api
npm install -production
npm start

If all goes well, you should be able to control Sonos speakers by using the HTTP commands
http://localhost:5005/living room/volume/15 (will set volume for room Living Room to 15%)


Issue : if the raspberry is re-boorted, node has to be restarted manually → not optimal ! solution : 

Cd / home/pi
sudo npm install -g pm2
sudo pm2 startup

cd /your/http-api_folder/
sudo pm2 start server.js -x --name "http-api"
sudo pm2 save
sudo pm2 dump

Now node should autostart, so also the Sonos control

Till here the preparations, let’s try to do something in Domoticz :


1) Project Alarm :
create a dummy switch in Domotics, you will use this to control the alarm

2) Setup the switch

Create the switch & use as “on action” as script, e.g. script://

Script :
1) to be located in domoticz/scripts
2) to be set as executable (sudo chmod +x )
content in my case :
! /bin/bash
curl ''
curl ''
curl ''
curl ''
curl ''
curl ''

To turn the speakers off, I simply use in the off section of the speaker
As I keep these speakers in the morning always grouped


Create timer :

In the switch you have a menu where you can set the timer on & off.

If all goes well, using this VRT Studio Brussels should start at 0700 am)


Project WC
Main issue, how to know the status of a sonos speaker ?

1) To get this create a new LUA script

Download and store JSON.lua library in the LUA scripts directory on your domoticz installation. The library is available here ; or
save this in home/pi/domoticz/scripts/lua/JSON.lua

Script (I copied this from somewhere on the net & got it working, any “ / ‘ , . is important :

return {
on = {
timer = {
'every 3 minutes'
execute = function(domoticz, timer)
local json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local sonos=assert(io.popen('curl'))
local status = sonos:read('*all')
local jsonStatus = json:decode(status)
local playbackState = jsonStatus['playbackState']
if (playbackState == nil)
playbackState = 'OFFLINE'
if (playbackState == 'PLAYING')


I hardcoded the switch names & IP addresses & speaker name, not optimal, but working
Create a dummy switch, in my case “keukenon” to keep the status of the playerEach 3 min this script will check if Kitchen is playing and update the switch Keukenon

Now create Create switch
WC toggle

Script on
curl ''
curl ''

Script off
curl ''
curl ''
curl ''
curl ''
sleep 5
curl ''

Create switch WC toggle group

Script on
curl ''
curl ''
curl ''
Script off
curl ''

Create 2 Blocky scripts


With all this in place, the speaker WC should be switching on when hue sensor (ZLL_presence WC) is on.


There is some room for improvement, if the speaker in the kitchen is switched on once the music in the WC started from the local stream, than in some cases the music in the kitchen will be stopped together with the WC.




Reageren op dit topic is niet mogelijk.

1 Reactie

Reputatie 2
Badge +2

I am in the meanwhile using for a couple of months the Home Assistant (HASSIO) integration.

Less steep learning curve, more eye candy, with the same/better integrations and same functionality

Not sure the Domotics version is still 100 % supported/working.