Sonos + raspberry pi + domotocz

  • 16 december 2019
  • 1 reactie
  • 3048 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.


Equipment
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
https://www.raspberrypi.org/downloads/noobs/
or Raspbian Buster
enable SSH to raspberry
Do a full upgrade of the Raspberry
→Chris made a great video on this ! https://www.youtube.com/watch?v=juHoJYX86Dg

  1. Install Domoticz

https://www.domoticz.com/wiki/Raspbe rry_Pi
curl -L https://install.domoticz.com | 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

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

git clone https://github.com/jishi/node-sonos-http-api 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
e.g.
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://morning_on.sh

Script morning_on.sh :
1) to be located in domoticz/scripts
2) to be set as executable (sudo chmod +x )
content in my case :
! /bin/bash
curl 'http://192.168.3.53:5005/dining%20room/volume/5'
curl 'http://192.168.3.53:5005/living%20room/volume/5'
curl 'http://192.168.3.53:5005/kitchen/volume/10'
curl 'http://192.168.3.53:5005/kitchen/tunein/play/2611'
curl 'http://192.168.3.53:5005/dining%20room/join/Kitchen'
curl 'http://192.168.3.53:5005/living%20room/join/Kitchen'

To turn the speakers off, I simply use in the off section of the speaker 

http://192.168.3.53:5005/kitchen/pause
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 ;
https://www.domoticz.com/wiki/Lua_-_json.lua or http://regex.info/blog/lua/json
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 http://192.168.3.53:5005/kitchen/state'))
local status = sonos:read('*all')
sonos:close()
local jsonStatus = json:decode(status)
local playbackState = jsonStatus['playbackState']
if (playbackState == nil)
then
playbackState = 'OFFLINE'
end
if (playbackState == 'PLAYING')
then
domoticz.devices('kitchenon').switchOn()
else
domoticz.devices('kitchenon').switchOff()
end
end
}

 

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 'http://192.168.3.53:5005/wc/volume/4'
curl 'http://192.168.3.53:5005/wc/play'

Script off
curl 'http://192.168.3.53:5005/wc/pause'
curl 'http://192.168.3.53:5005/wc/playlist/80s'
curl 'http://192.168.3.53:5005/wc/pause'
curl 'http://192.168.3.53:5005/wc/shuffle/on'
sleep 5
curl 'http://192.168.3.53:5005/wc/repeat/on'

Create switch WC toggle group


Script on
curl 'http://192.168.3.53:5005/wc/say/enjoy%20the%20experience/7'
curl 'http://192.168.3.53:5005/wc/volume/4'
curl 'http://192.168.3.53:5005/wc/join/kitchen'
Script off
curl 'http://192.168.3.53:5005/wc/leave'

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.

 

 

 


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.

Reageer