Lua Script for turning light on when motion

Moderator: leecollings

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 »

OK Simon :)

I will try using your script

Code: Select all

-- ~/domoticz/scripts/lua/script_time_PIRs.lua
 
function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}
 
for i, v in pairs(otherdevices) do
   timeon = 240
   tc = tostring(i)
   v = i:sub(1,3)
   if (v == 'PIR') then
      difference = timedifference(otherdevices_lastupdate[tc])
      if (difference > timeon and difference < (timeon + 60)) then
         tempdiff = tostring(difference)
         c = i:sub(4)
         tempmessage = c.." Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
         print(tempmessage)
         commandArray[c] = 'Off'
      end
   end
end
 
return commandArray
If I have correctly understood, your script turns On a device which name start with PIR and this switch On a lamp. In my case my PIR device "OcchioMagicoFibaro-PIR" turns On the device "Switch1" (to whom I connected my table lamp) when a motion is detected.

So I guess that I have to modify your above script in order to tell Domoticz to switch Off device "Switch1" if no motions are detected during 120 sec.

Considering your suggestions I tried to modifify your script as follow

Code: Select all

-- ~/domoticz/scripts/lua/script_time_PIRs.lua
 
function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}
 
for i, v in pairs(otherdevices) do
   timeon = 120
   tc=next(devicechanged)
   v=tostring(tc)
   -- Look for a device name which ends in PIR
   if (v:sub(-3,-1) == 'PIR') then
      difference = timedifference(otherdevices_lastupdate[tc])
      if (difference > timeon and difference < (timeon + 60)) then
         tempdiff = tostring(difference)
         c = i:sub(4)
         tempmessage = c.." Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
         print(tempmessage)
         commandArray[c] = 'Off'
      end
   end
end
 
return commandArray
You may see that I arranged part of code that you suggested at 14:17 to change only lines:
timeon = 120
tc=next(devicechanged)
v=tostring(tc)
-- Look for a device name which ends in PIR
if (v:sub(-3,-1) == 'PIR') then

to set a period of 120 seconds and to tell Domoticz to serarch for a device name which ends in PIR (my "OcchioMagicoFibaro-PIR")

I don't know if I have done correctly until this point (please advice me) and furthermore I don't know how to modify the bottom part of your script to instruct Domoticz to switch Off device "Switch1" :(

Would 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:OK Simon :)

I will try using your script
If I have correctly understood, your script turns On a device which name start with PIR and this switch On a lamp. In my case my PIR device "OcchioMagicoFibaro-PIR" turns On the device "Switch1" (to whom I connected my table lamp) when a motion is detected.

So I guess that I have to modify your above script in order to tell Domoticz to switch Off device "Switch1" if no motions are detected during 120 sec.

Considering your suggestions I tried to modifify your script as follow ....

You may see that I arranged part of code that you suggested at 14:17 to change only lines:
timeon = 120
tc=next(devicechanged)
v=tostring(tc)
-- Look for a device name which ends in PIR
if (v:sub(-3,-1) == 'PIR') then

to set a period of 120 seconds and to tell Domoticz to serarch for a device name which ends in PIR (my "OcchioMagicoFibaro-PIR")

I don't know if I have done correctly until this point (please advice me) and furthermore I don't know how to modify the bottom part of your script to instruct Domoticz to switch Off device "Switch1" :(

Would you help me?

Claudio :)
What you have done is fine, but is rather complicated as you only want to operate 1 PIR and 1 Switch by rewriting code which is flexible on PIR and Switch but designed for a specific naming convention.

So, if you are starting with your simple script:

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 make a simple script for the time part:

Code: Select all

-- ~/domoticz/scripts/lua/script_time_occhio.lua
 
function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}

-- Set length of time light should be on for in seconds
timeon = 120
-- Calculate time since time PIR was last activated
difference = timedifference(otherdevices_lastupdate['OcchioMagicoFibaro-PIR'])
-- If the time since last activation was within 1 minute of time for light to stay on
if (difference > timeon and difference < (timeon + 61)) then
   tempdiff = tostring(difference)
   tempmessage = "Switch1 Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
   print(tempmessage)
   -- Switch off Switch1
   commandArray['Switch1'] = 'Off'
end
 
return commandArray
This script will only work for OcchioMagicoFibaro-PIR and Switch1.

As your PIR also switches "off" you will need to set the time before switching off on the physical PIR to a minute or less, as the script will only turn the light off a set time after the last update to Domoticz, which could be either on or off. Alternatively a long time longer than the time you want it stay on for would also work, as the last update would then be definitely the PIR motion detected event.
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 »

Thanks Simon :D

your scripts work fine and untill now I cant see any problem you mentioned on
simonrg wrote: As your PIR also switches "off" you will need to set the time before switching off on the physical PIR to a minute or less, as the script will only turn the light off a set time after the last update to Domoticz, which could be either on or off. Alternatively a long time longer than the time you want it stay on for would also work, as the last update would then be definitely the PIR motion detected event.
As a matter of fact i didn't understand what you mean. Can you explain better?

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 »

pvm wrote:You can also use setting 6 in your fibaro 'eye', no need for lua (though it is nice to learn something new
From previously in the thread, it was stated your Fibaro PIR has both an on and an off state, and it turns off after a set number of minutes.

Domoticz tells you a device's current state and when it last changed state.

The script only looks at the last change of state, so if your device changes to off within the time you are trying to control your light for then, this would increase the amount of time the light stayed on for.

But if the script is working then job done.
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 »

Thanks Simonrg, Claudio :)
nikels
Posts: 10
Joined: Wednesday 01 July 2015 23:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Lua Script for turning light on when motion

Post by nikels »

Hello i am pretty new to scripting a have a few good working scripts.

I have used the above scripts but i still have a question! I have noticed that starting a script is faster then a blocky script.

– My question is how could I use a motion sensor to start a device when the its dark (from SUNSET to SUNRISE )



--- And not start a device in Domoticz but a (insteon )device with this line:
http://Test:[email protected]:25105/3? ... 0F12FF=I=3
--- This line to stop (with a delay of 30 seconds) OFF with this line:
http://Test:[email protected]:25105/3? ... 0F1400=I=3

I would like to know if someone can offer me some help.
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 »

nikels wrote:My question is how could I use a motion sensor to start a device when the its dark (from SUNSET to SUNRISE )
You could use the Smart Scripts documented on the Wiki which have an option to only work at night - http://www.domoticz.com/wiki/Smart_Lua_Scripts
nikels wrote:--- And not start a device in Domoticz but a (insteon )device with this line:
http://Test:[email protected]:25105/3? ... 0F12FF=I=3
--- This line to stop (with a delay of 30 seconds) OFF with this line:
http://Test:[email protected]:25105/3? ... 0F1400=I=3
Easiest thing would be to create a dummy device then put your http requests an the On action and the Off action (create the switch then click edit to access the On / Off actions).
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
nikels
Posts: 10
Joined: Wednesday 01 July 2015 23:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Lua Script for turning light on when motion

Post by nikels »

Hello i am now trying this script! I have not tested it yet!

The problem is i can not turn off the motion sensor by day because its also activating a recording blocky.

The blocky activates a API URL for recording in Synology SS this blocky works all day i just want to do the other job of turning on a light when its dark!

I hope this blocky only will activate the light when its dark and not turn off the motion sensor so that it will still activate the blocky script that activates the recording.

Code: Select all

commandArray = {}

time = os.date("*t")
minutes = time.min + time.hour * 60
    if (timeofday['Nighttime']) and (devicechanged['Bewegings melder poort'] == 'On') then
          commandArray['OpenURL']='http://xxxx:[email protected]:xxxx/3?026226391D0F11FF=I=357'

end

  if (timeofday['Nighttime']) and (devicechanged['Bewegings melder poort'] == 'Off') then
          commandArray['OpenURL']='http://xxxx:[email protected]:xxxx/3?026226391D0F1400=I=33'

end
return commandArray
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 »

nikels wrote:The problem is i can not turn off the motion sensor by day because its also activating a recording blocky.
The blocky activates a API URL for recording in Synology SS this blocky works all day i just want to do the other job of turning on a light when its dark!
I hope this blocky only will activate the light when its dark and not turn off the motion sensor so that it will still activate the blocky script that activates the recording.
The beauty of scripiting is that you can abstract behaviour from the hardware you are using, so a sensor or a switch can be interpreted in different ways in different scripts.

So to keep this simple, if you have a lamp called Light, then you would call your PIR by a related name as per the smart script PIRnr4Light, now the light will come on if you have used the smart scripts. There is nothing then stopping you from writing Blockly which detect PIRnr4Light and switches your recordings on and off 24 hours a day.

In your case Light would have On and Off actions which use your http to control the lamp.
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
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 »

simonrg wrote:
nikels wrote:The problem is i can not turn off the motion sensor by day because its also activating a recording blocky.
The blocky activates a API URL for recording in Synology SS this blocky works all day i just want to do the other job of turning on a light when its dark!
I hope this blocky only will activate the light when its dark and not turn off the motion sensor so that it will still activate the blocky script that activates the recording.
The beauty of scripiting is that you can abstract behaviour from the hardware you are using, so a sensor or a switch can be interpreted in different ways in different scripts.

So to keep this simple, if you have a lamp called Light, then you would call your PIR by a related name as per the smart script PIRnr4Light, now the light will come on if you have used the smart scripts only at night for 4 minutes. There is nothing then stopping you from writing Blockly which detects PIRnr4Light and switches your recordings on and off 24 hours a day.

In your case Light would have On and Off actions which use your http code to control the lamp directly.
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
nikels
Posts: 10
Joined: Wednesday 01 July 2015 23:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Lua Script for turning light on when motion

Post by nikels »

Code: Select all

commandArray = {}

time = os.date("*t")
minutes = time.min + time.hour * 60
    if (timeofday['Nighttime']) and (devicechanged['Bewegings melder poort'] == 'On') then
          commandArray['OpenURL']='http://xxxx:[email protected]:xxxx/3?026226391D0F11FF=I=357'

end

  if (timeofday['Nighttime']) and (devicechanged['Bewegings melder poort'] == 'Off') then
          commandArray['OpenURL']='http://xxxx:[email protected]:xxxx/3?026226391D0F1400=I=33'

end
return commandArray
Above script is also working perfect! Only On when after sunrise and not turn on when its light.
fantic
Posts: 1
Joined: Saturday 30 December 2017 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: NL
Contact:

Re: Lua Script for turning light on when motion

Post by fantic »

The Smart Off script on the sample page does not keep into account the timings you set with the first parameter, so I added that part to the Smart off script as I needed the light to be on during normal evening hours and switch on by movement during the night time. The way the script was it would just turn off after the set time of z minutes anytime. now that is done the smart way.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), NEO Coolcam sockets, 4 Siemens PIR, KlikAanKlikUit sockets, Oregon Temp humidity sensors.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest