Tag Archives: Spotify

Control of the Logitech Media Server using the LogicMachine

I use the Logitech Media Server to stream multi-room audio in our apartment. Lately I wanted to be able to also control the Spotify Plugin from the KNX buttons at home and automate some tasks.

This is a simple user library to allow you to send commands to a player attached to your LMS.

--Functions for Logitech Media Server integration
squeezboxserverip = '192.168.178.2' -- IP of the Logitech Media Server

function unescape(space)
return string.gsub(space, ':', '%%3A') -- the telnet server returns : as %3A
end

function squeezeboxsendcommand(player,command,value)
socket=require('socket')
local client = socket.connect(squeezboxserverip, 9090)
playercommand = player .. ' ' .. command .. ' '
client:send(playercommand .. value .. '\n')
local result=client:receive()
status = string.sub(result, string.len(unescape(playercommand))) --remove the MAC and command to get to the answer
client:close()
os.sleep(0.2)
return(status)
end

function squeezeboxstatus(player)
socket=require('socket')
local client = socket.connect(squeezboxserverip, 9090)
playercommand = player .. ' status'
client:send(playercommand .. '\n')
local result=client:receive()
status = split(result, ' ')
--status = string.sub(result, string.len(unescape(playercommand))) --remove the MAC and command to get to the answer
client:close()
log(status)
return(status)
end

to play a Spotify Playlist you need to add this code to a KNX object.


require('user.squeezebox') --get the user library to access LMS

local player = 'b8:27:eb:ad:6b:c3' --set witch player to access

if (event.getvalue() == true) then
squeezeboxsendcommand(player, 'playlist', 'clear') -- reset any playlist
squeezeboxsendcommand(player, 'playlist add', 'spotify:user:1175125342:playlist:7yCDwOnYZTXFTYymXZxp0t') --get this command from the logs of the LMS when playing the Spotify playlist
squeezeboxsendcommand(player, 'playlist shuffle', 1) -- shuffle, if you want
squeezeboxsendcommand(player, 'play', '') -- now play the list
grp.write('11/6/1', 'Playing Spotify')
else
squeezeboxsendcommand(player, 'playlist', 'clear')
grp.write('11/4/0', false) --turn off player
end