Author Topic: Script adjust 2nd Sonos same volume as 1st one  (Read 270 times)

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Script adjust 2nd Sonos same volume as 1st one
« on: May 19, 2017, 06:15:00 pm »
Trying to add variable watch to Lua startup,
I want to check if the volume change on sonos device 8 and set same volume to sonos device 9.
Am i close?

Code: [Select]

-- Set up variable-watch for device 9
luup.variable_watch("doChange9","urn:upnp-org-com:serviceId:RenderingControl","Volume",9)

volume = tonumber((luup.variable_get("urn:upnp-org-com:serviceId:RenderingControl","Volume",9)))
volumeset = volume
-- Process variable-watch callback for device 9. Chamge Sonos Cuisine Volume
function doChange9()
   luup.call_action("urn:upnp-org-com:serviceId:RenderingControl", "SetVolume", {DesiredVolume = tonumber(volumeset)}, 8)
end

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Script adjust 2nd Sonos same volume as 1st one
« Reply #1 on: May 20, 2017, 03:52:39 pm »
You are close, but there is an issue that you need to fix.  Also, you'll probably need to do a little debug and look at your logs.  Main issue I see is that only the function runs on the watch change, so the value in volume and volumeset will only be set once when lua startup runs.  You need to get these into the function itself.  Try the function in the test window first and make sure it works the way you want, then get the watch to work.

Also, this code watches device 9 and changes device 8, but your message said you want the opposite.  You need to swap the 8's and 9's if you want to watch device 8 and set it on device 9.
Code: [Select]
-- Set up variable-watch for device 9
luup.variable_watch("doChange9","urn:upnp-org-com:serviceId:RenderingControl","Volume",9)

-- Process variable-watch callback for device 9. Chamge Sonos Cuisine Volume
function doChange9()
  local volume = tonumber((luup.variable_get("urn:upnp-org-com:serviceId:RenderingControl","Volume",9)))
  luup.log("Volume from Device 9 is: " .. volume) -- added for debug
  luup.call_action("urn:upnp-org-com:serviceId:RenderingControl", "SetVolume", {DesiredVolume = volume}, 8)
end
Note that the MicasVerde wiki shows slightly different parameters for the call_action, but I see that other posters in the forum use what you have.  So try it first (outside the watch), make sure it works, then get it to work in the watch.
http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_call_action

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Re: Script adjust 2nd Sonos same volume as 1st one
« Reply #2 on: May 21, 2017, 11:42:23 am »
Watch change works but not the fonction

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Script adjust 2nd Sonos same volume as 1st one
« Reply #3 on: May 21, 2017, 02:14:10 pm »
Okay, need to know if the volume was read correctly from device 9, or if it didn't get set in device 8.  Did you see the log message?  Does it have the value you expect?

Can you run this with a hard coded value in the test window?  Does it work?
Code: [Select]
local volume = 10 -- pick a value to use
luup.call_action("urn:upnp-org-com:serviceId:RenderingControl", "SetVolume", {DesiredVolume = volume}, 8)

If the above works, try these 3 lines to see if it will pick up from 9 and set 8:
Code: [Select]
  local volume = tonumber((luup.variable_get("urn:upnp-org-com:serviceId:RenderingControl","Volume",9)))
  luup.log("Volume from Device 9 is: " .. volume) -- added for debug
  luup.call_action("urn:upnp-org-com:serviceId:RenderingControl", "SetVolume", {DesiredVolume = volume}, 8)