Step by step guide: Sonos integration

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Step by step guide: Sonos integration

Postby Holland » Tuesday 08 March 2016 0:14

Updated on 2017-01-28
- latest version node-sonos-http-api installed

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 :P

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


3. jump to the just created map; cd node-sonos-http-api

4. Install the sonos http api, and do a few checks

Code: Select all

sudo npm install --production


5. since I use pm2, the sonos http api must be running in pm2

Code: Select all

 pm2 start server.js


6. check if it runs

Code: Select all

 pm2 list


7. So now the sonos http api is running in the background, and waiting for commands

8. Some basic commands
The Sonos starts playing with;

Code: Select all

http://ip:5005/badkamer/play

The Sonos pauses via;

Code: Select all

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; viewtopic.php?f=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;

Code: Select all

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-stream%3as6712%3fsid%3d254%26flags%3d32"},"nextTrack":{"artist":"","title":"","album":"","albumArtUri":"","duration":0,"uri":""},"volume":18,"mute":false,"trackNo":1,"elapsedTime":0,"elapsedTimeFormatted":"00:00:00","playbackState":"STOPPED","playMode":{"repeat":"none","shuffle":false,"crossfade":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://10.58.60.6: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)

--playerstate = jsonStatus['playerState']
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'] = '43|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'] = '43|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
Attachments
Sonos selector switch 2.JPG
Sonos selector switch 2.JPG (70.43 KiB) Viewed 7524 times
Sonos selectorswitch.JPG
Sonos selectorswitch.JPG (22.55 KiB) Viewed 7524 times
Last edited by Holland on Saturday 28 January 2017 22:33, edited 7 times in total.

teha
Posts: 17
Joined: Wednesday 30 December 2015 21:23
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Lomma, Sweden
Contact:

Re: Step by step guide: Sonos integration

Postby teha » Tuesday 08 March 2016 11:06

I really liked the way that Jishi implemented the http-api however when I install and run this I get error when i run the server.js

anyone having any ideas?
Attachments
Capture.PNG
Capture.PNG (16.35 KiB) Viewed 7498 times
RPi2 with RFXtrf433(E) at stable, RPi2 with AeonStick S2 on beta, 6*Sonos, Synology DS415play, Axis, Foscam, PhilipsHue

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Tuesday 08 March 2016 23:24

What version of Node have you running. At least version 4.0.0 is required

I have the following;

[email protected] ~ $ node -v
v5.6.0

If outdated do the following;

http://blog.wia.io/installing-node-js-v4-0-0-on-a-raspberry-pi

wmn79
Posts: 63
Joined: Monday 13 April 2015 23:19
Target OS: NAS (Synology & others)
Domoticz version: 3.5033
Location: Amsterdam, The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby wmn79 » Wednesday 09 March 2016 0:06

Just made a link to this post on the Wiki ( https://www.domoticz.com/wiki/Sonos ) feel free to edit that where needed.

wmn79
Posts: 63
Joined: Monday 13 April 2015 23:19
Target OS: NAS (Synology & others)
Domoticz version: 3.5033
Location: Amsterdam, The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby wmn79 » Wednesday 09 March 2016 22:23

I tried to install this but I run into an error which I'm not able to solve at the moment. Maybe you have an idea.

The whole install works fine but when I then run:
npm start

I get the following:

Code: Select all

> [email protected] start /home/domoticz/node-sonos-http-api
> node server.js

info: binding SSDP to port 1905
info: discovering all IPs from lo
info: discovering all IPs from eth0
info: discovering all IPs from eth1
info: discovering all IPs from eth2
info: relevant IPs 10.0.2.15=null, 192.168.56.101=null, 192.168.1.4=null
info: notification server listening on port 3500
http server listening on port 5005
no preset file, ignoring...
info: scanning for players in ip 10.0.2.15
info: scanning for players in ip 192.168.56.101
info: scanning for players in ip 192.168.1.4
info: subscribing to topology 10.0.2.2
info: using local endpoint 192.168.1.4
error:  code=ECONNREFUSED, errno=ECONNREFUSED, syscall=connect, address=10.0.2.2, port=1400


I am running Domoticz on Debian Jessie in a virtual box virtual box which has three virtual network adapters. I think the issue is that it is "subscribing to topology 10.0.2.2" because that is not my local LAN ip range is 192.168.1.xx.

Do you have any ideas on how I can force node-sonos-http-api to use the 192.168.1.xx instead of the 10.0.2.xx range. Maybe with a settings.json? I didn't find a solution on the github repo yet.

Node version: 5.7.1
NPM version: 3.6.0

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Wednesday 09 March 2016 22:52

Dont' now your complete setup, but if you set the network adapter in bridge mode, then the vm will use the same network range as you local network. In addition, in a standard setup enabling just 1 networkadapter (NIC) should be enough

First, I would make the setup as simple as possible. When that works, you can complicate things a bit further if you like

With settings.json you can select a preferred port, but not fix a certain ip. But I'm not completely sure about the latter

wmn79
Posts: 63
Joined: Monday 13 April 2015 23:19
Target OS: NAS (Synology & others)
Domoticz version: 3.5033
Location: Amsterdam, The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby wmn79 » Thursday 10 March 2016 0:01

I have a FreeNAS machine that runs a virtual box host in a jail. That virtual box hosts a Debian Jessie VM that runs domoticz. So a bit of a complicated setup. The VM has three network adapters:
  • one for NAT (10.0.2.x range),
  • one host-only adapter (192.168.95.x range)
  • one bridged adapter(192.168.1.4 fixed ip).
I tried during troubleshooting this issue to disable the other two adapters and only keep the bridged adapter but that didn't seem to work out, since I wasn't able to access my machine anymore when I did that. I think I need to do some more digging into this network config because I didn't find a way to force the third adapter.

EDIT: I have unticked "cable connected" for the first two adapters and now it works. I think I will look into getting rid of the two adapters in total. But for now this is the workaround so I can continue with your step-by-step guide. Thanks for your help.

wmn79
Posts: 63
Joined: Monday 13 April 2015 23:19
Target OS: NAS (Synology & others)
Domoticz version: 3.5033
Location: Amsterdam, The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby wmn79 » Thursday 10 March 2016 17:24

So I now got it fixed, removed the two other network adapters and got the node-sonos-http-api up and running through pm2.

Holland wrote:9. Create a selector switch 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

For the first thing I don't know a solution, but I would love to have that implemented as well.
For the second however it is quite easily implemented in the same way I used to do with my previous Sonos On/Off switches. You can just send the command again so the switch will reflect the current state. The only disadvantage of this is that it will create a line in your error log every time the status is corrected it shows up that it cannot open the url. I tried with a json string as well but that doesn't seem to work so I commented it out. Below is your example adjusted to one of my Sonos speakers.

Code: Select all

-- ~/domoticz/scripts/lua/script_time_sonoskeuken.lua
json = (loadfile "/home/domoticz/domoticz/scripts/lua/json.lua")()

local sonos=assert(io.popen('curl http://192.168.1.4:5005/Keuken/state'))
local status = sonos:read('*all')
sonos:close()
local jsonStatus = json:decode(status)
local sonos_keuken = 'Sonos Keuken'

--print(status)

playerstate = jsonStatus['playerState']
artist = jsonStatus['currentTrack']['artist']
title = jsonStatus['currentTrack']['title']
album = jsonStatus['currentTrack']['album']
streaminfo = jsonStatus['currentTrack']['streamInfo']
   
commandArray = {}

if (playerstate == "PLAYING" or playerstate == "STOPPED" or playerstate == "PAUSED_PLAYBACK") then
   if playerstate == "PLAYING" then
      if   otherdevices[sonos_keuken]~='Play' then
      commandArray[sonos_keuken]='Set Level 10'
      --os.execute('curl http://192.168.1.4:8080/json.htm?type=command&param=switchlight&idx=103&switchcmd=Set%20Level&level=10')
     end    
     print('Sonos Keuken status: ' ..playerstate.. ' Artiest: ' ..artist.. ' Titel: '..title.. ' Streaminfo: ' ..streaminfo)
   end
   if (playerstate == "STOPPED" or playerstate == "PAUSED_PLAYBACK") then
      if   otherdevices[sonos_keuken]~='Pause' then
      commandArray[sonos_keuken]='Set Level 20'
      --os.execute('curl http://192.168.1.4:8080/json.htm?type=command&param=switchlight&idx=103&switchcmd=Set%20Level&level=20')
     end
     print('Sonos Keuken: ' ..playerstate)
   end
else
   --print("2")
   print('<font color="red">Sonos Keuken: SONOS-HTTP-API is off-line</font>')
end   

return commandArray


When I went further through your guide I saw one error which I think is the following:
Holland wrote:1. If I issue the state command in a browser;

Code: Select all

http://10.58.60.84:5005/badkamer/


I think you forgot to put state at the end:

Code: Select all

http://10.58.60.84:5005/badkamer/state


Regarding the Lua script maybe you can give me some advice:
I have three Sonos devices here, what do you think would be best to use one Lua script per device or to use one single Lua file for all three devices?

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Thursday 10 March 2016 23:00

@wmn79. I corrected the state error. Added that I have used device script, and not a time script.

Good suggestion for the status issue. Gonna give it a try. Thanks

And for you last question. I have also several Sonos devices, and used a separate script for each room, just for sake of simplicity.

But if would put a little more time in and effort in it, I could integrate it in just one script.

teha
Posts: 17
Joined: Wednesday 30 December 2015 21:23
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Lomma, Sweden
Contact:

Re: Step by step guide: Sonos integration

Postby teha » Friday 11 March 2016 0:41

Thank you Holland for the note about the node.js version

I was indeed outdated

Keep in mind that the link for installing version 4.0.0 have a different path for node.js running on the latest domoticz distribution image so the copy command in the end will be to /home/pi and not for /usr/local, that was probably why I got it wrong in the first place
RPi2 with RFXtrf433(E) at stable, RPi2 with AeonStick S2 on beta, 6*Sonos, Synology DS415play, Axis, Foscam, PhilipsHue

wmn79
Posts: 63
Joined: Monday 13 April 2015 23:19
Target OS: NAS (Synology & others)
Domoticz version: 3.5033
Location: Amsterdam, The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby wmn79 » Friday 11 March 2016 22:26

I've improved my lua script a bit so it makes a distinction between the paused and stopped state. On pause it sets the selector on pause, on stopped it put the selector switch off (different icon, no button selected). Maybe this can be helpful for others.

Code: Select all

-- ~/domoticz/scripts/lua/script_time_SonosKeuken.lua
json = (loadfile "/home/domoticz/domoticz/scripts/lua/json.lua")()

local sonos=assert(io.popen('curl http://192.168.1.4:5005/Keuken/state'))
local status = sonos:read('*all')
sonos:close()
local jsonStatus = json:decode(status)
local sonos_keuken = 'Sonos Keuken'

--print(status)

playerstate = jsonStatus['playerState']
artist = jsonStatus['currentTrack']['artist']
title = jsonStatus['currentTrack']['title']
album = jsonStatus['currentTrack']['album']
streaminfo = jsonStatus['currentTrack']['streamInfo']
   
commandArray = {}

if (playerstate == "PLAYING" or playerstate == "STOPPED" or playerstate == "PAUSED_PLAYBACK") then
   if playerstate == "PLAYING" then
      if   otherdevices[sonos_keuken]~='Play' then
         commandArray[sonos_keuken]='Set Level 10'
         --os.execute('curl http://192.168.1.4:8080/json.htm?type=command&param=switchlight&idx=103&switchcmd=Set%20Level&level=10')
     end
     print('Sonos Keuken status: ' ..playerstate.. ' Artiest: ' ..artist.. ' Titel: '..title.. ' Streaminfo: ' ..streaminfo)
   end
   if playerstate == "PAUSED_PLAYBACK" then
      if   otherdevices[sonos_keuken]~='Pause' then
         commandArray[sonos_keuken]='Set Level 20'
         --os.execute('curl http://192.168.1.4:8080/json.htm?type=command&param=switchlight&idx=103&switchcmd=Set%20Level&level=20')
     end
     print('Sonos Keuken: ' ..playerstate)
   end
   if playerstate == "STOPPED" then
      if   otherdevices[sonos_keuken]~='Off' then
         commandArray[sonos_keuken]='Set Level 0'
         --os.execute('curl http://192.168.1.4:8080/json.htm?type=command&param=switchlight&idx=103&switchcmd=Set%20Level&level=20')
     end
     print('Sonos Keuken: ' ..playerstate)
   end
else
   --print("2")
   print('<font color="red">Sonos Keuken: SONOS-HTTP-API is off-line</font>')
end   

return commandArray

micbou
Posts: 88
Joined: Sunday 01 May 2016 0:34
Target OS: Raspberry Pi
Domoticz version:
Location: NL
Contact:

Re: Step by step guide: Sonos integration

Postby micbou » Saturday 07 May 2016 20:39

Works like a charm now, thanks. However I do have two more questions:

1 - How do I start the server automatically after a reboot?
2 - We have a Sonos in the livingroom and kitchen who are grouped together. I would like to control them grouped and ungrouped. Any advice?

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Sunday 08 May 2016 13:27

H Micbou

Have a look over here; https://www.domoticz.com/forum/viewtopic.php?t=9653&start=280#p76926

copy/past;

Only if you are using Jessie (check via hostnamectl) the following workaround solves the autostart issue;

1. Delete the pm2*.sh file in /etc/init.d
2. sudo pm2 startup systemd –u pi
3. pm2 start nefiteasyhttpserver.sh
4. pm2 save

About the grouping;

If you group Sonos devices, 1 of the devices will be the coordinator. Just find out which of the 2 has the coordinator role, and use that specific device to control music for the group. You can control just the other device (which is not the coordinator) by using the ip address of that specific device. I don't know how to control only the coordinator device. You will first need to ungroup it, dont know if that's supported by http sonos api.

micbou
Posts: 88
Joined: Sunday 01 May 2016 0:34
Target OS: Raspberry Pi
Domoticz version:
Location: NL
Contact:

Re: Step by step guide: Sonos integration

Postby micbou » Sunday 08 May 2016 13:35

Thx Holland. Unfortunately I don't seem to have a pm2*.sh file in /etc/init.d

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Sunday 08 May 2016 13:58

Just skip step 1, then it should work.

I upgraded to jessie recently, and the pm2 file in /etc/init.d is an old pm2 config file, in the previous version of debian (wheezy)
Last edited by Holland on Sunday 08 May 2016 18:55, edited 1 time in total.

micbou
Posts: 88
Joined: Sunday 01 May 2016 0:34
Target OS: Raspberry Pi
Domoticz version:
Location: NL
Contact:

Re: Step by step guide: Sonos integration

Postby micbou » Sunday 08 May 2016 15:12

That worked thanks!

While checking my logs I noticed that I get an error everytime I trigger the Sonos. Funny thing is that everything does work as wanted and expected

Code: Select all

2016-05-08 14:49:24.395 Error: Error opening url: http://192.168.1.112:5005/woonkamer/pause

Holland
Posts: 304
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Postby Holland » Sunday 08 May 2016 18:59

I have that error too

Tried to get it solved; viewtopic.php?f=6&t=10211

But apparently the problem is caused by the sonos http api, since it doesn't return anything if a command is successful received. So for that, the sonos http api developer should be contacted.

piotr
Posts: 29
Joined: Monday 15 July 2013 8:17
Target OS: Linux
Domoticz version: Beta
Location: Drenthe
Contact:

Re: Step by step guide: Sonos integration

Postby piotr » Wednesday 08 June 2016 9:37

Get a free api-key from http://www.voicerss.org/ and you can get spoken (TTS) messages like: "The lights are off" when switching lights off.

usage example : http://yourserver:5005/LiveRoom/say/The lights are off

Ps: did anyone find a way the display the artist/track info in Domoticz ?

dhanjel
Posts: 131
Joined: Tuesday 05 August 2014 22:16
Target OS: Linux
Domoticz version: 3.5146
Location: Sweden
Contact:

Re: Step by step guide: Sonos integration

Postby dhanjel » Wednesday 08 June 2016 16:01

piotr wrote:Ps: did anyone find a way the display the artist/track info in Domoticz ?


Should be fairly simple using a text sensor?
https://www.domoticz.com/wiki/Domoticz_ ... ext_sensor

Would nice with more complex sensors, that are a cominations of other sensors.

This is btw a dzVents-modified version, change the variables according to your local installation.

Code: Select all

return  {
  active = true,
  on = { ['timer'] = 'every minute' },
  execute = function(domoticz)           
   
        local json = (loadfile "/home/daniel/domoticz/scripts/lua/JSON.lua")()

        local sonos=assert(io.popen('curl http://192.168.1.200:5005/Living%20room/state'))
        local sonos_name = 'Vardagsrum, Sonos'

        local status = sonos:read('*all')
        sonos:close()
        local jsonStatus = json:decode(status)

        playerstate = jsonStatus['playerState']

        if (playerstate == 'PLAYING')
        then
            domoticz.devices[sonos_name].level = 10
        elseif (playerstate == 'PAUSED_PLAYBACK')
        then
            domoticz.devices[sonos_name].level = 20
        else
            domoticz.devices[sonos_name].level = 0   
        end
   
  end
}


Attached an icon as well:
Sonos.zip
(8.8 KiB) Downloaded 115 times

Dingetje2004
Posts: 11
Joined: Saturday 04 June 2016 8:10
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Step by step guide: Sonos integration

Postby Dingetje2004 » Friday 17 June 2016 16:14

OLD: How to keep the program running in the background?
I can use domoticz for changing Sonos to Pause or play as long as my terminal service client is open. I use " npm start" to start the server, but it looks like as soon as I close the terminal it stops working...



EDIT: GOT IT working, installed PM2...


Return to “Show your projects”

Who is online

Users browsing this forum: No registered users and 2 guests