Softly switching off a light (newbie Q)

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
athoopen
Posts: 40
Joined: Wednesday 14 December 2022 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

Softly switching off a light (newbie Q)

Post 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,
Cheers,
Arjan
Kedi
Posts: 575
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Softly switching off a light (newbie Q)

Post 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
Logic will get you from A to B. Imagination will take you everywhere.
athoopen
Posts: 40
Joined: Wednesday 14 December 2022 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: Softly switching off a light (newbie Q)

Post 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!
Cheers,
Arjan
Kedi
Posts: 575
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Softly switching off a light (newbie Q)

Post by Kedi »

Try Google with "lua loop" and you get a lot of hits with examples you could use in dzVents.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
mvveelen
Posts: 688
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

Re: Softly switching off a light (newbie Q)

Post 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

RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
athoopen
Posts: 40
Joined: Wednesday 14 December 2022 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: Softly switching off a light (newbie Q)

Post by athoopen »

Thanks, only looked at the docs of dzvents, not lua. Example was a great starting point, got it all working now!
Cheers,
Arjan
athoopen
Posts: 40
Joined: Wednesday 14 December 2022 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: Softly switching off a light (newbie Q)

Post 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!
Cheers,
Arjan
Kedi
Posts: 575
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Softly switching off a light (newbie Q)

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
athoopen
Posts: 40
Joined: Wednesday 14 December 2022 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

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

Post by athoopen »

@Kedi: require did the trick!! Thanks again.
Cheers,
Arjan
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest