Light script - Event detected but no effect

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

Moderator: leecollings

Post Reply
TomBoeg
Posts: 3
Joined: Monday 15 November 2021 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Light script - Event detected but no effect

Post by TomBoeg »

Hello everyone,

I'm currently having some trouble with the dzvenst script i've written for my lights.
I'm using one selector switch to say if I want it all automated (Auto) or manually On or Off (5 levels in total).
Then I have two dummy light switch for the schedule (outdoor lights and indoor lights) to indicate if the lights should be switched On or Off depending on the day of the week and so on.

All modes are working except for the Auto : the lights are not switched On or Off according to the schedule (the dummy switch used for the schedule are going On and Off as planned).

Here is the code :

Code: Select all

return {
	active = true,

	on = {
		devices = { 'Selecteur_Ambiance', 
		            'Planning_Eclairage_Indoor',
		            'Planning_Eclairage_Outdoor'
		            }
        },
	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Script Ambiance_lumineuse_v2"
    },

	execute = function(domoticz, Selecteur_Ambiance, Planning_Eclairage_Indoor, Planning_Eclairage_Outdoor)

		if (Selecteur_Ambiance.levelName == 'Off_All') then -- Full Off
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
		    domoticz.devices('Eclairage_Exterieur').switchOff()
		    domoticz.devices('Level_Lampe_Buffet').switchOff()
			domoticz.log('The lights are switched Off forever')
		end	
		if (Selecteur_Ambiance.levelName == 'On_All') then -- Full On
		    domoticz.devices('Lampe_Salle_a_Manger').switchOn()
		    domoticz.devices('Lampe_Entrée').switchOn()
		    domoticz.devices('Eclairage_Exterieur').switchOn()
		    domoticz.devices('Level_Lampe_Buffet').switchOn()
			domoticz.log('The lights are switched On forever')
		end	
		if (Selecteur_Ambiance.levelName == 'On_Once') then -- Set everything On once and then back to Auto at 23:59
		    domoticz.devices('Lampe_Salle_a_Manger').switchOn()
		    domoticz.devices('Lampe_Entrée').switchOn()
		    domoticz.devices('Eclairage_Exterieur').switchOn()
		    Selecteur_Ambiance.switchSelector('Auto').at('23:59')
			domoticz.log('The lights are switched On and selector set on Auto')
		end	
		if (Selecteur_Ambiance.levelName == 'Off_Once') then -- Set everything Off once and then back to Auto at 23:59
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
		    domoticz.devices('Eclairage_Exterieur').switchOff()
		    Selecteur_Ambiance.switchSelector('Auto').at('23:59') 
			domoticz.log('The lights are switched Off and selector set on Auto')
		end	
		if (Selecteur_Ambiance.levelName == 'Auto' and Planning_Eclairage_Indoor.state == 'On') then -- Auto as per Planning_Eclairage_Indoor On
		    domoticz.devices('Lampe_Salle_a_Manger').switchOn()
		    domoticz.devices('Lampe_Entrée').switchOn()
			domoticz.log('The lights are switched On (Auto Inside)')
		end	
		if (Selecteur_Ambiance.levelName == 'Auto' and Planning_Eclairage_Indoor.state == 'Off') then -- Auto as per Planning_Eclairage_Indoor Off
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
			domoticz.log('The lights are switched Off (Auto Inside)')
		end	
		if (Selecteur_Ambiance.levelName == 'Auto' and Planning_Eclairage_Outdoor.state == 'On') then -- Auto as per Planning_Eclairage_Outdoor On
		    domoticz.devices('Eclairage_Exterieur').switchOn()
			domoticz.log('The lights are switched On (Auto Outside)')
		end	
		if (Selecteur_Ambiance.levelName == 'Auto' and Planning_Eclairage_Outdoor.state == 'Off') then -- Auto as per Planning_Eclairage_Outdoor Off
		    domoticz.devices('Eclairage_Exterieur').switchOff()
			domoticz.log('The lights are switched Off (Auto Outside)')
		end
	end
}
And here is for example the log when the outdoor lights should be switched Off :

Code: Select all

2021-12-09 21:00:00.585 Status: Starting automatic database backup procedure...
2021-12-09 21:00:01.585 Status: Schedule item started! Name: Planning_Eclairage_Outdoor, Type: On Time, DevID: 268, Time: 2021-12-09 21:00:01
2021-12-09 21:00:02.874 Planning_Eclairage_Outdoor: Light/Switch (Planning_Eclairage_Outdoor)
2021-12-09 21:00:02.862 Status: Ending automatic database backup procedure...
2021-12-09 21:00:03.053 Status: dzVents: Info: Handling events for: "Planning_Eclairage_Outdoor", value: "Off"
2021-12-09 21:00:03.053 Status: dzVents: Info: Script Ambiance_lumineuse_v2: ------ Start internal script: Ambiance_lumineuse_v2: Device: "Planning_Eclairage_Outdoor (Planning_Eclairage_Outdoor)", Index: 268
2021-12-09 21:00:03.054 Status: dzVents: Info: Script Ambiance_lumineuse_v2: ------ Finished Ambiance_lumineuse_v2
The event is detected but nothing happens...
I've tried as well a version with if/elseif but with the same results.

If anyone has an idea...

Thanks in advance for wour help
Thomas
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Light script - Event detected but no effect

Post by boum »

Ugh, you've got some misunderstandings about the basics in dzvents.
Please read https://www.domoticz.com/wiki/DzVents:_ ... .80.A6_end

Especially, "The function has three parameters"

Code: Select all

execute = function(domoticz, Selecteur_Ambiance, Planning_Eclairage_Indoor, Planning_Eclairage_Outdoor)
should be

Code: Select all

execute = function(domoticz, item)
The third parameter can be ignored. Since you trigger on devices, item will be the device that triggered the script. You can test it with

Code: Select all

if item.name == "Selecteur_Ambiance" then
To access other devices, you should query the domoticz object (first parameter)

Code: Select all

local Planning_Eclairage_Indoor = domoticz.devices("Planning_Eclairage_Indoor")
Hope this helps.
TomBoeg
Posts: 3
Joined: Monday 15 November 2021 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Light script - Event detected but no effect

Post by TomBoeg »

Hi boum,

Thank you for your message.

I've updated the code but I think that I'm still don't understanding something.
Here is what I came up with (short version for one of the combination only) but it's not working :

Code: Select all

return {
	active = true,

	on = {
		devices = { 'Selecteur_Ambiance', 
		            'Planning_Eclairage_Indoor',
		            'Planning_Eclairage_Outdoor'
		            }
        },
	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Script Ambiance_lumineuse_v3"
    },

	execute = function(domoticz, item)
    
    local Planning_Eclairage_Indoor = domoticz.devices("Planning_Eclairage_Indoor")
    local Planning_Eclairage_Outdoor = domoticz.devices("Planning_Eclairage_Outdoor")

		if (item.name == "Selecteur_Ambiance" and item.levelName == 'Auto') and 
		    (item.name == "Planning_Eclairage_Indoor" and item.state == 'Off') then -- Auto as per Planning_Eclairage_Indoor Off		    
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
			domoticz.log('The lights are switched Off (Auto Inside)')
		end	
	end
}
Could you please help ?
Thanks in advance
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Light script - Event detected but no effect

Post by boum »

The problem is now boolean algebra. In your "if test" you check that item.name is both equal to Selecteur and Planning.
item is the triggered object. With each invocation of the script it can only be one of the triggers. "item" is what changed.

You would want:

Code: Select all

	local Selecteur_Ambiance = domoticz.devices("Selecteur_Ambiance")
	if (Selecteur_Ambiance.levelName == 'Auto') and 
	    (item.name == "Planning_Eclairage_Indoor" and item.state == 'Off') then -- Auto as per Planning_Eclairage_Indoor Off		    
	    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
	    domoticz.devices('Lampe_Entrée').switchOff()
		domoticz.log('The lights are switched Off (Auto Inside)')
	end
Because you want to switch those indoor lights off when the Planning device is changed (and triggers the script), not when the selector is changed.
And this only when the selector state is on Auto.

In your case, your script might look like (using your first message as base):

Code: Select all

return {
	active = true,

	on = {
		devices = { 'Selecteur_Ambiance', 
		            'Planning_Eclairage_Indoor',
		            'Planning_Eclairage_Outdoor'
		            }
        },
	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Script Ambiance_lumineuse_v2"
    },

	execute = function(domoticz, item)
		-- if the trigger is not the selector, only act if on Auto
		if item.name ~= 'Selecteur_Ambiance' then
		    	local selector = domoticz.devices("Selecteur_Ambiance")
			if selector.levelName == 'Auto' then
				-- table with item.name as index and an array of device names as values
				local commands = {
					Planning_Eclairage_Indoor = { 'Lampe_Salle_a_Manger', 'Lampe_Entrée', },
					Planning_Eclairage_Outdoor = { 'Eclairage_Exterieur', },
				}
				domoticz.log('The lights are switched ' .. item.state .. ' (Auto ' .. item.name .. ')')
				local names = commands[ item.name ]
				for i,name in ipairs(names) do
					local device = domoticz.devices(name)
					if item.active then
						device.switchOn().checkFirst()
					else
						device.switchOff().checkFirst()
					end						
				end
			end
			return -- early exit 
		end
		
		-- here we manage the change on the selector
		if (item.levelName == 'Off_All') then -- Full Off
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
		    domoticz.devices('Eclairage_Exterieur').switchOff()
		    domoticz.devices('Level_Lampe_Buffet').switchOff()
			domoticz.log('The lights are switched Off forever')
		elseif (item.levelName == 'On_All') then -- Full On
		    domoticz.devices('Lampe_Salle_a_Manger').switchOn()
		    domoticz.devices('Lampe_Entrée').switchOn()
		    domoticz.devices('Eclairage_Exterieur').switchOn()
		    domoticz.devices('Level_Lampe_Buffet').switchOn()
			domoticz.log('The lights are switched On forever')
		elseif (item.levelName == 'On_Once') then -- Set everything On once and then back to Auto at 23:59
		    domoticz.devices('Lampe_Salle_a_Manger').switchOn()
		    domoticz.devices('Lampe_Entrée').switchOn()
		    domoticz.devices('Eclairage_Exterieur').switchOn()
		    item.switchSelector('Auto').at('23:59')
			domoticz.log('The lights are switched On and selector set on Auto')
		elseif (item.levelName == 'Off_Once') then -- Set everything Off once and then back to Auto at 23:59
		    domoticz.devices('Lampe_Salle_a_Manger').switchOff()
		    domoticz.devices('Lampe_Entrée').switchOff()
		    domoticz.devices('Eclairage_Exterieur').switchOff()
		    item.switchSelector('Auto').at('23:59') 
			domoticz.log('The lights are switched Off and selector set on Auto')
		end	
	end
}
Edit: fix script
Last edited by boum on Monday 13 December 2021 22:21, edited 1 time in total.
TomBoeg
Posts: 3
Joined: Monday 15 November 2021 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Light script - Event detected but no effect

Post by TomBoeg »

Hi boum,

It's working well, thanks a lot for your explanation and the updated script !
Once you've explained the boolean algebra issue it was clear that it could not work :)

It will help me a lot for my future scripts

Thanks again

PS : there was a small typo mistake at line 29, the "s" was missing : local device = domoticz.devices(name) ;)
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Light script - Event detected but no effect

Post by boum »

Thanks Tom, I edited the code.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest