Page 1 of 1

Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 14:40
by athoopen
All,

I could do with some help. I created a script that softly switches off a light by gradually reducing the brightniss:

Code: Select all

domoticz.devices(2002).setColorBrightness(255,255,255,100).afterSec(1)
domoticz.devices(2002).setColorBrightness(255,255,255,99).afterSec(2)
domoticz.devices(2002).setColorBrightness(255,255,255,98).afterSec(3)
....
domoticz.devices(2002).setColorBrightness(255,255,255,2).afterSec(98)
domoticz.devices(2002).setColorBrightness(255,255,255,1).afterSec(99)
domoticz.devices(2002).switchOff().afterSec(100)
But honestly ... isn't there a more proper/nicer/better/easier way to do this?

All help appreciated,

Re: Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 15:54
by Kedi
It depends on the kind of device(s) and type of devices you have.
On some Zigbee device there is a transition setting and/or brightness_move_onoff setting.

And of course you could the "domoticz.devices(2002).setColorBrightness(255,255,255,100-x).afterSec(x)" in a loop

Re: Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 16:21
by athoopen
Thanks @Kedi. The lights are Wi-Fi lights from the Action. Have to do it all "manually" (to my knowledge)

I did think about looping, but I couldn't find any instructions/examples on how to do looping in dzvents.
Could you give me an example, or point me to examples/instructions/documentation?

TIA!

Re: Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 17:14
by Kedi
Try Google with "lua loop" and you get a lot of hits with examples you could use in dzVents.

Re: Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 17:28
by mvveelen
Not tested (I have Hue lights and they have a setting for it), but something like this?

Code: Select all

local device = domoticz.devices(2002)

for brightness = 100, 1, -1 do
    device.setColorBrightness(255, 255, 255, brightness).afterSec(100 - brightness)
end


Re: Softly switching off a light (newbie Q)

Posted: Saturday 10 June 2023 20:34
by athoopen
Thanks, only looked at the docs of dzvents, not lua. Example was a great starting point, got it all working now!

Re: Softly switching off a light (newbie Q)

Posted: Sunday 18 June 2023 16:47
by athoopen
I still could do with some help since I try to create a global function to softly switch off a light. The trouble I have is that it returns an error and I have no clue what I am doing wrong.

This is the global function I created:

Code: Select all

return {
	-- global persistent data
	data = {
		myGlobalVar = { initial = 12 }
	},

	-- global helper functions
	helpers = {
		myHelperFunction = function(domoticz)
			-- code
		end,
		
		LightSoftOn = function (domoticz, idx)
		  -- os.execute("/usr/local/bin/telegramsend Execute LightSoftOn for " .. IDX)
		  for brightness = 0, 100, 1 do
            domoticz.devices(idx).setColorBrightness(255, 255, 255, brightness).afterSec(brightness)
          end
          return true    
        end,
        
		LightSoftOff = function (domoticz, idx)
		  -- os.execute("/usr/local/bin/telegramsend Execute LightSoftOff for " .. idx)
		  for brightness = 100, 0, -1 do
            domoticz.devices(idx).setColorBrightness(255, 255, 255, brightness).afterSec(100 - brightness)
          end
          domoticz.devices(idx).switchOff().afterSec(idx)
          return true    
        end,

...     
and here how I call it:

Code: Select all

return {
	on = {
		timer = {
			'every 2 minutes',	
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'TEST',
	},
    execute = function(domoticz, timer)
        -- local Lamp = domoticz.devices(2002)
        
        LightSoftOff (domoticz, 2002)

        end
}
Which results in the error:

Code: Select all

2023-06-18 16:46:00.500 Error: dzVents: Error: (3.1.8) TEST: An error occurred when calling event handler XYZtest
2023-06-18 16:46:00.500 Error: dzVents: Error: (3.1.8) TEST: ...i/domoticz/scripts/dzVents/generated_scripts/XYZtest.lua:14: attempt to call a nil value (global 'LightSoftOff') 
What am I doing wrong?? All help is appreciated!

Re: Softly switching off a light (newbie Q)

Posted: Monday 19 June 2023 9:31
by Kedi
What I do in such cases is that I put this line as the first line in the script.

Code: Select all

local dg = require ("global_data")
After that you can access all data and functions with something like:

Code: Select all

dg.data.xxxx  or
dg.helpers.yyyy
I do this so that in the source it will be visible where the data or function comes from.

Re: Softly switching off a light (newbie Q) [SOLVED]

Posted: Monday 19 June 2023 10:17
by athoopen
@Kedi: require did the trick!! Thanks again.