Smart Lua Scripts, cannot get it to work

Moderator: leecollings

Post Reply
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Smart Lua Scripts, cannot get it to work

Post by BarryT »

Hi all,

i have a x10 motion sensor, wich works with the 'normal' PIR lua script.
It goes on, and off after 60 seconds.

But, i just want to do it only when it is dark, but what i'm just doing, its not gonna work.
I use the script_time_PIRs.lua and the script_device_PIRs.lua as seen in the Wiki.
Also i've struggeling with the "Reacting to a Dummy Switch" part, wich is also not working.
The light is going on, but never goes off anymore.
Also there is no "monitoring" of what the lua is doing.
If i turn off the light by hand, and it's switched on when motion is detected, it's not going off anymore :-(

This are my scripts:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_pirs.lua
-- Each of the motion sensors in Domoticz follow this name convention:
-- PIRxrzSwitchName or PIRxgzGroupName
-- x speicifies when the PIR controls - a=all day, n=nighttime, d=daytime,
--   1=custom timer 1 set to 22:00 to 07:30
-- y specifies what the PIR is controlling -
--   r = room (single Domoticz) switch and g = group
-- z specifies how long the ligth will stay on for in minutes, so z = 5 turns
--   the switch or the group on for 5 minutes
-- N.B. be carefully as currently there is little error checking so wrongly
--      named PIRs in Domoticz may cause an error
-- N.B. one wrongly named PIR may stop the script, check log for any issues
 
function customtest(nowhours, nowmins, starthours, startmins, endhours, endmins)
   nowmins = nowmins + (nowhours * 60)
   startmins = startmins + (starthours * 60)
   endmins = endmins + (endhours * 60)
   if (startmins > endmins) then
--    spans midnight
      if ((nowmins >= startmins) or (nowmins <= endmins)) then
         return true
      else
         return false
      end
   else
--    doesn't span midnight
      if ((nowmins >= startmins) and (nowmins <= endmins)) then
         return true
      else
         return false
      end
   end
end
 
function timetest(opertime)
   if opertime == "a" then
      return true
   end
   if opertime == "n" then
      if timeofday['Nighttime'] then
         return true
      else
         return false
      end
   end
   if opertime == "d" then
      if timeofday['Daytime'] then
         return true
      else
         return false
      end
   end
   if opertime == "s" then
      if (otherdevices['HetisDonker'] == "On") then
         return true
      else
         return false
      end
   end
   if opertime == "1" then
      time = os.date("*t")
      return customtest(time.hour, time.min, 22, 0, 7, 30)
   end
   return false
end
 
commandArray = {}
 
tc=next(devicechanged)
v=tostring(tc)
if (v:sub(1,3) == 'PIR') then
   if timetest(v:sub(4,4)) then
      c=v:sub(7)
      if v:sub(5,5) == "g" then
         c="Group:"..c
      end
      commandArray[c] = 'On'
      tmess = c..' On - time 0'
      print(tmess)
   end
end
 
return commandArray
and

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
pir motion sensor named as PIRsr1Trap
switch named as Trap

Thanks!
Last edited by BarryT on Monday 03 October 2016 16:37, edited 3 times in total.
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: Smart Lua Scripts, cannot get it to work

Post by simonrg »

Not quite sure what is going wrong, but a few comments.

Please could you put the text into a code block in the forum(

Code: Select all

[code]Your code here
[/code]), then it will be a lot more readable with all the indentations etc..

Your X10 PIR is smarter than assumed by the script, as it sends both on and off signals, many only send on. So the script assumes any device change event is an on, so with your PIRs, the script will switch the light on for 1 minute, then the PIR send the off and the script will switch the light on again for 1 minute. So we would need to add a test to the script that it is only reacting to on and not off.

Are you only after 1 minute on? If you really only want 1 minute on, then these scripts aren't perhaps the best way to go, as time scripts are only tested every minute at 00 seconds, so if the PIR fires at 12:01:45, the script will turn the light off at 12:02:00 - only 15 seconds.

http://www.domoticz.com/wiki/Smart_Lua_ ... mes_of_Day
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
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Smart Lua Scripts, cannot get it to work

Post by BarryT »

simonrg wrote:Not quite sure what is going wrong, but a few comments.

Please could you put the text into a code block in the forum(

Code: Select all

[code]Your code here
[/code]), then it will be a lot more readable with all the indentations etc..

Your X10 PIR is smarter than assumed by the script, as it sends both on and off signals, many only send on. So the script assumes any device change event is an on, so with your PIRs, the script will switch the light on for 1 minute, then the PIR send the off and the script will switch the light on again for 1 minute. So we would need to add a test to the script that it is only reacting to on and not off.

Are you only after 1 minute on? If you really only want 1 minute on, then these scripts aren't perhaps the best way to go, as time scripts are only tested every minute at 00 seconds, so if the PIR fires at 12:01:45, the script will turn the light off at 12:02:00 - only 15 seconds.

http://www.domoticz.com/wiki/Smart_Lua_ ... mes_of_Day
Ah pardon me Simon, was forgot to include the codes..

Anyway, Thanks.
I wrote already a really basic, simple lua script.

Script_Device_Trap.lua

Code: Select all

commandArray = {}

    if (otherdevices['HetisDonker'] == "On") and (devicechanged['MotionTrap'] == 'Motion') then
          commandArray['Trap'] = "On"

end
return commandArray
What is does, is it checks if the "isdark" sensor is on then it triggers (activate) the pir sensor.
After that, i use a switch "trap" wich is used for the lights, and did a delay OFF action after x minutes in that switch.
As long as the PIR detect motion, the light will be on, and after that the DELAY takes care of the OFF action of the lamp.
Never tought it was that simple!
It works, just perfect.
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: Smart Lua Scripts, cannot get it to work

Post by simonrg »

BarryT wrote:I wrote already a really basic, simple lua script.

Script_Device_Trap.lua

Code: Select all

commandArray = {}

    if (otherdevices['HetisDonker'] == "On") and (devicechanged['MotionTrap'] == 'Motion') then
          commandArray['Trap'] = "On"

end
return commandArray
What is does, is it checks if the "isdark" sensor is on then it triggers (activate) the pir sensor.
After that, i use a switch "trap" wich is used for the lights, and did a delay OFF action after x minutes in that switch.
As long as the PIR detect motion, the light will be on, and after that the DELAY takes care of the OFF action of the lamp.
Never tought it was that simple!
It works, just perfect.
Great, glad its working.

The idea of the Smart Script is that it reuses things hence easier debugging while retaining flexibility. However, if all you need is a single PIR - light combo and you have a dark sensor, then your script is far easier and the other scripts are overkill.

When the scripts were written Domoticz didn't have the delay avaiable in the switch setting built in.

Domoticz knows when it is night time, so you could also use. timeofday['Nighttime'], which will be true when it is night.
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
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Smart Lua Scripts, cannot get it to work

Post by BarryT »

simonrg wrote:
BarryT wrote:I wrote already a really basic, simple lua script.

Script_Device_Trap.lua

Code: Select all

commandArray = {}

    if (otherdevices['HetisDonker'] == "On") and (devicechanged['MotionTrap'] == 'Motion') then
          commandArray['Trap'] = "On"

end
return commandArray
What is does, is it checks if the "isdark" sensor is on then it triggers (activate) the pir sensor.
After that, i use a switch "trap" wich is used for the lights, and did a delay OFF action after x minutes in that switch.
As long as the PIR detect motion, the light will be on, and after that the DELAY takes care of the OFF action of the lamp.
Never tought it was that simple!
It works, just perfect.
Great, glad its working.

The idea of the Smart Script is that it reuses things hence easier debugging while retaining flexibility. However, if all you need is a single PIR - light combo and you have a dark sensor, then your script is far easier and the other scripts are overkill.

When the scripts were written Domoticz didn't have the delay avaiable in the switch setting built in.

Domoticz knows when it is night time, so you could also use. timeofday['Nighttime'], which will be true when it is night.
Okay, nice..
I did using the nighttime, but that didnt work.
It also give me an lua error in the log, thats the reason i used the isdark switch.
Also, there is more control when usibg a switch instead of the nighttime in lua.
I use 4 pirs atm with the same script, and they all works superfast and stable!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest