LUA PIR on/off

Moderator: leecollings

Post Reply
Martijn85
Posts: 53
Joined: Wednesday 20 January 2016 20:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

LUA PIR on/off

Post by Martijn85 »

I can't figure out why my LUA script is not working correctly.

I have a motion detector in Domoticz with the name "PIR Gang Voor". The ON function is working, but the OFF function, afther a couple of minutes is not working, it`s not going OFF.

I use these scripts: https://www.domoticz.com/wiki/Smart_Lua_Scripts

When a motion is detected i use this script. This works fine and the motion detector is going ON.

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
When the motions detector is not sense any motions for 2 minutes it should go OFF, this is the script i use:

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 = 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
It looks like the name is resolving correctly, when i change commandArray[c] = 'Off' to commandArray['PIR Gang Voor'] = 'Off' it's working and the motion detector is going OFF afther 2 minutes.

How come that the command commandArray[c] = 'Off' is not with the OFF command but is working with the ON command in the first script?
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 PIR on/off

Post by simonrg »

Martijn85 wrote:I have a motion detector in Domoticz with the name "PIR Gang Voor". The ON function is working, but the OFF function, afther a couple of minutes is not working, it`s not going OFF.

I use these scripts: https://www.domoticz.com/wiki/Smart_Lua_Scripts

How come that the command commandArray[c] = 'Off' is not with the OFF command but is working with the ON command in the first script?
It looks like the script is not doing what you expect as you have not followed the strict naming convention. If the light switch is called "Gang Voor" then your PIR should be called "PIRGang Voor" i.e. no space between "PIR" and "Gang Voor". You could adapt the script to allow spaces.

N.B. The script does not turn the PIR on or off, it reacts to the PIR turning on the associated light. It is mean't mainly for PIRs which only turn on, but never off.

So your PIR is turning itself on and the script is doing nothing in your current set up.

In fact the more complex script is easier to follow as you set the parameters in the name and it gives a lot of flexibility, allowing you to set behaviour in software visibly in the PIR device name. If your light switch is called "Gang Voor" and you want the PIR to turn it on for 2 minutes, all the time then you should name the PIR "PIRar2Gang Voor", following the naming convention set out in original script comments:
https://www.domoticz.com/wiki/Smart_Lua ... ied_Coding

Code: Select all

-- ~/domoticz/scripts/lua/script_device_pirs.lua
-- Each of the motion sensors in Domoticz follow this name convention:
-- PIRxyzSwitchName or PIRxyzGroupName
-- x speicifies when the PIR controls - a=all day, n=nighttime, d=daytime, s=Dummy switch set in Domoticz which
--    has to be on for the PIR to operate - Dummy-SwitchName or Dummy-GroupName
--    custom-timers can be set by creating a timed dummy switch in Domoticz
-- 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
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
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: LUA PIR on/off

Post by Dnpwwo »

I also have PIRs that only send 'On' and never off.

To turn them off I specify a number of seconds in the device's 'Off Delay' field (on the Edit Device page) instead of a Lua script because native functionality beats scripting every time.

Have a look at http://www.domoticz.com/forum/viewtopic ... 170#p20532
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
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 PIR on/off

Post by simonrg »

Dnpwwo wrote:To turn them off I specify a number of seconds in the device's 'Off Delay' field (on the Edit Device page) instead of a Lua script because native functionality beats scripting every time.
I like the idea of keeping things simple.

So I set a 60second off delay for my PIR Motion Sensor device in Domoticz, however it still doesn't seem to ever turn off.

Also setting the light as a slave of the PIR does not seem to get the light to turn on.

Your system is obviously working, how have you set up the PIR to control the light without a script?
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
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: LUA PIR on/off

Post by Dnpwwo »

Apologies, I mis-read the question.

To do what you are after I have a Time based script, my devices are named consistently (i.e '<room> Light', '<room> Fan', '<room> Heat Lamps', '<room> PIR') in the wet areas of my house so this covers multiple rooms.

Fans always get turned off after a set time, Heat Lamps and Lights get turned off after a set time and when the local PIR has not seen movement for a while:

Code: Select all

function timedifference (s)
  ret = os.difftime (os.time(), os.time{year=string.sub(s, 1, 4), month=string.sub(s, 6, 7), day=string.sub(s, 9, 10), hour=string.sub(s, 12, 13), min=string.sub(s, 15, 16), sec=string.sub(s, 18, 19)})  return ret
end

waitTime = 300           -- 5 minutes, time to leave devices on without movement
commandArray = {}
--
-- loop through all the devices
--
for deviceName,deviceValue in pairs(otherdevices) do
    -- Time based Bathroom Light/Fan/PIR/Humidity rules
    if (deviceValue == "On") and (timedifference(otherdevices_lastupdate[deviceName]) > waitTime) then
        if (string.sub(deviceName,-3) == 'Fan') then
            -- If a fan has been on longer than the specified time, turn it off
            print ("Active device time check on '"..deviceName.."', last seen "..timedifference(otherdevices_lastupdate[deviceName]).." seconds ago")
            commandArray[deviceName]='Off'
        end
            if (string.sub(deviceName,-10) == 'Heat Lamps') then
            -- If Heat Lamps has been on longer than the specified time, turn it off if the related PIR has not seen movement recently
            local pirDev = string.sub(deviceName,1,string.len(deviceName)-10)..'PIR'
            if (otherdevices[pirDev] ~= nil) then
                print ("Active device time check on '"..deviceName.."', last movement "..timedifference(otherdevices_lastupdate[pirDev]).." seconds ago")
                if (timedifference(otherdevices_lastupdate[pirDev]) > waitTime) then
                    commandArray[deviceName]='Off'
                end
            end
        end
        if (string.sub(deviceName,-5) == 'Light') then
            -- If light has been on longer than the specified time, turn it off if the related PIR has not seen movement recently
            local pirDev = string.sub(deviceName,1,string.len(deviceName)-5)..'PIR'
            if (otherdevices[pirDev] ~= nil) then
                print ("Active device time check on '"..deviceName.."', last movement "..timedifference(otherdevices_lastupdate[pirDev]).." seconds ago")
                if (timedifference(otherdevices_lastupdate[pirDev]) > waitTime*2) then
                    commandArray[deviceName]='Off'
                end
            end
        end
    end
end
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
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 PIR on/off

Post by simonrg »

Dnpwwo wrote:Apologies, I mis-read the question.

To do what you are after I have a Time based script, my devices are named consistently (i.e '<room> Light', '<room> Fan', '<room> Heat Lamps', '<room> PIR') in the wet areas of my house so this covers multiple rooms.

Fans always get turned off after a set time, Heat Lamps and Lights get turned off after a set time and when the local PIR has not seen movement for a while:
Thanks for clarification, a shame because for many instances what you wrote would be a very simple solution, but it seems the hardware of my Byron SX Chime PIRs don't allow an off state and I can't get slave devices to work correctly.

I really like your using a suffix on the device name to allow you to have a single script which controls multiple different rooms / devices, in the same way that the PIR as a prefix does, this gives a lot of flexibility and makes the settings visible as they are part of the device name.
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
Martijn85
Posts: 53
Joined: Wednesday 20 January 2016 20:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: LUA PIR on/off

Post by Martijn85 »

simonrg wrote:
It looks like the script is not doing what you expect as you have not followed the strict naming convention.
Sorry for this very late reaction! The script does indeed not what I was expecting. I just wanted to turn the motion detector off when i`t going on. I have solved this simple by entering the Off Delay option in the switch.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest