Page 1 of 1

Basic script. Where am I going wrong

Posted: Wednesday 03 January 2018 10:35
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
}

Re: Basic script. Where am I going wrong

Posted: Wednesday 03 January 2018 10:44
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
}

Re: Basic script. Where am I going wrong

Posted: Wednesday 03 January 2018 10:47
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

Re: Basic script. Where am I going wrong

Posted: Wednesday 03 January 2018 10:52
by digital
Thank you both.
I understand now what you are saying.
Thanks for your time and help