Lua Script for turning light on when motion

Moderator: leecollings

leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Lua Script for turning light on when motion

Post by leby »

Hej, change 'Set Level 100' to 'On' then it should work. Is it a dimmer or an on/off switch?

Code: Select all

commandArray = {}
if (devicechanged['Badrum-Motion'] == 'Motion' and otherdevices['Badrum-Belysning'] == 'Off') then
        commandArray['Badrum-Belysning']='On'
end
print('Badrum-Belysning on!')

return commandArray
/lennart
Chopper_Rob
Posts: 17
Joined: Wednesday 28 May 2014 21:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: NL
Contact:

Re: Lua Script for turning light on when motion

Post by Chopper_Rob »

The reason that the print command does work is that it is outside of the if statement. So every time the script runs it will always log to the event log. Put the print command before the end statement to make it dependent of the if statement.
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Lua Script for turning light on when motion

Post by leby »

Sorry, didnt see first time, a motion sensor is normally indicated as "On" not as "Motion"

Code: Select all

commandArray = {}
if (devicechanged['Badrum-Motion'] == 'On' and otherdevices['Badrum-Belysning'] == 'Off') then
        commandArray['Badrum-Belysning']='On'
print('Badrum-Belysning on!')
End
return commandArray
or

Code: Select all

commandArray = {}
if (devicechanged['Badrum-Motion']  and otherdevices['Badrum-Belysning'] == 'Off') then
        commandArray['Badrum-Belysning']='On'
print('Badrum-Belysning on!')
End
return commandArray
which motion sensor do you have?
/lennart
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Lua Script for turning light on when motion

Post by pvm »

Wouldn't it be easier to use blocky events to do this?
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Lua Script for turning light on when motion

Post by leby »

Blockly would definitely be easier but perhaps not so fun :D

The problem with your script now is that you want the time to be more than 23 and less than 8, a bit tricky I would say if you start to think about it ;)

If you change the 'and' to 'or' you would cover the time period you intend. You want the time to be 23,24, or 1,2,3,4,5,6,7,8

I have not tested the code below but I think it should work, If you want to use time in your lua script I suggest you spend a few minutes looking in to this site http://www.lua.org/pil/22.1.html

Code: Select all

commandArray = {}

hour = (os.date('%H'))
if 	(devicechanged['Badrum-Motion'] == 'On' and (hour >= 23 or hour < 8) and otherdevices['Badrum-Belysning'] == 'Off') then
	commandArray['Badrum-Belysning']='Set Level 50'
	print("Badrum Belysning on 50%")
	else
	commandArray['Badrum-Belysning']='Set Level 100'
	print('Badrum Belysning on 100%')
end
return commandArray
/lennart
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Lua Script for turning light on when motion

Post by pvm »

Agree Leby,
The script you provide is missing something, the else will work for all checks provided. I think you need need an if for the switches and within the then statement another if with checks on time
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

You could have a look at http://www.domoticz.com/wiki/Smart_Lua_ ... _Smart_PIR for examples of doing this.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Lua Script for turning light on when motion

Post by leby »

As simonrg say, the smart pir might be better but, if you want to do what you wrote in the first post you could do like the code below, I have tested it with a dummy device and its working as you intended with the twist that it turns off the light after 5 minutes, you can take that code part out if you wish to turn off manually.

Code: Select all

-- the script turns on the dimmer for 5 minutes, the level during daytime is 100 and night 50
commandArray = {}
h = tonumber((os.date('%H')))
if  	(h >= 8 and h < 23) then
	x = 'Set Level 100 FOR 5'
	else
	x = 'Set Level 50 FOR 5'
end
if 	(devicechanged['Badrum-Motion'] == 'On' and otherdevices['Badrum-Belysning'] == 'Off') then	
	commandArray['Badrum-Belysning']= x
	print(x)
end
/lennart
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

:) I'm going to insert my issue into this topic instead of creating a new one

As a first step for learing LUA funcionality I would light a lamp connected to a FibaroSwitch (Switch1) when my Fibaro Motion Sensor (OcchioMagicoFibaro-PIR) detects a movement.

I have created a simple lua file script_device_LightOnAfterMovement.lua

Code: Select all

print('this will end up in the domoticz log')

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On' then
	commandArray['Switch1']='On'
end
return commandArray
and the lamp does not light when moving in front of the MotionSensor.

Where am I wrong?

Claudio :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

cpighin wrote::) I'm going to insert my issue into this topic instead of creating a new one

As a first step for learing LUA funcionality I would light a lamp connected to a FibaroSwitch (Switch1) when my Fibaro Motion Sensor (OcchioMagicoFibaro-PIR) detects a movement.

I have created a simple lua file script_device_LightOnAfterMovement.lua

Code: Select all

print('this will end up in the domoticz log')

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On' then
	commandArray['Switch1']='On'
end
return commandArray
and the lamp does not light when moving in front of the MotionSensor.

Where am I wrong?

Claudio :)
You are missing a closing bracket on your if line:

Code: Select all

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On') then
        print('Motion detected')
	commandArray['Switch1']='On'
end
return commandArray
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

simonrg wrote:
cpighin wrote::) I'm going to insert my issue into this topic instead of creating a new one

As a first step for learing LUA funcionality I would light a lamp connected to a FibaroSwitch (Switch1) when my Fibaro Motion Sensor (OcchioMagicoFibaro-PIR) detects a movement.

I have created a simple lua file script_device_LightOnAfterMovement.lua

Code: Select all

print('this will end up in the domoticz log')

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On' then
	commandArray['Switch1']='On'
end
return commandArray
and the lamp does not light when moving in front of the MotionSensor.

Where am I wrong?

Claudio :)
You are missing a closing bracket on your if line:

Code: Select all

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On') then
        print('Motion detected')
	commandArray['Switch1']='On'
end
return commandArray
I corrected my error and tested the file which now works quite fine lighting the lamp.

I note that the action is not immediate and some times the Switch1 goes on status On after tens of seconds or doesen't act at all. Is this normal?

Claudio :)
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

:) I have done few test and realized that the LUA script works always fine (the lamp light on if a movement is detected) only if

- Dashboard shows both devices OcchioMagicoFibaro-PIR and Switch1 in the state: Off
- a movement is perceived by the Motion Sensor (OcchioMagicoFibaro-PIR)

If OcchioMagicoFibaro-PIR is in status On (with Switch1 in status On or Off), the system doesn't reacts.

I believe this is exactly what is expected Domoticz does. If not, please advice me ;)

I observed also that the status of my OcchioMagicoFibaro-PIR changes automatically from On to Off after 30 sec. if no further movement is detected (I guess this parameter can be changed in the configuration file).

After learning how to use a LUA script , I would like to improve it to ensure that the lamp comes on when the sensor detects motion and stays on unless no movements are detected for 2 minutes.

Can you help me?

Claudio :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

cpighin wrote:After learning how to use a LUA script , I would like to improve it to ensure that the lamp comes on when the sensor detects motion and stays on unless no movements are detected for 2 minutes.
Not wishing to repeat myself, but you now seem to be trying to do exactly what the Smart Lua Scripts were designed for so have a look at http://www.domoticz.com/wiki/Smart_Lua_ ... _Smart_PIR, basically will keep a light on for a defined length of time after last motion detection and can be configured by the way you name the PIR in Domoticz.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Lua Script for turning light on when motion

Post by pvm »

You can also use setting 6 in your fibaro 'eye', no need for lua (though it is nice to learn something new
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

simonrg wrote:Not wishing to repeat myself, but you now seem to be trying to do exactly what the Smart Lua Scripts were designed for so have a look at http://www.domoticz.com/wiki/Smart_Lua_ ... _Smart_PIR, basically will keep a light on for a defined length of time after last motion detection and can be configured by the way you name the PIR in Domoticz.
Tanks simong :)

I was tryong to follow yor suggestion and I encountered the first obstacle on undesrstanding what exactly to do.

In the link mentioned I read
Adding Turning Off Functionality[edit]
First in Domoticz add a new Siemens PIR and call it after a light in your house, say the "Study" light, so the PIR is named "PIRStudy", make its device type "Motion Sensor". Then add the scripts described below.
Should I understand to create a new "virtual" device (if so please instruct me on haw to) or Should I rename my actual device "OcchioMagicoFibaro-PIR" in "PIR-OcchioMagicoFibaro" (or something else)?

Claudio :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

cpighin wrote:
Adding Turning Off Functionality[edit]
First in Domoticz add a new Siemens PIR and call it after a light in your house, say the "Study" light, so the PIR is named "PIRStudy", make its device type "Motion Sensor". Then add the scripts described below.
Should I understand to create a new "virtual" device (if so please instruct me on haw to) or Should I rename my actual device "OcchioMagicoFibaro-PIR" in "PIR-OcchioMagicoFibaro" (or something else)?
You are going to rename your actual device, the idea is that then the script will know that this is a PIR just from its name and so will pick up any PIRs not just a single 1.

Further down the wiki page the idea is to add some parameters that allow you to set how long the PIR stays on for, whether it comes on only at night etc..

Of course the reason why I wrote the scripts was that I have some very simple PIRs which only say I've seen something and don't even turn off ever, it sounds like your PIR could do a lot of this itself, but it still may be nice to be able to set them from Domoticz rather than on the hardware.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

simonrg wrote: You are going to rename your actual device, the idea is that then the script will know that this is a PIR just from its name and so will pick up any PIRs not just a single 1.
Thanks simonrg :) Since i would prefer to avoid the renaming of devices, is it possible to modify your script

Code: Select all

-- ~/domoticz/scripts/lua/script_device_PIRs.lua
 
commandArray = {}
 
tc=next(devicechanged)
v=tostring(tc)
if (v:sub(1,3) == 'PIR') then
    c=v:sub(4)
    commandArray[c] = 'On'
    tmess = c..' On - time 0'
    print(tmess)
end
 
return commandArray
in order to consider only a device with a specific name (for example OcchioMagicoFibaro-PIR)?

Claudio :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

cpighin wrote:Thanks simonrg :) Since i would prefer to avoid the renaming of devices, is it possible to modify your script in order to consider only a device with a specific name (for example OcchioMagicoFibaro-PIR)?
You can modify it however you like - ;) .
Probably the easiest way to work with your device name is:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_PIRs.lua
 
commandArray = {}
 
tc=next(devicechanged)
v=tostring(tc)
-- Look for a device name which ends in PIR
if (v:sub(-3,-1) == 'PIR') then
    -- Remove -PIR from the device name and then turn that device on
    c=v:sub(1, -5)
    commandArray[c] = 'On'
    tmess = c..' On - time 0'
    print(tmess)
end
 
return commandArray

would just take the PIR off the end.

Alternatively just write yourself from scripts from scratch, a script_device_name.lua which turns on the light whenever the PIR is detected, then a script_time_name.lua which turns the light off when the last update was x minutes ago. How to do this is in the more flexible scripts.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
cpighin
Posts: 68
Joined: Friday 03 July 2015 10:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Assisi, Italy
Contact:

Re: Lua Script for turning light on when motion

Post by cpighin »

simonrg wrote:
cpighin wrote: You are missing a closing bracket on your if line:

Code: Select all

commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On') then
        print('Motion detected')
	commandArray['Switch1']='On'
end
return commandArray
Simonrg, few days ago you corrected my LUA file that now works fine lighting On my lamp after a motion detection.

Now I would like to improve the system to ensure that the lamp comes On when the sensor detects motion and stays On unless no movements are detected for x time (120 sec. as an example).

You have just wrote
simonrg wrote:Alternatively just write yourself from scripts from scratch, a script_device_name.lua which turns on the light whenever the PIR is detected, then a script_time_name.lua which turns the light off when the last update was x minutes ago. How to do this is in the more flexible scripts.
and I understand that I can use the LUA file you corrected as the one that turns On the light and a new script_time_name.lua which turns the light Off when the last update was 120 minutes ago, eventually using parts of your article mentioned above.

Am I write?

Claudio :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script for turning light on when motion

Post by simonrg »

cpighin wrote:You corrected my LUA file that now works fine lighting On my lamp after a motion detection.

Now I would like to improve the system to ensure that the lamp comes On when the sensor detects motion and stays On unless no movements are detected for x time (120 sec. as an example).

You have just wrote
simonrg wrote:Alternatively just write yourself from scripts from scratch, a script_device_name.lua which turns on the light whenever the PIR is detected, then a script_time_name.lua which turns the light off when the last update was x minutes ago. How to do this is in the more flexible scripts.
and I understand that I can use the LUA file you corrected as the one that turns On the light and a new script_time_name.lua which turns the light Off when the last update was 120 minutes ago, eventually using parts of your article mentioned above.

Am I right?

Claudio :)
Claudio, yes you can use your original script to turn the light on:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_occhio.lua
commandArray = {}
if (devicechanged['OcchioMagicoFibaro-PIR'] == 'On') then
        print('Motion detected')
	commandArray['Switch1']='On'
end
return commandArray
And then just a time script to turn it off after 120minutes.

Simon
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

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