Page 4 of 7

Re: Step by step guide: Sonos integration

Posted: Sunday 19 March 2017 22:06
by Holland
So the http api is running fine. You are running a recent version of the http api. Just to be sure, I made a few changes to the lua code, can you try the most recent version, see below.

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'] = '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

Re: Step by step guide: Sonos integration

Posted: Monday 20 March 2017 8:09
by markiemajax
Hello,

I changed the lua to yours.
I changed:
- ip to the ip of my domoticz (http://10.100.75.113:5005/woonkamer/state)
- line 32, i changed badkamer to Woonkamer/Eetkamer

I get this message in de log:
2017-03-20 08:06:16.879 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_sonos_tv_woonkamer.lua: ...omoticz/scripts/lua/script_device_sonos_tv_woonkamer.lua:32: attempt to concatenate global 'title' (a nil value)

Is the number "32"' the line where the error is?
2017-03-20 16_04_23-10.100.75.113_5005_woonkamer_state.png
2017-03-20 16_04_23-10.100.75.113_5005_woonkamer_state.png (20.37 KiB) Viewed 7086 times
2017-03-20 16_03_58-Domoticz.png
2017-03-20 16_03_58-Domoticz.png (49.84 KiB) Viewed 7086 times
Holland wrote:So the http api is running fine. You are running a recent version of the http api. Just to be sure, I made a few changes to the lua code, can you try the most recent version, see below.

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'] = '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

Re: Step by step guide: Sonos integration

Posted: Thursday 23 March 2017 14:29
by markiemajax
Hello,

I found out that the error is only when the sonos is active. If it is in pause, there is no error.

The switch is not reacting to the sonos. It stays in pause.

Can anyone tell me how to resolve this?

Re: Step by step guide: Sonos integration

Posted: Thursday 23 March 2017 21:16
by Holland
The reason that it errors out is that your stream or mp3 does not contain any meta data.

About the selector switch, the first value should be changed into your idx of the sonos switch

Re: Step by step guide: Sonos integration

Posted: Thursday 23 March 2017 21:50
by markiemajax
Hello,

Thanx. What value do you mean?

Holland wrote:The reason that it errors out is that your stream or mp3 does not contain any meta data.

About the selector switch, the first value should be changed into your idx of the sonos switch

Re: Step by step guide: Sonos integration

Posted: Thursday 23 March 2017 21:51
by Holland
commandArray['UpdateDevice'] = '43|10|10'

Re: Step by step guide: Sonos integration

Posted: Monday 10 April 2017 16:58
by Heursqem
This is what I would love to achieve

#1 - Someone pushes my doorbell
#2 - I capture the current state of my sonos player (medium, song, volume etc) and save this for later
#3 - I play my own sound through the sonos player (ding-dong)
#4 - I go back to to captured/saved state and listen to original music again

Would all of this be possible with this solution?

Re: Step by step guide: Sonos integration

Posted: Tuesday 11 April 2017 1:18
by Holland
Try this, just an example, but it does the trick :lol:


http://ip of raspberry running sonos http api:5005/name of sonos player/say/dingdong/nl

Re: Step by step guide: Sonos integration

Posted: Friday 28 April 2017 15:49
by FaddieB
Heursqem wrote:This is what I would love to achieve

#1 - Someone pushes my doorbell
#2 - I capture the current state of my sonos player (medium, song, volume etc) and save this for later
#3 - I play my own sound through the sonos player (ding-dong)
#4 - I go back to to captured/saved state and listen to original music again

Would all of this be possible with this solution?
This will be super! Is this already coded by someone? :)

Re: Step by step guide: Sonos integration

Posted: Saturday 20 May 2017 14:11
by Asterixenobelix
I followed every step, have the latest versions of everything installed and still a strange error:
After entering http://192.168.1.14:5005/Keuken/state (ip-adress of one of my Sonos-devices), I receive the message:
-bash http etc: bestandsnaam of map bestaat niet (file or directory don't exist).

Using the ip-adres of another device, or of my Raspberry, doesn't change anyting: same message.

Seems like my Pi doesn't know the command "http".

I can't find anything about this on Google.

Anyone any idea?

Re: Step by step guide: Sonos integration

Posted: Saturday 20 May 2017 14:13
by Trigun
Do you see the sonos menu when entering IP-adres:5005?


Sent from my iPhone using Tapatalk

Re: Step by step guide: Sonos integration

Posted: Saturday 20 May 2017 14:27
by Asterixenobelix
No. Just the error-message.

As the Raspberry doesn't recognise the command, it can't show the Sonos-page.

I can see the sonos-api is running. But I think the cause is something else, something in the OS.

Step by step guide: Sonos integration

Posted: Saturday 20 May 2017 17:34
by Trigun
If you re not seeing anything it means the sonos integration was not successful
I'll have a look at it when I'm at home.

Sent from my iPhone using Tapatalk

Re: Step by step guide: Sonos integration

Posted: Saturday 20 May 2017 18:00
by Asterixenobelix
Ok, thanks.

I was surprised by the bash-error: http:// etc., file or directory doesn't exist (in Dutch), as if "http" was not recognised. I hope I'm mistaken.

Is there a way to uninstall everything en re-install the sonos-http-api stuff?

Re: Step by step guide: Sonos integration

Posted: Monday 22 May 2017 18:46
by Asterixenobelix
Anyone else any suggestions about my above mentioned problem? Is there a way to uninstall the sonos-api and install it again? I keep getting the error message when entering the right http-command.

Re: Step by step guide: Sonos integration

Posted: Thursday 25 May 2017 23:04
by leecollings
Heursqem wrote:This is what I would love to achieve

#1 - Someone pushes my doorbell
#2 - I capture the current state of my sonos player (medium, song, volume etc) and save this for later
#3 - I play my own sound through the sonos player (ding-dong)
#4 - I go back to to captured/saved state and listen to original music again

Would all of this be possible with this solution?
Hey, you can already do this with the clipall command (if you want to play the announcement through all Sonos devices on your network).

You can handle all of the above by using:

Code: Select all

http://localhost:5005/clipall/name-of-your-mp3.mp3
(store your mp3 file in /static/clips/)

Re: Step by step guide: Sonos integration

Posted: Monday 29 May 2017 11:20
by rjpost
Hello Guys,

I was wondering if you could provide me with some guidance in regards to getting this Sonos integration to work the way i would like to.

I have the following objective. I want to use a sonos connect ( not the amp version). When I select play on that specific sonos I want my to turn my receiver on and set it to input AUX. I want to achieve this through the Harmony HUB. I can control that perfectly through domoticz however I need to determine whether or not a specific sonos player is playing music.
I wanted to create an event with blocky to sent a command to the harmony hub when the sonos switch is set to status playing.

In order to do that I wanted to use this script to update the switch status to playing, pause, stopped.
I I have been playing around with it but I cant seem to get it to work properly and show the playing or pauze status in the switch.
I can control Sonos fine through domoticz so the api is running as it should.

So what have I done.
- Placed the Lua file in the domoticz scripts folder.
- added a lua script there to with the name Sonos_Kitchen.lua.
- in domoticz I added a dummy device called Sonos_Kitchen
- based on this dummy switch I created a selector switch with the name Sonos_Kitchen (idx 166)
- The script is a copy from the one that is posted on the 19th of march 2017.
- I have changed the Curl adres to : http://localhost:5005/kitchen/state.
- I have changed the commandArray to commandArray['UpdateDevice'] = '166|20|20' where the 166 is the idx of my switch sonos_kitchen.

I think am stuck with the script and the naming conventions that are used. I'm either interpreting them wrong of I'm mis using them.
Can some explain to me where I should change the variables so it will work in my setup.
any help is appreciated.
Thanks a lot.

- Ronald-

Re: Step by step guide: Sonos integration

Posted: Wednesday 31 May 2017 13:22
by Heursqem
leecollings wrote:
Heursqem wrote:This is what I would love to achieve

#1 - Someone pushes my doorbell
#2 - I capture the current state of my sonos player (medium, song, volume etc) and save this for later
#3 - I play my own sound through the sonos player (ding-dong)
#4 - I go back to to captured/saved state and listen to original music again

Would all of this be possible with this solution?
Hey, you can already do this with the clipall command (if you want to play the announcement through all Sonos devices on your network).

You can handle all of the above by using:

Code: Select all

http://localhost:5005/clipall/name-of-your-mp3.mp3
(store your mp3 file in /static/clips/)
But how could I store the actual state, so I can resume after the clipall command?

Re: Step by step guide: Sonos integration

Posted: Wednesday 31 May 2017 14:06
by leecollings
It stores the current state within the command. Try it out and you'll see.

I've tested it, with a Play 1 playing a song, it pauses, plays the mp3 file, and then resumes the previously playing song.

Re: Step by step guide: Sonos integration

Posted: Wednesday 31 May 2017 14:51
by Heursqem
ok, great, will try it out

I can only address all Players at the same time - I can't just choose 1?