Can this script made smarter / better

Easy to use, 100% Lua-based event scripting framework.
Post Reply
Boredcat
Posts: 38
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Can this script made smarter / better

Post by Boredcat » 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 ?

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
}
RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

User avatar
waaren
Posts: 945
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Can this script made smarter / better

Post by waaren » Sunday 15 April 2018 21:15

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
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

Boredcat
Posts: 38
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Can this script made smarter / better

Post by Boredcat » Monday 16 April 2018 16:34

@Waaren,

Thanks for you input

Had to lookup the Lua Operators for % (didn't know that one).
RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

Ries
Posts: 33
Joined: Tuesday 01 December 2015 9:47
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Can this script made smarter / better

Post by Ries » 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
Version 3.9639
Platform: Jessie lite
Hardware: Raspberry pi 3B; RFXCOM; Weather Underground; Dummy; OpenZwave USB; Fritzbox Callmonitor via LAN interface; System Alive Checker (Ping); Philips Hue Bridge; Atag One Thermostat

Boredcat
Posts: 38
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Can this script made smarter / better

Post by Boredcat » Friday 22 June 2018 8:51

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.
RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests