Page 1 of 1

Can this script made smarter / better

Posted: Sunday 15 April 2018 16:03
by Boredcat
If have created the following script, which flash 2 yeelights and the Xiaomo gateway. Also it plays a sound using the Xiaomi gateway.

It is triggered with the TEST SW (a dummy switch which is activated by several PIR's).

The script works, but I think that the scripts can be made smarter of shorter.

Any tips ?

Code: Select all

return {
	on = {
		devices = {
			'TEST SW'
		}
	},
	execute = function(domoticz, device)
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		domoticz.log('Executing alarm sound and light', domoticz.LOG_INFO)
		
		local light = domoticz.devices('XIAOMI LIVING ROOM')
		local alsw  = domoticz.devices('TEST SW')
		local light1 = domoticz.devices('Lamp bank links')
		local light2 = domoticz.devices('YeeLight LED Bank')
		local alarmsound = domoticz.devices('Xiaomi Doorbell')
		local alarmlevel = domoticz.devices('GW Xiaomi Volume')
		
		if device.state == 'On' then
		    
		    -- Set alarm sound and level 100%
		    
		    alarmlevel.dimTo(100)
		    -- alarmsound.switchSelector(50)
		    
		    -- End set sound level 
		    
		    -- Start alarm lights
		    
		    
		    light.dimTo(100)
		    light.setRGB(255,0,0).afterSec(10)
		    light.setRGB(0,0,255).afterSec(9)
		    light.setRGB(255,0,0).afterSec(8)
		    light.setRGB(0,0,255).afterSec(7)
		    light.setRGB(255,0,0).afterSec(6)
		    light.setRGB(0,0,255).afterSec(5)
		    light.setRGB(255,0,0).afterSec(4)
		    light.setRGB(255,0,0).afterSec(3)
		    light.setRGB(0,0,255).afterSec(2)
		    light.setRGB(255,0,0).afterSec(1)
		    light.setRGB(0,0,255)
		    
		    light1.dimTo(100)
		    light1.setRGB(255,0,0).afterSec(10)
		    light1.setRGB(0,0,255).afterSec(9)
		    light1.setRGB(255,0,0).afterSec(8)
		    light1.setRGB(0,0,255).afterSec(7)
		    light1.setRGB(255,0,0).afterSec(6)
		    light1.setRGB(0,0,255).afterSec(5)
		    light1.setRGB(255,0,0).afterSec(4)
		    light1.setRGB(255,0,0).afterSec(3)
		    light1.setRGB(0,0,255).afterSec(2)
		    light1.setRGB(255,0,0).afterSec(1)
		    light1.setRGB(0,0,255)
		    
		    
		    light2.dimTo(100)
		    light2.setRGB(255,0,0).afterSec(10)
		    light2.setRGB(0,0,255).afterSec(9)
		    light2.setRGB(255,0,0).afterSec(8)
		    light2.setRGB(0,0,255).afterSec(7)
		    light2.setRGB(255,0,0).afterSec(6)
		    light2.setRGB(0,0,255).afterSec(5)
		    light2.setRGB(255,0,0).afterSec(4)
		    light2.setRGB(255,0,0).afterSec(3)
		    light2.setRGB(0,0,255).afterSec(2)
		    light2.setRGB(255,0,0).afterSec(1)
		    light2.setRGB(0,0,255)
		    
		  
		    
		    
		    -- Switch off alarm sound and light after 11 seconds. Also switch off alarm switch (alsw). AlSW will be activated againif motion is detected
		    
		    
		    light.switchOff().afterSec(11)
		    light1.switchOff().afterSec(11)
		    light2.switchOff().afterSec(11)
		   
		    alsw.switchOff().afterSec(11)
		    alarmsound.switchSelector(0).afterSec(11)
		    
		    
        end

		    
	end
}

Re: Can this script made smarter / better

Posted: Sunday 15 April 2018 21:15
by waaren
Boredcat wrote: Sunday 15 April 2018 16:03 If have created the following script, which flash 2 yeelights and the Xiaomo gateway. Also it plays a sound using the Xiaomi gateway.
It is triggered with the TEST SW (a dummy switch which is activated by several PIR's).
The script works, but I think that the scripts can be made smarter of shorter.

Any tips ?
you can make it shorter (see attached).

Code: Select all

return {
	on = {
		devices = {
			'TEST SW'
		}
	},
	execute = function(domoticz, device)
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		domoticz.log('Executing alarm sound and light', domoticz.LOG_INFO)
		
		local light = domoticz.devices('XIAOMI LIVING ROOM')
		local alsw  = domoticz.devices('TEST SW')
		local light1 = domoticz.devices('Lamp bank links')
		local light2 = domoticz.devices('YeeLight LED Bank')
		local alarmsound = domoticz.devices('Xiaomi Doorbell')
		local alarmlevel = domoticz.devices('GW Xiaomi Volume')
		
		if device.state == 'On' then
		    
		    -- Set alarm sound and level 100%
		    
		    alarmlevel.dimTo(100)
		    -- alarmsound.switchSelector(50)
		    
		    -- End set sound level 
		    
		    -- Start alarm lights
		    
		    
		    light.dimTo(100)
		    light1.dimTo(100)
		    light2.dimTo(100)
			for i = 10,1,-1 do
				if i % 2 == 0 then
					light.setRGB(255,0,0).afterSec(i)
					light1.setRGB(255,0,0).afterSec(i)
					light2.setRGB(255,0,0).afterSec(i)
				else
					light1.setRGB(0,0,255).afterSec(i)
					light2.setRGB(0,0,255).afterSec(i)
					light3.setRGB(0,0,255).afterSec(i)
				end
			end
					    
		    light0.setRGB(0,0,255)
		    light1.setRGB(0,0,255)
		    light2.setRGB(0,0,255)
		    
		    -- Switch off alarm sound and light after 11 seconds. Also switch off alarm switch (alsw). AlSW will be activated againif motion is detected
		    light.switchOff().afterSec(11)
		    light1.switchOff().afterSec(11)
		    light2.switchOff().afterSec(11)
		   
		    alsw.switchOff().afterSec(11)
		    alarmsound.switchSelector(0).afterSec(11)
		    
		    
        end

		    
	end
}

Re: Can this script made smarter / better

Posted: Monday 16 April 2018 16:34
by Boredcat
@Waaren,

Thanks for you input

Had to lookup the Lua Operators for % (didn't know that one).

Re: Can this script made smarter / better

Posted: Wednesday 13 June 2018 19:52
by Ries
A lot of redunancy. all three lights undergo the same actions by the looks of it.
You could array the lights, instead of light, light1, light2 you could say light[0], light [1] and light[2], then create a loop like

i =0
repeat
— your code light[ i].switchOn()
i=i+1
Until i=2

Re: Can this script made smarter / better

Posted: Friday 22 June 2018 8:51
by Boredcat
Ries wrote: Wednesday 13 June 2018 19:52 A lot of redunancy. all three lights undergo the same actions by the looks of it.
You could array the lights, instead of light, light1, light2 you could say light[0], light [1] and light[2], then create a loop like

i =0
repeat
— your code light[ i].switchOn()
i=i+1
Until i=2
Making my script not smarter but more efficient. Thnx for the idea, never thought of the idea of creating an array. Will use this in more scripts.