- latest version node-sonos-http-api installed
Updated on 2017-03-19
- lua script updated. Selector switch follows state Sonos (playing or pauze)
There a few options to integrate Sonos with Domoticz
1. via php https://github.com/DjMomo/sonos
2. via the sonos http api; https://github.com/jishi/node-sonos-http-api
I prefer option 2, since it's far more flexible and (for me) easier to implement
Below you can find the step by step guide to get this running.
1. First make sure that at least version 4.0.0 of node is running. If it's older; update to the latest version.
node -v
I have version 5.6.0
2. clone the sonos http api to you home directory
Code: Select all
git clone https://github.com/jishi/node-sonos-http-api.git
4. Install the sonos http api, and do a few checks
Code: Select all
sudo npm install --production
Code: Select all
pm2 start server.js
Code: Select all
pm2 list
8. Some basic commands (where ip is the ip-address of the PM2 machine)
The Sonos starts playing with; http://ip:5005/badkamer/play
The Sonos pauses via; http://ip:5005/badkamer/pause
9. Create a selector switch (create a dummy device and pick selector switch in the list of virtual devices ) according to the below images;
I also created a functionality to extract a json string from the sonos. This way I can check the status of the sonos. E.g If always would like to pause the sonos when the tv is switched on. So the below script checks first if the Sonos is playing. If the tv is switched on, then the Sonos is paused. Keep in mind the below json script is just a way to show what is possible with json.
One if the things I would like, is to show some of the below json output on the selector switch. This way the selector switch shows e.g the artist or radio station that is playing. I have posted that issue over here, but the problem is still there; https://www.domoticz.com/forum/viewtopi ... 28&t=10689
Secondly I would also like to update the the selector switch according to the current status. E.g Pause button is active when in pause mode, and the play button is active is active when in play mode, even when the sonos is e.g paused outside of Domoticz. But to be able to do that I need a general on and off action feature, which can control the whole selector switch via a json script
1. If I issue the state command in a browser; http://ip:5005/badkamer/state
The below json string is returned;
{"currentTrack":{"title":"x-sonosapi-stream:s6712?sid=254&flags=32","albumArtUri":"/getaa?s=1&u=x-sonosapi-stream%3as6712%3fsid%3d254%26flags%3d32","duration":0,"uri":"x-sonosapi-stream:s6712?sid=254&flags=32","type":"radio","absoluteAlbumArtUri":"http://ip:1400/getaa?s=1&u=x-sonosapi-s ... ade":false}}
2. Now we will use LUA to decode the above json string.
3. 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
This library is required to decode the above json string
4. Create the below lua (script_device_sonos_tv_woonkamer.lua) script to start decoding the string;
Code: Select all
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local sonos=assert(io.popen('curl http://ip:5005/eetkamer/state'))
local status = sonos:read('*all')
sonos:close()
local jsonStatus = json:decode(status)
local tv_woonkamer_status = 'TV Woonkamer System Alive'
local sonos_woonkamer = 'Sonos Woonkamer/Eetkamer'
--print("Eerste JSON stappen met LUA")
--print(status)
print(jsonStatus)
playbackState = jsonStatus['playbackState']
artist = jsonStatus['currentTrack']['artist']
title = jsonStatus['currentTrack']['title']
album = jsonStatus['currentTrack']['album']
type = jsonStatus['currentTrack']['type']
--streaminfo = jsonStatus['currentTrack']['streamInfo']
commandArray = {}
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK" or playbackState == "PLAYING") then
if (playbackState == "PLAYING") then
if type == "radio" then
print('Woonkamer/Eetkamer Sonos status: ' ..playbackState.. ' ' ..type.. ' Titel: '..title)
else
if artist ~= nil then
print('Badkamer Sonos status: ' ..playbackState.. ' Artiest: ' ..artist.. ' Titel: '..title)
else
print('Badkamer Sonos status: ' ..playbackState.. ' Titel: '..title)
end
end
if (uservariables["SonosWoonkamerStatus"] == "PAUSED") then
commandArray['Variable:SonosWoonkamerStatus']='PLAYING'
print("Uservar SonosWoonkamer updated to PLAYING")
end
end
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then
print('Woonkamer/Eetkamer Sonos Status: ' ..playbackState)
if (uservariables["SonosWoonkamerStatus"] == "PLAYING") then
commandArray['Variable:SonosWoonkamerStatus']='PAUSED'
print("Uservar SonosWoonkamer updated to PAUSED")
end
end
else
--print("2")
print('<font color="red">Woonkamer/Eetkamer: SONOS-HTTP-API is off-line</font>')
end
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then
switch_status_sonos_woonkamer_eetkamer = otherdevices["Sonos Woonkamer/Eetkamer"]
print('Sonos Woonkamer/Eetkamer Switch Status: ' ..switch_status_sonos_woonkamer_eetkamer)
if otherdevices["Sonos Woonkamer/Eetkamer"] == 'Pauze' then
print('Sonos Woonkamer/Eetkamer Switch Status is correct, should be PAUZE: ' ..switch_status_sonos_woonkamer_eetkamer)
else
print('Sonos Woonkamer/Eetkamer Switch Status not correct, should be PAUZE: ' ..switch_status_sonos_woonkamer_eetkamer)
commandArray['UpdateDevice'] = 'idx|20|20'
end
end
if (playbackState == "PLAYING") then
switch_status_sonos_woonkamer_eetkamer = otherdevices["Sonos Woonkamer/Eetkamer"]
print('Sonos Woonkamer/Eetkamer Switch Status: ' ..switch_status_sonos_woonkamer_eetkamer)
if otherdevices["Sonos Woonkamer/Eetkamer"] == 'Play' then
print('Sonos Woonkamer/Eetkamer Switch Status is correct, should be PLAY: ' ..switch_status_sonos_woonkamer_eetkamer)
else
print('Sonos Woonkamer/Eetkamer Switch Status not correct, should be PLAY: ' ..switch_status_sonos_woonkamer_eetkamer)
commandArray['UpdateDevice'] = 'idx|10|10'
end
end
if devicechanged[tv_woonkamer_status] == 'On' then
print("TV Woonkamer is net aangezet, status Sonos Woonkamer onderzoeken")
if (playerstate == "PLAYING" or playbackState == "PLAYING") then
print("Sonos woonkamer heeft status PLAYING, Sonos Woonkamer/Eetkamer wordt uitgeschakeld")
commandArray[sonos_woonkamer]='Set Level 20'
end
end
return commandArray