Skip to content

Examples to use LUA script for various sensors

Smanar edited this page Oct 22, 2020 · 12 revisions

You have two way to make LUA scripts.

  • make a file called "script_device_That_You_Want.lua in "/home/pi/domoticz/scripts/lua"
  • go to "Setting" / "More options" / "Event" / "LUA"

I m using scene, because you can easily set light value, color or temporisation.

-- Some LUA exemples

time = os.date("*t")

commandArray = {}

-- Motion detector
if (devicechanged['deCONZ - Motion Sensor'] == 'On') then
    print("Motion detector detection")
    
    if (otherdevices['working day']== 'On') then
		commandArray['Scene:Morning light']='On'
    else
		if ((time.hour >= 23) or (time.hour < 7)) then
			commandArray['Scene:night light']='On'
		else
			commandArray['Scene:normal light']='On'
		end
		if ((time.hour >= 12) and (time.hour < 20)) then
			commandArray['Scene:Light 2']='On'
		end
	end
end

-- Door openend
if (devicechanged["deCONZ - entry door"] == 'Open') then
    print("Door opened")
    commandArray['Scene:Entrance Light']='On'
end

-- Room sensor
if devicechanged['deCONZ - Vibration Sensor'] == 'Vibrate' then
    if tonumber(otherdevices_svalues['deCONZ - Light Room']) > 50 then
	print("Dim room light")
	commandArray['Scene:dim room'] = 'On'
    end
end

return commandArray

deCONZ - Motion Sensor : Motion detector name.
working day : custom device, enabled on working day.
Scene:Morning light : Scene with light on.
Scene:night light : scene with lower light.
Scene:normal light : scene witn normmal light.
Scene:Light 2 : Scene with sheduled bulbs.
dim room : Scene with decreasing light level in room, et set it to off after 10mn.

A scene examples with a 5mn timer. scene1

Remark on the presence detector.
The Xioami motion detector in reality isn't a motion detector, but more a presence detector. So It's a better idea to check when it trigger "off" than when it trigger "on".

First increase the time duration with something like :
curl -H 'Content-Type: application/json' -X PUT -d '{"duration": 300}' http://192.168.1.1:80/api/63E7882BAE/sensors/5/config
And use this kind of script:

if devicechanged['deCONZ - Motion Sensor'] then
    if devicechanged['deCONZ - Motion Sensor'] == 'On' then
        print("Detecteur active")
        commandArray['deCONZ - Bulb']='On'
    else
        print("Detecteur Desactive")
        if (otherdevices['deCONZ - Bulb']== 'On') then
            commandArray['deCONZ - Bulb']='Off'
        end
    end
end