Skip to content

Instantly share code, notes, and snippets.

@C-Duv
Last active December 9, 2017 12:54
Show Gist options
  • Save C-Duv/03de85cafc2cd2324e9d856d5889e5cb to your computer and use it in GitHub Desktop.
Save C-Duv/03de85cafc2cd2324e9d856d5889e5cb to your computer and use it in GitHub Desktop.
A LUA script for Domoticz that controls an MPD server volume (via `mpc` client) under the orders of a dummy Domoticz dimmer
--
-- LUA script for Domoticz that controls an MPD server (via `mpc` client) under
-- the orders of a dummy Domoticz dimmer device.
--
-- No configuration required on the device, the LUA script listen to order such
-- as "Set Level: 42 %" and sends adequate `mpc volume` commands.
--
commandArray = {}
-- Name of the dummy Domoticz dimmer:
DomDevice = 'MPD radio'
if devicechanged[DomDevice] then
--print(DomDevice..' received order: '..devicechanged[DomDevice]) -- DEBUG:
if devicechanged[DomDevice] == 'On' then
--print(DomDevice..' should be set to: On') -- DEBUG:
os.execute('mpc play')
elseif string.match(devicechanged[DomDevice], 'Set Level: ') then -- "Set Level: 42 %" type of order
--print(DomDevice..' level changed ('..otherdevices_svalues[DomDevice]..')') -- DEBUG:
-- Compute the volume: Use the "Set Level: 42 %" order and strip anything that's not a digit
volume = devicechanged[DomDevice]:gsub('%D', '')
volume = tonumber(volume)
-- Supports level=0 (even though Domoticz seems to handle it: Setting device's level/slider to "0", sets device to "Off")
if volume == 0 then
--print('Asked level for '..DomDevice..' is 0') -- DEBUG:
commandArray[1]={[DomDevice]='Off'}
else
--print('Setting MPD volume to '..volume) -- DEBUG:
os.execute('mpc volume '..volume)
-- Conveniently make sure mpd plays after setting volume
--print('Forcing MPD to play') -- DEBUG:
os.execute('mpc play')
end
elseif devicechanged[DomDevice] == 'Off' then
--print(DomDevice..' should be set to: Off') -- DEBUG:
os.execute('mpc pause')
end
end
return commandArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment