Page 2 of 3
Re: ByronSX PIR
Posted: Sunday 19 January 2014 23:50
by simonrg
@windriverski thanks, sorry should have checked post more carefully.
Code: Select all
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
tc = tostring(i)
v = i:sub(1,3)
if (v == 'PIR') then
difference = timedifference(otherdevices_lastupdate[tc])
if (difference > 239 and difference < 320) then
tempdiff = tostring(difference)
c = i:sub(5)
tempmessage = c.." Light Off - at least 240 secs up - actually - " .. tempdiff .. "seconds"
print(tempmessage)
commandArray[c] = 'Off'
end
end
end
return commandArray
Simon
Re: ByronSX PIR
Posted: Friday 24 January 2014 1:28
by simonrg
Domoticz and a bit of LUA script have turned these Siemens wireless PIRs into the ultimate smart PIRs (tm)

that work with the LightwaveRF system
The Siemens wireless PIRs only send an on signal (detectd) but no off signal, so I had to write a couple of scripts to make them function more like normal PIRs and actually turn things on and then off after a set length of time - see previous posts.
I liked the idea of having a single script that interacted with all the PIRs such that I could add more PIRs without adding any extra script. So by associating each PIR with a Domoticz motion sensor called "PIR roomx" it would turn on lightswitch "roomx". Initially this meant that each PIR and light switch did exactly the same thing, so I settled on turning things on all the day for a set length of time. However, having posted the script here and on the LightwaveRFcommunity.org.uk forum, then other people suggested why couldn't they control groups or only control at certain times.
So I have now extended the scripts to create the ultimate smart PIR (tm)
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, 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, or u = don't turn off ever
So to use and install just place the two scripts in ~/domoticz/scripts/lua, then create your correctly named PIRs and you will have the ultimate smart PIR (tm)

system.
script_device_pirs.lua
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, 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, or u = don't turn off ever
-- 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 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 == "1" then
time = os.date("*t")
if ((time.hour <= 7) and (time.min <= 30)) or (time.hour >= 22) then
return true
else
return false
end
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
script_time_pirs.lua
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, 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, or u = don't turn off ever
-- 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 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
tc = tostring(i)
v = i:sub(1,3)
if (v == 'PIR') then
onmode = i:sub(6,6)
if onmode ~= "u" then
timeon = (tonumber(onmode) * 60) - 1
difference = timedifference(otherdevices_lastupdate[tc])
if (difference > timeon and difference < (timeon + 60)) then
tempdiff = tostring(difference)
c = i:sub(7)
if i:sub(5,5) == "g" then
c="Group:"..c
end
tempmessage = c.." Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
print(tempmessage)
commandArray[c] = 'Off'
end
end
end
end
return commandArray
So please enjoy the ultimate smart PIR (tm)

system
Re: ByronSX PIR
Posted: Friday 24 January 2014 8:46
by CopyCatz
Nice work Simon!
Re: ByronSX PIR
Posted: Friday 24 January 2014 14:06
by markk
Pure genius. Thanks very much Simon.
Re: ByronSX PIR
Posted: Saturday 25 January 2014 10:56
by markk
Hi Simon
Is there a simple amendment that can be made to the first script which would start the timer but not actually turn the light on? I ask because my family don't tend to have a problem switching lights on when they enter a room, only turning them off when they leave it

. Without the on command I could have the off command working all day at set times after last movement without turning lights on during the day as well.
Thanks
Mark
Re: ByronSX PIR
Posted: Saturday 25 January 2014 13:24
by simonrg
Hi Mark,
Yes, I think it will almost do it already, if you set the custom timer 1 then it will only turn on between 22:00 & 07:30, but I think it will turn off anyway as it is the PIR which starts the timer not the light. Give it a try and see, set PIR name to use custom timer 1, walk in the room the light won't turn on unless between the correct times, but if you then switch on the light and leave the room, the light will switch off after set time.
I could explicitly add this to script, but for the time being it should work anyway.
Simon
Re: ByronSX PIR
Posted: Wednesday 29 January 2014 1:07
by markk
Hi @simonrg
I have finally got your script working fantastically and have realised where I was going wrong. I was setting the time off time to 15 minutes but it wasn’t working. I finally tried it at 9 minutes and it worked so it appears the time off time can only be from 1-9. It won’t work with any two digit number. I would love to be able to set my lights to stay on for a bit longer than 9 minutes before they plunge us into darkness. One final amendment to your already ultimate script would be the icing on the cake for me
Re: ByronSX PIR
Posted: Wednesday 29 January 2014 7:18
by Derik
Is this sensor to buy in Holland?
Or a clone?
Re: ByronSX PIR
Posted: Wednesday 29 January 2014 8:38
by simonrg
@markk I had kept x y z as single characters to make the script simpler and potentially quicker. However there is no reason why these have to correspond directly to minutes. z could be used as an index to look up in a table, then any character could be used or just change the calculation. So, a quick fix is just to change the single line in the script_time_pirs.lua script where the length of timeto stay on is calculated.
Current version the value of z (onmode) is multiplied by 60 seconds:
timeon = (tonumber(onmode) * 60) - 1
Either to get double the time, i.e. 2, 4, 6, 8, 12, 14, 16, 18 minutes on
timeon = (tonumber(onmode) * 120) - 1
Or to square z to get, 1, 4, 9, 16, 25, 36, 49, 64, 89 minutes on
timeon = (60 * tonumber(onmode) ^ 2) - 1
Re: ByronSX PIR
Posted: Wednesday 29 January 2014 13:35
by markk
Oh yes, that's just perfect. Thanks v much Simon
Re: ByronSX PIR
Posted: Wednesday 29 January 2014 13:42
by markk
Just to add that if, like me, any of you have a Wickes or Response SA range wirefree alarm system installed, the door contacts and PIRs in this range work exactly the same as the byron SX PIRs and therefore perfectly with @simonrg's scripts.
Since I had these in almost every room anyway I now have automatic on/off lights in them now too

Re: ByronSX PIR
Posted: Sunday 02 February 2014 20:52
by Mathiasw
Simon, I don't quite understand the line of code:
Code: Select all
if ((time.hour <= 7) and (time.min <= 30)) or (time.hour >= 22) then
Wouldn't that mean that "timezone" 1 is 2200 to midnight, and the first half hour of every hour from midnight to 0730?
Re: ByronSX PIR
Posted: Monday 03 February 2014 1:57
by simonrg
@Mathiasw thanks for pointing this out, it doesn't make sense because it's not supposed to be that way,

it is a bug, if you look at the line in context:
Code: Select all
if ((time.hour <= 7) and (time.min <= 30)) or (time.hour >= 22) then
return true
else
return false
end
Basically, the clause will only return true if the hour is less than or equal to 7 and the minutes are less than or equal to 30, so 6:35 would result in false.
So it should be:
Code: Select all
if (((time.hour * 60) + time.min) <= ((7 * 60) + 30)) or (time.hour >= 22) then
return true
else
return false
end
Now if hours * 60 + minutes < 7 * 60 + 30 then it will return true correctly, i.e. any time between 10pm and 7:31am.
I should really write a function then it would be easy to set up any custom times.
Here's the function:
Code: Select all
function customtest(nowhours, nowmins, starthours, startmins, endhours, endmins)
nowmins = nowmins + (nowhours * 60)
startmins = startmins + (starthours * 60)
endmins = endmins + (endhours * 60)
-- print(nowmins..":"..startmins..":"..endmins)
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
This purely returns true or false depending on whether within operation time or not. It also copes with times that span midnight or times that don't.
So the current state of the script_device_pirs.lua is:
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, 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)
-- print(nowmins..":"..startmins..":"..endmins)
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 == "1" then
time = os.date("*t")
return customtest(time.hour, time.min, 22, 0, 7, 30)
end
return false
end
commandArray = {}
tc=next(devicechanged)
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
I am a bit concerned that this script is now becoming a bit long and as such may start to impact Domoticz's performance, but LUA seems fairly efficient so should be ok. I guess the best thing is only to implement this version if you want to use custom timers. In which case you can just add extra ones with:
Code: Select all
if opertime == "1" then
time = os.date("*t")
return customtest(time.hour, time.min, 22, 0, 7, 30)
end
if opertime == "2" then
time = os.date("*t")
return customtest(time.hour, time.min, 0, 0, 5, 30)
end
if opertime == "3" then
time = os.date("*t")
return customtest(time.hour, time.min, 9, 1, 17,59)
end
ONce again, thanks for the pointing out my error

.
Re: ByronSX PIR
Posted: Tuesday 04 February 2014 9:53
by Mathiasw
I've been programming for 40 years and I was sure the code was wrong, but being new to the community it seemed politer to say "I think the may be an issue with the code"!
I solve the time comparison problem by converting the time into an integer (hours*100)+minutes, then it becomes a simple comparison between eg.
CompositeTime > 0630 and CompositeTime < 2215
.. For example.
Re: ByronSX PIR
Posted: Monday 10 February 2014 22:13
by Stokie21
Hi Again Simon, sorry not had anytime online over the last couple of weeks
I am still having trouble setting this up, due to me not knowing anything about code
i entered the scripts
Made a group called Birds and added a few switches to test
I called the PIR, PIR1gzBirds i am guessing thats where I set up the 1 for custom timer, the G for Group, and the Z for 5 mins???
but can not get this to work, its just giving an error which is
Error: Mon Feb 10 21:09:38 2014 scripts\lua\script_time_pirs.lua:31: attempt to perform arithmetic on a nil value
I know it might be a simple error for a coder, but i know nothing about code so it might aswel be in german!
Can you point me in the correct direction to get this thing going?
Re: ByronSX PIR
Posted: Monday 10 February 2014 23:36
by markk
Stokie21 wrote:Hi Again Simon, sorry not had anytime online over the last couple of weeks
I am still having trouble setting this up, due to me not knowing anything about code
i entered the scripts
Made a group called Birds and added a few switches to test
I called the PIR, PIR1gzBirds i am guessing thats where I set up the 1 for custom timer, the G for Group, and the Z for 5 mins???
but can not get this to work, its just giving an error which is
Error: Mon Feb 10 21:09:38 2014 scripts\lua\script_time_pirs.lua:31: attempt to perform arithmetic on a nil value
I know it might be a simple error for a coder, but i know nothing about code so it might aswel be in german!
Can you point me in the correct direction to get this thing going?
You are nearly there. You need to enter the actual amount of minutes you want the light to stay on for between 1and 5 not "z"
Try that.
Re: ByronSX PIR
Posted: Monday 10 February 2014 23:38
by simonrg
@Stokie21, as @Markk says, it looks like the only thing wrong is that rather than z you need to replace this to specify the time - PIR1g5Birds rather than PIR1gzBirds, as 5 will mean the lights will not turn off for at least 5 minutes. I would also try with PIRag5Birds first so that you can see its working without worrying about it only working at certain times, and then when that works change the name to PIR1g5Birds. Hope that now works ok. Simon
Re: ByronSX PIR
Posted: Tuesday 11 February 2014 18:19
by Stokie21
Agh i see, missed the last jump!!
working just fine now on the test, so have set it to custom time so if anyone goes in my bird shed after 10pm the house lights up
Thanks for all the help
Re: ByronSX PIR
Posted: Wednesday 19 February 2014 16:24
by nigels0
Fantastic work. Extremely easy to add PIRs now.
Re: ByronSX PIR
Posted: Wednesday 26 February 2014 8:13
by commodore white
simonrg wrote:I don't think it is appropriate for Domoticz to make a special case for the ByronSX PIRs
I sort of agree. However, if you register the device as of type "motion sensor" it's not unreasonable to expect domoticz to do "motions sensor" things - this includes turning the darn thing off if an off signal isn't received in a period you define. As it is, my new house is going to have 8 motion detection that all show "on". Not very wife friendly.