Jump to content
  • 0

Is there anyway to control Sonos Plugin via scenes ?


Question

Hello friends,

 

I would like to control my sonos devices via scenes. It seems not possible to do that with common ways. So maybe someone in this forum already did that via http methods, rest apis etc. Would be great help if someone has any idea about that.

 

Thanks in advance.

Link to post
Share on other sites

18 answers to this question

Recommended Posts

  • 0

I think you can call buttons on the virtual device that is availble in the forum, but not on the plugin.

Yea that's why I'm looking for it. My plan is creating a virtual device then call buttons.. Not possible to call plugin's button.  

Link to post
Share on other sites
  • 0

Hello friends,

 

I would like to control my sonos devices via scenes. It seems not possible to do that with common ways. So maybe someone in this forum already did that via http methods, rest apis etc. Would be great help if someone has any idea about that.

 

Thanks in advance.

 

Here you can see sample of such code for SONOS plugin:

You can control your SONOS by interface in two ways:

1) Create a virtual device and create in it appropriate for you number of buttons.

The example code for button “play” below:

HC2 = Net.FHttp(“192.168.80.85”) IP of HC

HC2:setBasicAuthentication(“admin”,“admin”) —change your credentials

response = HC2:GET ( "/api/plugins/callUIEvent?deviceID=140&elementName=play_Button&eventType=onReleased ") This code you can achieve by plugin “Firebug” which is add to web browser.

The IP Address: 127.0.0.1

TCP Port: 11111

You can add other buttons in the same way, change only last line: “/api/plugins/callUIEvent?deviceID=140&elementName=play_Button&eventType=onReleased”.

Then you can control your virtual devices by a scene. It can be a block scene.

2) You can create a LUA scene which will be control your SONOS.

An example for push the “pause” button is below:

-[[

% properties

% globals

-]]

local function updateDevice(successCallback, errorCallback)

local http = net.HTTPClient() http:request(‘http://127.0.0.1:11111/api/plugins/callUIEvent?deviceID=140&elementName=pause_Button&eventType=onReleased’, { options = { method = ‘GET’ }, success = successCallback, error = errorCallback })

end

deviceId = updateDevice(function(resp)

print("Status: " .. resp.status)

end,

function(err)

print(‘error’ .. err)

end

)

  • Like 1
Link to post
Share on other sites
  • 0

Dont complicate things, unless you want to that is  :-)
 
Use the built in Fibaro Sonos plugin and do simple LUA scenes that you then call.

Gets rid of all the virtual device hassles and code. 

 

Then call the scenes you want.

--[[
%% properties
%% globals
--]]

local ID = 583-- Insert your Fibaro Sonos plugin ID here

-- Supported commands via Fibaro Sonos Plugin --

fibaro:call(ID, "setMute", "X")    -- X = 1 or 0
fibaro:call(ID, "setVolume", "X")  -- X = volume 0 to 100
fibaro:call(ID, "turnOff")         -- Off
fibaro:call(ID, "turnOn")          -- On

Or if you want to use a virtual device slider for ex the volume then just add a slider and then place this code in the slider:

local ID = 583 -- Insert your Fibaro sonos plugin ID here
local volume = tonumber(_sliderValue_) -- Convert slider value to a number
fibaro:call(ID, "setVolume", volume)  -- Set the volume of your sonos device

Done!

Link to post
Share on other sites
  • 0

Or look at the link in the address bar of your plugin.

Go to your device and then check the address bar.

XYZ Will be your device id.

http://HC2IP/fibaro/en/devices/plugins.html?id=XYZ

Link to post
Share on other sites
  • 0

 

Here you can see sample of such code for SONOS plugin:

 

Your code got me inspired!

Thank you for this way of controlling Sonos, that was nice, clean and simple!

 

So i made a scene so you can control Sonos from a regular button and sceneActivation using the S2 button.

(or use the same code in any VD och scene you want for simple Sonos control)

 

Using only 1 S2 button you can Play, Pause, Next song, Prev song and volume upp + down.

 

 1 click = Play and Pause

 2 click = Next song

 3 click = Prev song

 Press and hold = Volume change upp and down (works just like a dimmer)

--[[
%% properties
117 sceneActivation
%% globals
--]]

--
--  Use key S2 and sceneActivation to control Sonos.
--  To get started follow settings below. 
--  Create a LUA scene and paste this code in it.
--  Create a Fibaro Sonos plugin and configure accordingly.
--  Activate sceneActivation on your Fibaro device (Setting 40 on fibaro dimmer v1. Setting 28 on dimmer v2)
--  Create a variable, i use "sonos_control" in my system and set it to 0. (Change it in settings below if you have a different name)
--  Enter sceneactivation ID above below %% properties, then enter all settings below.
--  Done! Control your Sonos audio with just 1 button (S2 in this perticular case)
--
--  1 click = Play and Pause
--  2 click = Next song
--  3 click = Prev song
--  Press and hold = Volume change upp and down (works just like a dimmer)
--
--  ToDo: Error handling.. Boring!
--

-- ENTER SETTINGS HERE --
local device_ID = 117 	    -- ID of device with scenectivation enabled
local sonos_ID = 611	    -- ID if sonos device plugin
local var_sonos_vol = "sonos_control"	--Name of your variable to control dimming of audio

-- Function Settings (Not needed to change unless you want to) --
local sonos_volume = 0 		  -- Volume variable
local sonos_add_volume = 2	  -- Volume Step when dimming volume
local sonos_base_vol = 5	  -- Base volume when going over sonos_max_vol
local sonos_max_vol = 50	  -- Maximum volume possible, to protect your ears..
local sonos_volume_up_down  = 0	  -- Changes the way that volume is "dimmed" 0 = down. 1 = UP
local loop_delay = 300 		  -- Delay for volume change loop
local vol_up =	1 		  -- Volume up variable
local vol_down = 0 		  --Volume down variable

-- Get keypress
local button_ID = tonumber(fibaro:getValue(device_ID, "sceneActivation")) -- Get keypress number.

-- Get volume change variable
sonos_volume_up_down = fibaro:getGlobal(var_sonos_vol)
sonos_volume_up_down = tonumber(sonos_volume_up_down)

-- Get volume from sonos plugin.
sonos_volume = tonumber(fibaro:getValue(sonos_ID, "volume"))

-- Execute Sonos command (Thanks to cag014 in the forum for this way of sending a command to sonos plugin!!)
function sonos_event(event)
  
  local function sonos_send_command(successCallback, errorCallback)
	local http = net.HTTPClient() 
    	http:request("http://127.0.0.1:11111/api/plugins/callUIEvent?deviceID=" .. sonos_ID .. "&elementName=" .. event .. "_Button&eventType=onReleased", { options = { method ='GET' }, success = successCallback, error = errorCallback })
	end

  	sonos_send_command(function(resp) print("Event:" .. event .. " Status: " .. resp.status) end, function(err) print("Event:" .. event .. "Error" .. err) end)
  
end


--  Volume change
if button_ID == 22 then
  
  -- Switch the status of global variable to swith the direction of volume change next keypress
  if sonos_volume_up_down == vol_up then
     fibaro:setGlobal(var_sonos_vol, vol_down)
  else
     fibaro:setGlobal(var_sonos_vol, vol_up)
  end
  
  -- Get current volume from plugin
  sonos_volume = tonumber(fibaro:getValue(sonos_ID, "volume"))
  fibaro:debug("Changing volume from:" .. sonos_volume)
  
  repeat -- Loop to "dim" the volume up or down
  
    if (sonos_volume_up_down == vol_down) then
      sonos_volume = (sonos_volume - sonos_add_volume)
    else
      sonos_volume = (sonos_volume + sonos_add_volume)  
    end  
  
    if sonos_volume > sonos_max_vol then 
      fibaro:call(sonos_ID, "setVolume", sonos_base_vol)
      sonos_volume = sonos_base_vol
      fibaro:debug("Max volume!, Volume is reset!")
      fibaro:setGlobal(var_sonos_vol, vol_up)
      break
    end
  
    if sonos_volume < 0 then 
      fibaro:call(sonos_ID, "setVolume", 0)
      sonos_volume = 0
      fibaro:debug("Minimum volume!, Volume is reset!")
      fibaro:setGlobal(var_sonos_vol, vol_up)
      break
    end  
    
    fibaro:call(sonos_ID, "setVolume", sonos_volume)
 
    fibaro:sleep(loop_delay)
  
  until tonumber(fibaro:getValue(device_ID, "sceneActivation")) == 23   -- End loop when button is released
  
  fibaro:debug("New volume set:" .. sonos_volume)
  
end


--  Next track
if button_ID == 24 then

  sonos_event("next")
  
end  


-- Prev track
if button_ID == 25 then

  sonos_event("prev")
  
end 


-- Play and pause
if button_ID == 26 then
  
  local sonos_state = tostring(fibaro:getValue(sonos_ID, "state"))
	
  	if sonos_state == "PAUSED_PLAYBACK" then
      	fibaro:call(sonos_ID, "turnOn")
      	fibaro:call(sonos_ID, "setMute", "0")
	sonos_event("play")
    end
  
  	if sonos_state == "PLAYING" then
      	sonos_event("pause")
    end
  
end 

*added small bugfixes and stuff..

Link to post
Share on other sites
  • 0

I have a question about this. I'm trying to create a lua-scene where I would like to turn my Sonos off. I use the following command:

 

fibaro:call(134, "turnOff")

 

But I can't get it to work. To see if there was something wrong with the plugin I used the command fibaro:call(134, "setVolume", "30") to change the volume, and that works fine.

 

And I have no problem controlling it with the plugin-interface.

 

Does anyone have any idea what could be the problem? Has anyone encountered the same problem?

Link to post
Share on other sites
  • 0
On 2016-04-07 at 9:37 PM, pomfritz said:

fibaro:call(134, "turnOff")

 

I'm so happy for finding this. You are all incredibly talented.

 

I had the same problem as you pomfritz and could also change the volume just like you. I experimented with pause and play instead of turnOff and turnOn and got them to work as they should. Maybe Fibaro has done some changes to the plugin from on version to another? (I run 4.100)

 

fibaro:call(134, "pause");  

fibaro:call(134, "play");  

 

 

Link to post
Share on other sites
  • 0
35 minutes ago, abigbear said:

 

I'm so happy for finding this. You are all incredibly talented.

 

I had the same problem as you pomfritz and could also change the volume just like you. I experimented with pause and play instead of turnOff and turnOn and got them to work as they should. Maybe Fibaro has done some changes to the plugin from on version to another? (I run 4.100)

 

fibaro:call(134, "pause");  

fibaro:call(134, "play");  

 

 

 

HC never show all commands and functions that are available in particular plugin. Since there is never any help or short description of the plugin only what you can do is play with it. That is really lame. So I played and except "pause" and "play" you have also commands "stop", "next", "prev" and by the way mute is not working on my HC2 4.100 with plugin!

 

Link to post
Share on other sites
  • 0

 

1 hour ago, Sankotronic said:

HC never show all commands and functions that are available in particular plugin. Since there is never any help or short description of the plugin only what you can do is play with it. That is really lame. So I played and except "pause" and "play" you have also commands "stop", "next", "prev" and by the way mute is not working on my HC2 4.100 with plugin!

 

Great, even though strange with the mute thing. I tried variants like muteOn and muteon but those did not work either. 

 

Any ideas on how to determine if the player is playing or if it's paused or stopped? I've tried the simple ones in case there were fields for Status and status and Time, time, timeRun, timerun but with no luck.

Link to post
Share on other sites
  • 0

 

1 hour ago, Sankotronic said:

HC never show all commands and functions that are available in particular plugin. Since there is never any help or short description of the plugin only what you can do is play with it. That is really lame. So I played and except "pause" and "play" you have also commands "stop", "next", "prev" and by the way mute is not working on my HC2 4.100 with plugin!

 

Great, even though strange with the mute thing.  

 

 

I just found some documentation (very cryptic) on http://<HC2IP>/api/devices/<deviceid> and when I put my deviceid in there with the correct IP address I got a long JSON feed where I found more variables. Like state which tells if the player is playing or not. Could not get mute to work with this information either though. But state works fine.

 

local state = fibaro:getValue(id,'state')

 

 

Link to post
Share on other sites
  • 0
48 minutes ago, abigbear said:

 

 

Great, even though strange with the mute thing. I tried variants like muteOn and muteon but those did not work either. 

 

Any ideas on how to determine if the player is playing or if it's paused or stopped? I've tried the simple ones in case there were fields for Status and status and Time, time, timeRun, timerun but with no luck.

Hi @abigbear,

 

Bellow ones are actually listed :-) Just use and you will get answer like "PLAYING" or "STOPPED".

fibaro:getValue(571, "state")

to check if it is muted it returns 1 for muted and 0 for not

fibaro:getValue(571, "mute")

If you want to now volume that is set then use

fibaro:getValue(571, "ui.volume_Slider.value")

For the rest you can play yourself. Sonos plugin was first thing I installed almost a year ago but I wasn't so happy with it. Then I replace it with virtual device Sonos remote made by Krikroff. It is also not perfect but it has ability to play TTS messages what I found very handy so that my HC2 can tell me some notifications. There is also one very nice solution for controlling Sonos speakers but requires small server like raspberry PI and some software for which I can't find at the moment

Link to post
Share on other sites
  • 0

Hi,

 

Thank you all for this. I prefer to go with the plugin, easier to use. 

 

Does anyone know what is the command for the Sonos plugin to preselect a playlist or radio station before sending the play command ?

 

Thank you.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...