Group Dimmer script

Moderator: leecollings

Post Reply
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Group Dimmer script

Post by EscApe »

This script will let you control a group of dimmers from one virtual dimmer. You can even use different levels per dimmer to deal with brightness differences. Instructions are in the script.

I share this script as is. Feel free to adapt and tinker.

Code: Select all

-- 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_<virtual dimmer name> (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_<lower_case_dimmer_name>.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="Dimmer Beneden"

commandArray = {}

if devicechanged[myname] then
	cfgvar="Cfg_" .. myname
	if not uservariables[cfgvar] then
		print("ERROR! Virtual '" .. myname ..  "' dimmer is missing configuration variable named: '" .. cfgvar .. "'")
		return commandArray
	end
	debug=false
	defaultlevel=0
	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
			defaultlevel = tonumber(value)
		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
		dimlevel = tonumber(otherdevices_svalues[myname])/100
		if debug then print("setting relative level to: " .. dimlevel) 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
		end
	end 
end

return commandArray
Cyroq
Posts: 30
Joined: Monday 10 December 2018 13:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Group Dimmer script

Post by Cyroq »

Thanks for this! I have a few lighting spots that use multiple lightbulbs so I'm very curious about this script. I followed the instructions, but when I toggle the virtual switch to ON the script seem to run twice, turning it off immediately. This is the log:

Code: Select all

2019-01-23 16:09:03.134 Status: LUA: -- starting with debug mode enabled --
2019-01-23 16:09:03.134 Status: LUA: relative light levels:
2019-01-23 16:09:03.134 Status: LUA: > Woonkamer2 : 100
2019-01-23 16:09:03.134 Status: LUA: > Woonkamer1 : 100
2019-01-23 16:09:03.134 Status: LUA: All lights will be set to their individual maximum when slider is set to 100%
2019-01-23 16:09:03.134 Status: LUA: setting to default level: 50
2019-01-23 16:09:03.135 Status: EventSystem: Script event triggered: script_device_dimmerwoonkamer
2019-01-23 16:09:03.180 Status: LUA: -- starting with debug mode enabled --
2019-01-23 16:09:03.180 Status: LUA: relative light levels:
2019-01-23 16:09:03.180 Status: LUA: > Woonkamer2 : 100
2019-01-23 16:09:03.180 Status: LUA: > Woonkamer1 : 100
2019-01-23 16:09:03.180 Status: LUA: All lights will be set to their individual maximum when slider is set to 100%
2019-01-23 16:09:03.180 Status: LUA: Turn Woonkamer2 off
2019-01-23 16:09:03.180 Status: LUA: Turn Woonkamer1 off
2019-01-23 16:09:03.180 Status: EventSystem: Script event triggered: script_device_dimmerwoonkamer
Any idea on what I might be doing wrong?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Group Dimmer script

Post by EscApe »

I am not at my pc right now, so if still needed I will run some test later. Just a quick check:
You wrote “virtual switch”. It should be a virtual dimmer (slider), but maybe that is what you meant?

Does it behave correctly if you use the slider instead of the toggle button to turn the lights on?
Cyroq
Posts: 30
Joined: Monday 10 December 2018 13:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Group Dimmer script

Post by Cyroq »

Sorry, I meant dimmer. The weird thing is I can't use the slider, it's 'stuck'. It behaves like a dimmer that can't get feedback from the device, it seems. So maybe that's why the script is switching it off directly after turning it on?

I just found out that it does turn off the lights when I first turn them on manually. They just won't stay on.
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Group Dimmer script

Post by EscApe »

That’s strange. The script is not controlling it’s own slider, just the ones in the ‘group’. Please make sure they use different names. Another explanation could be that the dummy dimmer you use as “master” is not created correctly or is being controlled by another script.

Does de slider work when you disable the script?
Cyroq
Posts: 30
Joined: Monday 10 December 2018 13:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Group Dimmer script

Post by Cyroq »

Hmm, with the script disabled it doesn't work either. A new dummy dimmer with a random name doesn't slide as well. Are they supposed to slide by default? I only worked with dummy switches before.

EDIT:
I just realized that there's a difference between creating a dimmer dummy from "< Manual Light/Switch" and "Create Virtual Sensors" in the hardware tab. The first one results in Type "Lighting 1" while the other is Type "Light/Switch". However, the Switch Type in Switches > Edit shows "Dimmer" on both dummies, that's why I didn't notice it before.

In other words: I was doing it completely wrong and it's working now! Thanks so much for your work :mrgreen:
sash
Posts: 5
Joined: Thursday 28 February 2019 16:58
Target OS: Linux
Domoticz version:
Contact:

Re: Group Dimmer script

Post by sash »

Hi there sorry i am new to domoticz and I am a little confused by the directions
maybe someone can clear it up for me.

for example the from lua script "Dimmer Beneden" is the name of the virtual switch


1- Create a virtual Dimmer (dummy) :
Hardware -> Dummy -> create virtual sensor -> switch -> go to devices change from on/off to dimmer

2 - Create Domoticz user variable (string) named Cfg_<virtual dimmer name> :
based on the name above --- > Cfg_Dimmer_Beneden ( with or with out underscore with or with out case sensitivity ???)
then go to user variables ( found in more options)
--

Code: Select all

Variable name : Cfg_Dimmer_Beneden
Variable type : string
Variable value: Lamp1=100,Lamp2=50,Lamp3=75,defaultlevel=50,maxout=1,debug=1
--
is Lamp1,Lamp2 and Lamp3 the names of the light switch or device name for the light ??????

3- save the script as -- script_device_<lower_case_dimmer_name>.lua
so if we used the above example name "Dimmer Beneden" then it should become;

Code: Select all

script_device_dimmer_beneden.lua 


where does one save the script ????? .. /domoticz/scripts/lua ???

as i keep trying but I still can not get it to work for me and I am not sure what i am doing wrong
any help would be welcome thank you
sash
Posts: 5
Joined: Thursday 28 February 2019 16:58
Target OS: Linux
Domoticz version:
Contact:

Re: Group Dimmer script

Post by sash »

okay I got it figured out from the example "Dimmer Beneden"
in the end I renamed it all to small case "dimmer_beneden" as the virtual device name.
and renamed accordingly the rest and now it works fine .. I tried with out the underscore in the virtual device name it would not work either .
nitpicker
Posts: 69
Joined: Tuesday 29 July 2014 10:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Re: Group Dimmer script

Post by nitpicker »

Too bad, not working anymore in version 2023.1

Will check if I can fix it.
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Group Dimmer script

Post by EscApe »

nitpicker wrote: Saturday 18 February 2023 12:17 Too bad, not working anymore in version 2023.1

Will check if I can fix it.
Great to see that the script is actually put to good use :)
I am not aware of any changes to Domoticz that would interfere with this script. Could there be another reason for it to stop working?
I am no longer using this script myself, but a much more complicated approach that was not affected by version 2023.1

Hope you can fix it! Please share if you do.
bert
Posts: 17
Joined: Monday 27 October 2014 23:57
Target OS: Linux
Domoticz version:
Contact:

Re: Group Dimmer script

Post by bert »

Here not working anymore either after an update of Domoticz to 2023.1. The script keeps looping and triggering the virtual input dimmer device and setting it to its default value continuously.
I guess 'silent()' is required on line 68 or something; but what is the LUA equivalent for that?
bert
Posts: 17
Joined: Monday 27 October 2014 23:57
Target OS: Linux
Domoticz version:
Contact:

Re: Group Dimmer script

Post by bert »

turned the script into Dzvent and eliminated the need for a uservariable in the process - for anyone also experiencing 'new behavior' from the LUA script.

Code: Select all

-- user variable line from LUA version translated into a table and some local variables below it:
-- uitbouw dimmer=100,Ikea kast=100,Eettafel=60,Aanrecht spots dimmer=100,Schemerlamp woonkamer=60,defaultlevel=50,maxout=1

local VIRTUALDIMMER = 'Woonkamer Lichten'

local DIMMERTABLE = { --  [   'Dimmer'   ]  = individual dimmer level
                            ['uitbouw dimmer']  = 100,
                            ['Ikea kast']  = 100,
                            ['Eettafel']  = 60,
                            ['Aanrecht spots dimmer']  = 100,
                            ['Schemerlamp woonkamer']  = 60
                        }

local DEFAULTLEVEL = 50
local MAXOUT = true

return {
    active = true,
	logging = {
		level = domoticz.LOG_INFO, -- Select one of LOG_DEBUG, LOG_INFO, LOG_ERROR, LOG_FORCE to override system log level
		marker = VIRTUALDIMMER
	},
	on = {
		devices = {
			VIRTUALDIMMER   -- define device to trigger this script
		}
	},


	execute = function(domoticz, device)
	    if (device.state == 'On' and device.lastLevel == device.level ) then -- if the level did not change, the on-button was pressed

              dimlevel = tonumber(DEFAULTLEVEL)/100
           domoticz.log('Setting to default level: '.. DEFAULTLEVEL , domoticz.LOG_INFO)
           for light, dimValue in pairs (DIMMERTABLE) do
       	       	if ( MAXOUT and dimlevel == 1 ) then
				    level = 100
			    else 
				    level =  math.floor(dimlevel * dimValue)
			    end
			    domoticz.log('36: changing ' .. light ..' to relative level: ' .. level, domoticz.LOG_INFO)
                domoticz.devices(light).dimTo(level)
           end
       	        device.dimTo(DEFAULTLEVEL).silent()
      


        elseif (device.state == 'On' and device.lastLevel ~= device.level ) then -- the dimmer slider was moved; level changed while on
           domoticz.log('Dimmer slider moved from '..device.lastLevel ..' to: ' .. device.level ..' while already on ' , domoticz.LOG_INFO)
           dimlevel = tonumber(device.level)/100
           for light, dimValue in pairs (DIMMERTABLE) do
       	       	if ( MAXOUT and dimlevel == 1 ) then
				    level = 100
			    else 
				    level =  math.floor(dimlevel * dimValue)
			    end
			    domoticz.log('52: changing ' .. light ..' to ' .. level, domoticz.LOG_INFO)
                domoticz.devices(light).dimTo(level)
           end

        elseif (device.state == 'Off') then
            domoticz.log('timer off; lights to off' , domoticz.LOG_INFO)
            for light, dimValue in pairs (DIMMERTABLE) do
                domoticz.devices(light).switchOff().checkFirst()
            end
        end
end
}
User avatar
Denny64
Posts: 53
Joined: Friday 03 February 2017 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Italy
Contact:

Re: Group Dimmer script

Post by Denny64 »

Great job. It works fine for roller shutter also.
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest