Control PiMusicbox with lua

Moderator: leecollings

Post Reply
Gijs69
Posts: 5
Joined: Friday 06 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Control PiMusicbox with lua

Post by Gijs69 »

I created this script to control my http://www.PiMusicbox.com with Domoticz.

Code: Select all

-- Script to control my Pi Musicbox
--
-- Usage of a two Dummy devices: Selector (MusicBox radio) and Dimmer (MusicBox volue)
--
--  

commandArray = {}

if devicechanged['MusicBox volume'] then
  if (devicechanged['MusicBox volume']=='Off') then
	os.execute('mpc --host=musicbox.local stop')
	print('Switch off Radio')
  else
     	if (devicechanged['MusicBox volume']=='On') then 
	   os.execute('mpc --host=musicbox.local play')
    	   print('Switch on Radio')
	else
	   os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	   print('Set volume to '..otherdevices_svalues['MusicBox volume'])
        end
  end
end

if (devicechanged['MusicBox radio']=='100% NL') then 
	os.execute('mpc --host=musicbox.local clear')
	os.execute('mpc --host=musicbox.local add http://stream.100p.nl/100pctnl.mp3')
	os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	os.execute('mpc --host=musicbox.local play 1')
	print('Switched Radio to 100% NL')
end
if (devicechanged['MusicBox radio']=='3FM Serious Radio') then 
	os.execute('mpc --host=musicbox.local clear')
	os.execute('mpc --host=musicbox.local add http://icecast.omroep.nl/3fm-bb-mp3.m3u')
	os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	os.execute('mpc --host=musicbox.local play 1')
	print('Switched Radio to 3FM')
end
if (devicechanged['MusicBox radio']=='Studio Brussel') then 
	os.execute('mpc --host=musicbox.local clear')
	os.execute('mpc --host=musicbox.local add http://mp3.streampower.be/stubru-high.mp3')
	os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	os.execute('mpc --host=musicbox.local play 1')
	print('Switched Radio to Studio Brussel')
end
if (devicechanged['MusicBox radio']=='Radio 538') then 
	os.execute('mpc --host=musicbox.local clear')
	os.execute('mpc --host=musicbox.local add http://vip-icecast.538.lw.triple-it.nl/RADIO538_MP3.m3u')
	os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	os.execute('mpc --host=musicbox.local play 1')
	print('Switched Radio to Radio 538')
end
if (devicechanged['MusicBox radio']=='Sky radio') then 
	os.execute('mpc --host=musicbox.local clear')
	os.execute('mpc --host=musicbox.local add http://8573.live.streamtheworld.com:80/SKYRADIO_SC')
	os.execute('mpc --host=musicbox.local volume '..otherdevices_svalues['MusicBox volume'])
	os.execute('mpc --host=musicbox.local play 1')
	print('Switched Radio to Sky Radio')
end

return commandArray
Domoticz / Raspberry Pi / Razberry / MySensors / SolarEdge API
Chiel
Posts: 6
Joined: Wednesday 02 September 2015 0:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Control PiMusicbox with lua

Post by Chiel »

Hi Gijs, tried to use your script, but can you tell me which dummy devices I have to make and which names I have to give the lua script?

Created script_devices_musicbox_keuken.lua and put that in the lua scripts folder, created two dummy devices but in the logging I receive different kind of errors:
2017-01-09 20:21:47.817 Error: EventSystem: in /usr/local/domoticz/var/scripts/lua/script_device_musicbox_keuken.lua: ...moticz/var/scripts/lua/script_device_musicbox_keuken.lua:27: attempt to concatenate field 'MusicBox volume' (a nil value)
2017-01-09 20:21:47.832 Error: EventSystem: in /usr/local/domoticz/var/scripts/lua/script_device_musicbox_volume.lua: ...moticz/var/scripts/lua/script_device_musicbox_volume.lua:27: attempt to concatenate field 'MusicBox volume' (a nil value)
2017-01-09 20:21:47.781 (MusicBox) Light/Switch (MusicBox radio)

Could you please help me? Thnx!
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Control PiMusicbox with lua

Post by elmortero »

Hi gijs69,

Until I saw your post I only used local playlists, thanks to your script I now have a mix of playlist and streaming radio.
Thanks a lot!
I just finished a script that gets the currently playing track and updates it to a text sensor to display it in Domoticz.

Do you by chance know if there is a way to make mopidy/mpd perform an action when it starts a new track?
I would like to make it trigger a hidden switch which then triggers the script to get the song name.

This Lua script needs dzvents

Code: Select all

return {
	active = true,
	on = {
			['timer'] = 'every minute'
	},
	--[[script runs every minute, max delay in updating new song is 59 seconds (give or take :-) )
		I am looking for a way to make MPD trigger a (hidden) switch which then can trigger this script
		so the text sensor updates in real-time and this script will not run unnecessary
		--]]
	
	-- persistent variable to store the song name so we can check if it is the same track playing
	data = {
        previous = {initial = nil}
		},
	execute = function(domoticz)
	-- getting the song name if MPD uses other port number -p <portnumber> needs to be added
	local rawname = io.popen("mpc -h 192.168.1.12 current")
	local fullname = rawname:read("*a")
	rawname:close()
	-- cropping the song name to 25 characters (for later use in a text sensor)
	local currentsong = string.sub(fullname, 1, 25)
	-- read the name that was stored last time the script ran
	local prevsong = tostring(domoticz.data.previous)
	local disp = domoticz.devices['Playing']		-- declaring the name of the text sensor
	-- check if current song is different from the stored one
				if currentsong ~= prevsong then
				domoticz.log("Current song is : " ..currentsong, domoticz.LOG_INFO)  -- only needed while testing
	--[[ current song is not the same as previous stored one so we overwrite
		 the content of previous with the current song and update the text sensor with the current track
	--]]
				domoticz.data.previous = currentsong
				disp.updateText(currentsong)

				end
	
	end
}
Artemgy
Posts: 11
Joined: Friday 17 March 2017 17:17
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.7017b
Contact:

Re: Control PiMusicbox with lua

Post by Artemgy »

Thanks for these Lua scripts, it's about time I rolled up my sleeves and did some real work integrating Domoticz and these look like a great start :)
If you are still looking for an alternative to polling MPD on a regular basis, then perhaps the MPC IDLE command will help.

Code: Select all

mpc idle player # block until the player changes songs
mpc current     # outputs "Artist Name - Song Name" onto stdout
Credit - https://stackoverflow.com/a/34905959
Help and Syntax: https://www.musicpd.org/doc/protocol/co ... s_commands

This is supposed to work with the Mopidy implementation as well as Kellerman's classic MPD (the one most people use).
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests