Basic script. Where am I going wrong

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

Moderator: leecollings

Post Reply
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Basic script. Where am I going wrong

Post by digital »

I am very new to scripting and learning as I go. What I am trying to achieve.......

When "Dusk Dawn Sensor" is On and "Hall Lamp Wall Switch" is Off switch "Hall Lamp Socket" On.
The script runs and shows no errors but the Hall Lamp Socket does not switch on and is not triggered in the script.
Where am I going wrong.

Code: Select all

return {

	active = true,

	on = {
		devices = {
            'Hall Lamp Wall Switch', 
            'Dusk Dawn Sensor', 
            'Hall Lamp Socket'
            
		},

	},

	execute = function(domoticz, device)
	    
       if ((device.name == 'Dusk Dawn Sensor' and device.state == 'On')
       and (device.name == 'Hall Lamp Wall Switch' and device.state == 'Off')) then
           domoticz.devices('Hall Lamp Socket').switchOn()

           end
      
	end
}
Last edited by dannybloe on Wednesday 03 January 2018 11:37, edited 1 time in total.
Reason: turned it into a code block
mosjonathan
Posts: 40
Joined: Friday 24 February 2017 21:20
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Basic script. Where am I going wrong

Post by mosjonathan »

i think it should be something like this:

Code: Select all

return {

active = true,

on = {
	devices = {
			'Test', 
			'Dusk Dawn Sensor', 
			'Hall Lamp Socket'

			},

	},

execute = function(domoticz, device)

if domoticz.devices('Dusk Dawn Sensor') == 'On' and domoticz.devices('Hall Lamp Wall Switch') == 'Off' Then
	domoticz.devices('Hall Lamp Socket').switchOn()

end

end
}
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Basic script. Where am I going wrong

Post by emme »

the error is in the 2 conditions:
the table device (initialized in the statement function(domoticz, device)) contains the ONLY one device that triggered the script.

based on that, you cannot have 2 device.name different eachother, the condition is always FALSE

to do what you need the script should be:

Code: Select all

return {
active = true,
   on = {
      devices = {
         'Dusk Dawn Sensor', 
      },
   },
execute = function(domoticz, device)
   if ((device.state == 'On')  then
      domoticz.devices('Hall Lamp Socket').switchOn().checkFirst()
   else
      domoticz.devices('Hall Lamp Socket').switchOff().checkFirst()
   end
end
}
I've added the else statement to switch off the light when the dusk sensor goes off ;)
the .checkFirst() function is used to avoid the condition in the if statement ( ...and domoticz.devices('Hall Lamp Socket').state == 'Off'): it does it for you

ciao
M
The most dangerous phrase in any language is:
"We always done this way"
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Basic script. Where am I going wrong

Post by digital »

Thank you both.
I understand now what you are saying.
Thanks for your time and help
Post Reply

Who is online

Users browsing this forum: andrehj, Bing [Bot] and 1 guest