-- Balanced group dimmer by EScApe 2018 -- Version 0.1 -- -- This virtual dimmer script will control multiple dimmers as a group and dim them with individual relative levels -- Installation: -- Create a virtual Dimmer (dummy) -- Create Domoticz user variable (string) named Cfg_ (eg. 'Cfg_DimmerLivingRoom') and configure it based on the example below -- Adapt the myname variable in the script to the Domoticz name of this virtual dimmer. -- Tip: save the script as script_device_.lua (with underscores instead of spaces) so it will only run if the specified virtual dimmer changes. -- -- Configuration: -- Example configuration (user variable): Lamp1=100,Lamp2=50,Lamp3=75,defaultlevel=50,maxout=1,debug=1 -- In this example Lamp2 will be at half the level compared to Lamp1 (so it Lamp1 is at 50, Lamp2 will be at 25). -- Not every lightsource has the same maximum brightness and this script helps you balance them to your liking. -- Make sure there are no extra spaces in the configuration variable and you are using the exact (case sensitive) names for the actual lights (which obviously must be dimmers). -- -- Besides the lamps you can also see some (optional) settings in the example: -- defaultlevel: the level the virtual dimmer is set to when it is turned on (by button or anything else than the slider). Can be usefull if individual lights are also controlled seperately. -- maxout: with this set to 1 alle lights will be set to maximum brightness (100%) if the virtual dimmer is set to (exactly) 100%. Ignoring the relative brightness settings. -- debug: if this is set to 1 then the script will generate verbose logging to help you check your configuration and monitor script behaviour. myname="Eettafel" commandArray = {} local http = require('socket.http') if devicechanged[myname] then cfgvar = "Cfg_" .. myname lvlvar = "Cfg_" .. myname .. "_Level" if not uservariables[cfgvar] then print("ERROR! Virtual '" .. myname .. "' dimmer is missing configuration variable named: '" .. cfgvar .. "'") return commandArray end defaultlevel = 0 if uservariables[lvlvar] then defaultlevel = tonumber(uservariables[lvlvar]) end debug = false maxout = false lights = {} for key, value in string.gmatch(uservariables[cfgvar], "([^=,]+)=(%d+)") do if key == 'debug' then if value == '1' then print('-- starting with debug mode enabled --') debug = true end elseif key == 'defaultlevel' then if defaultlevel == 0 then defaultlevel = tonumber(value) end elseif key == 'maxout' then if value == '1' then maxout = true end else lights[key] = value end end if debug then print("relative light levels:") for k, v in pairs(lights) do print("> " .. k .. " : " .. v) end if maxout then print("All lights will be set to their individual maximum when slider is set to 100%") end end if devicechanged[myname] == 'Off' then for lampname, lampmax in pairs(lights) do if debug then print("Turn " .. lampname .. " off") end commandArray[lampname]='Off' end elseif devicechanged[myname] == 'On' then if defaultlevel > 0 then if debug then print("setting to default level: " .. defaultlevel) end commandArray[myname] = 'Set Level ' .. defaultlevel else for lampname, lampmax in pairs(lights) do if debug then print("Turn " .. lampname .. " on") end commandArray[lampname]='On' end end else if uservariables[lvlvar] then commandArray['Variable:'..lvlvar] = tostring(otherdevices_svalues[myname]) end dimlevel = tonumber(otherdevices_svalues[myname])/100 if debug then print("setting relative level to: " .. dimlevel) end json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() local acolor = " " if debug then print(otherdevices_idx[myname]) end ab='curl "http://localhost:8080/json.htm?type=devices&rid=' .. otherdevices_idx[myname] .. '"' local jsondata = assert(io.popen(ab)) local jsondevices = jsondata:read('*all') jsondata:close() local jsonCPM = json:decode(jsondevices) local acolor=jsonCPM.result[1].Color if debug then print (acolor) end acolorj=acolor:gsub("}",",}") for k,v in acolorj:gmatch('"(.-)":(.-),') do if debug then print(k,v) end if (k == 'b') then rgbBlueValue = v elseif (k == 'r') then rgbRedValue = v elseif (k == 'g') then rgbGreenValue = v elseif (k == 't') then rgbColorTemp = v end end for lampname, lampmax in pairs(lights) do if maxout and dimlevel == 1 then level = 100 else level = math.floor(dimlevel * lampmax) end if level > 0 then if debug then print("set " .. lampname .. " to " .. level) end commandArray[lampname]='Set Level ' .. level else if debug then print("Turn " .. lampname .. " off") end commandArray[lampname]='Off' end -- http = require('socket.http') json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() reply = http.request('http://localhost:8080/json.htm?type=command¶m=setcolbrightnessvalue&idx=' .. otherdevices_idx[lampname] .. '&color={"m":2,"t":' .. rgbColorTemp .. ',"r":0,"g":0,"b":0,"cw":0,"ww":0}&brightness=' .. level ..'') if debug then print(reply) end -- http://localhost:8080/json.htm?type=command¶m=setcolbrightnessvalue&idx=51&color={%22m%22:2,%22t%22:120,%22r%22:0,%22g%22:0,%22b%22:0,%22cw%22:0,%22ww%22:0} end end end return commandArray