Bluesound in Domoticz

Alexa, Google Home and Siri

Moderator: leecollings

Post Reply
alveman
Posts: 21
Joined: Wednesday 29 January 2014 22:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Bluesound in Domoticz

Post by alveman »

Someone how has conneted Bluesound to Domoticz and like to share how. I have found some info but my skills is not good enough.

https://github.com/venjum/bluesound

https://helpdesk.bluesound.com/discussi ... php?t=2293

Martin


Skickat från min iPad med Tapatalk
possm
Posts: 2
Joined: Saturday 07 October 2017 14:28
Target OS: Linux
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by possm »

This is my setup for my Bluesound speakers. At this moment all I do is volume control through Domoticz
I've not used the mentioned API because i think the Bluesound speaker AP itself is already useable enough. :D

1 create a Virtual Dummy hardware device: Bluesound
Schermafbeelding 2017-10-07 om 14.31.04.png
Schermafbeelding 2017-10-07 om 14.31.04.png (26.61 KiB) Viewed 6025 times
2 Create your Virtual Switches (the speakers)
Schermafbeelding 2017-10-07 om 14.31.39.png
Schermafbeelding 2017-10-07 om 14.31.39.png (74.71 KiB) Viewed 6025 times
Schermafbeelding 2017-10-07 om 14.30.46.png
Schermafbeelding 2017-10-07 om 14.30.46.png (42.36 KiB) Viewed 6025 times
3 LUA script to translate the Domoticz controls into http requests for the speakers. (Set the script to "Device")

Code: Select all

------------------------------------------------------------------------------------------
-- Version 1 (20171006)
--
-- Domoticz lua script to convert the Domoticz switch commands to control the speaker
-- Creates a http request based on a list of unique IP and passes the volume values to the speakers 
-------------------------------------------------------------------------------------------
-- Poss'M (c)2017
-------------------------------------------------------------------------------------------
-- Define your device IP-addresses (Bluesound devices respond to port 11000)
-- { <IP>, <Virtual Switch Name>}
Devices = {}
Devices [1] = {"192.168.1.60:11000", 'Speaker Woonkamer'}
Devices [2] = {"192.168.1.61:11000", 'Speaker Keuken'}

commandArray = {}
-- Loop trough BlueSound speakers and set the volume to right speaker
for i,device in ipairs(Devices) do
    BSVolControl = 'http://' .. device[1] .. '/Volume?level=' .. otherdevices_svalues[device[2]]
    if (devicechanged[device[2]]) then
            commandArray['OpenURL']=BSVolControl
            print(device[2] .. " (".. device[1] ..") set to " ..  otherdevices_svalues[device[2]] .."%")
    end
end
return commandArray
4 Syncing the volume of the speaker with Domoticz through a LUA script.
When the volume of the speaker is set outside of Domoticz this script will sync its interface with the actual volume of the speaker. (Set the script to "Time")

Code: Select all

------------------------------------------------------------------------------------------
-- Version 1 (20171006)
--
-- Domoticz lua script to convert XML output from Bluesound Speakers to Domoticz commands
-- Reads the status based on a list of unique IP and passes the volume values to virtual switches in Domoticz
-------------------------------------------------------------------------------------------
-- Original XML_Capture function by RATA1 at Domoticz-forum, adapted by Poss'M (c)2017
-------------------------------------------------------------------------------------------
commandArray = {}

function XML_Capture(cmd,flatten)
   local f = assert(io.popen(cmd, 'r'))
   local s = assert(f:read('*a'))
   f:close()
   if flatten  then
      s = string.gsub(s, '^%s+', '')
      s = string.gsub(s, '%s+$', '')
      s = string.gsub(s, '[\n\r]+', ' ')
   end
   return s
end

-- Set debug to true to print to log file.
debug = false

-- Define your device IP-addresses (Bluesound devices respond to port 11000)
-- { <IP>, <Virtual Switch Name>}
Devices = {}
Devices [1] = {"192.168.1.60:11000", 'Speaker Woonkamer'}
Devices [2] = {"192.168.1.61:11000", 'Speaker Keuken'}

for i,device in ipairs(Devices) do
    if debug == true then
            print("Reading values from: 'http://".. device[1] .."/Volume'")
    end
    
    -- Read the XML data from the device
    XML_string=XML_Capture("curl -s 'http://".. device[1] .."/Volume'",1)
    valid = string.find(XML_string, "<volume>")    -- check we are looking in the right place
    
        if debug == true then
            print(XML_string)
        end
       
        if valid == nil then
            print ("Bad XML status read - info NOT updated")
        else
        -- Find in the XML_string the info-fields based on their labels
        -- Determine start/stop-positions of the values within the substrings
        v_open = string.find(XML_string,"<volume>")+8      -- read volume string
        v_close = string.find(XML_string,"</volume>")-1      -- read volume string
        volume= string.sub(XML_string,v_open,v_close)
        
        -- If volume levels mismatch with the Domoticz interface, update the Virtual Switch with the level of the Speaker
        if (otherdevices_svalues[device[2]] ~= volume) then
             commandArray[device[2]] = 'Set Level ' .. volume
        end
     end
 end
return commandArray
Last edited by possm on Wednesday 11 October 2017 9:20, edited 2 times in total.
alveman
Posts: 21
Joined: Wednesday 29 January 2014 22:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by alveman »

Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?

Code: Select all

2017-10-09 21:19:11.110 User: Admin initiated a switch command (764/V-rum/Set Level) 
2017-10-09 21:19:12.013 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound2.lua 
2017-10-09 21:19:12.086 LUA: V-rum (192.168.1.20:11000) set to 11% 
2017-10-09 21:19:12.087 EventSystem: Fetching url... 
2017-10-09 21:19:12.087 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound1.lua 
2017-10-09 21:19:12.162 (El) Light/Switch (V-rum) 
2017-10-09 21:19:13.114 LUA: V-rum (192.168.1.20:11000) set to 6% 
2017-10-09 21:19:13.115 EventSystem: Fetching url... 
2017-10-09 21:19:13.115 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound1.lua 
2017-10-09 21:19:13.190 (El) Light/Switch (V-rum) 
possm
Posts: 2
Joined: Saturday 07 October 2017 14:28
Target OS: Linux
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by possm »

alveman wrote: Monday 09 October 2017 21:26 Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?
Yes thats is correct, try setting the second script to "Time", and the first script to "Device".
alveman
Posts: 21
Joined: Wednesday 29 January 2014 22:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by alveman »

possm wrote:
alveman wrote: Monday 09 October 2017 21:26 Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?
Yes thats is correct, try setting the second script to "Time", and the first script to "Device".
Works perfectly now! Thanx


Skickat från min iPhone med Tapatalk
GJvdP
Posts: 19
Joined: Thursday 30 November 2017 0:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Arnhem, the Netherlands
Contact:

Re: Bluesound in Domoticz

Post by GJvdP »

Hello fellow bluesound users,
I own 3 Node1 mediastreamers. They react to the same command as the speakers (I checked)
I followed the above steps as far as I could understand them (I am new to domotics abd lua) and saved the scripts in the lua directory under "script_device_Bluesound.lua and script_time_bluesound,lua. Edited the IP addresses of the 3 Node1's.
Created de Bluesound dummy hardware.

First question: Do I create the dummy switches from this hardware or through Switches/Manual/Bluesound etc. (Did both but none seem to work)

If I look in the log I can see that the scripts read the correct value for the volumes but I don't see the switches change accordingly.
I also see the following error msg in the log:

2018-01-30 17:25:02.159 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_Bluesound.lua: /home/pi/domoticz/scripts/lua/script_device_Bluesound.lua:19: attempt to concatenate field '?' (a nil value)

Hope you can straighten me out, all help is welcome.

Thanks, GJ
GJvdP
Posts: 19
Joined: Thursday 30 November 2017 0:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Arnhem, the Netherlands
Contact:

Re: Bluesound in Domoticz

Post by GJvdP »

Update:
Found that when my router restarted the IUOP addresses were not as fixed as I thought. Added the mac addresses in the router and that problem is fixed now.
While editing the lua scripts found that one of the Nodes had a spelling error. Changed that and now the error msgs are gone!
The volume controls are now set to 0 on the nodes even if I turn them up. Next pass they are 0 again.
And it shows in the log:

2018-01-31 13:35:00.692 EventSystem: Fetching url...
2018-01-31 13:35:00.692 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Bluesound.lua
2018-01-31 13:36:00.314 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_Bluesound.lua
2018-01-31 13:36:00.349 LUA: Node1 Living (192.168.16.46:11000) set to %

If I understand the problem and if I master lua script I will try to create a script that does more than just set the volume.
So still all help is welcome

GJ
GJvdP
Posts: 19
Joined: Thursday 30 November 2017 0:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Arnhem, the Netherlands
Contact:

Re: Bluesound in Domoticz

Post by GJvdP »

Got it. It is the way you create the dummy switches. They actually have to be switches instead of soundlevel, as I falsely thought.
Later to change in Switches to dimmer. Works like a charm.
basleeftink
Posts: 2
Joined: Tuesday 17 June 2014 22:41
Target OS: NAS (Synology & others)
Domoticz version: 4.9796
Contact:

Re: Bluesound in Domoticz

Post by basleeftink »

Hi possm
Loveley script, i got it working for the control but have a problem reading the value´s.
The request on http://192.168.1.200:11000/Volume returns:

Code: Select all

<volume db="-100">0</volume>
The script looks in the XML for <volume> but it can not be found because the db= is added.
Does anyone know how to solve this? do you have an updated script?
alveman
Posts: 21
Joined: Wednesday 29 January 2014 22:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by alveman »

basleeftink wrote:Hi possm
Loveley script, i got it working for the control but have a problem reading the value´s.
The request on http://192.168.1.200:11000/Volume returns:

Code: Select all

<volume db="-100">0</volume>
The script looks in the XML for <volume> but it can not be found because the db= is added.
Does anyone know how to solve this? do you have an updated script?
Change to http://192.168.1.200:11000/Status and it will work.

Martin


Skickat från min iPhone med Tapatalk
basleeftink
Posts: 2
Joined: Tuesday 17 June 2014 22:41
Target OS: NAS (Synology & others)
Domoticz version: 4.9796
Contact:

Re: Bluesound in Domoticz

Post by basleeftink »

Thanx Martin,
That works for me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest