Page 1 of 1

Smart PIR Script Lua

Posted: Sunday 22 November 2015 11:53
by frizby66
Have tried some of the scripts here and can get the PIR light one to work with some success:-

https://www.domoticz.com/wiki/Smart_Lua_Scripts

My problem is that there is a script to switch the light on at certain times of the day (which works) but doesn't switch it off.

There is another that will switch on and off regardless of the time of day (which works) but what I want is a script that will turn on at certain times of the day and then turn off after x minutes of no PIR activity.

Can anyone help?

Edit - The log says the light is turned off, but domoticz switches panel says it is not. Light is lightwaverf with feedback.

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 12:30
by jvdz
I would help when you show the script that is not working correctly so we can see what might be going wrong.

Jos

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 15:44
by frizby66
Sorry, I put a link to the wiki page that had the script.

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 == "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
The script above switched on the light, but never switches it off (although the log says it does). The txt description for the script says that "The device script is purely concerned with turning the light on and so does not need to worry about the parameter which determines when to turn the light off. For "PIRxyzStudy", after the "PIR" in the name: x specifies what times of day to turn the light on so replace x with a for all day, d for daytime, n for nighttime and 1 for a custom time, any other letter will stop the light being turned on at all, while this may seem silly, in fact it is useful if you just want to stop room lights being left on unnecessarily. y specifies whether it is a single device or group of devices, with r being a single Domoticz switch and g being a Domoticz Group."

However why have a variable for how long the light has to stay on if it never acts upon it?

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 16:06
by SweetPants
Can you try: commandArray[c] = 'On AFTER 5"

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 16:29
by frizby66
Will try. However the issue is it never switches off, not a problem switching on.

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 17:53
by SweetPants
frizby66 wrote:Will try. However the issue is it never switches off, not a problem switching on.
Sorry, i misread your post.

In your script i do not see a commandArray[c] = 'Off' command, so that the light stays On is what you can expect

Re: Smart PIR Script Lua

Posted: Sunday 22 November 2015 20:51
by jvdz
The script you posted is only switching lights On and there is a second script to switch them Off, when I understand the WiKi correctly.

Jos

Re: Smart PIR Script Lua

Posted: Wednesday 25 November 2015 17:33
by frizby66
Hi Jos, yes you are 100% right. Needed both scripts copied over.

Got it working a treat, but trying to do something different. I only want the lights to switch on between sunset and 22:00 (have dogs and don't want them turning the light on when we are sleeping). Any thoughts on how to edit the line:-

Code: Select all

if opertime == "n" then
      if timeofday['Nighttime'] then
         return true
Fraser

Re: Smart PIR Script Lua

Posted: Wednesday 25 November 2015 20:01
by simonrg
frizby66 wrote:Hi Jos, yes you are 100% right. Needed both scripts copied over.

Got it working a treat, but trying to do something different. I only want the lights to switch on between sunset and 22:00 (have dogs and don't want them turning the light on when we are sleeping). Any thoughts on how to edit the line:-

Code: Select all

if opertime == "n" then
      if timeofday['Nighttime'] then
         return true
Fraser
As a hack you could do:

Code: Select all

time = os.date("*t")
if opertime == "n" then
      if timeofday['Nighttime'] and time.hour < 22 then
         return true
So it will only be true when it is night, i.e. after sunset and before 10pm.

Re: Smart PIR Script Lua

Posted: Sunday 06 December 2015 17:27
by frizby66
Got a more pressing issue with PIR and light control.

I am using HE-108 switch (Home Easy / Byron) 433 switch. Works a treat in Domoticz but these dimmers have a feature which makes them cycle through their dimmer levels if you send a second "On" command (this feature is used to set the dimmer level buy pressing "On" on Home Easy remote to start the cycle then press "On" again to stop at the desired level).

Problem I have is that every time the PIR detects movement, the script sends the "On" command and so the light starts to cycle up and down!

Any ideas of a work around? Rather than the script sending the "On" command every time the PIR detects movement, could the script check the state of the light in Domoticz and only send the "On" when it detects that the light is "Off"?

Re: Smart PIR Script Lua

Posted: Sunday 06 December 2015 20:37
by simonrg
frizby66 wrote:Got a more pressing issue with PIR and light control.

I am using HE-108 switch (Home Easy / Byron) 433 switch. Works a treat in Domoticz but these dimmers have a feature which makes them cycle through their dimmer levels if you send a second "On" command (this feature is used to set the dimmer level buy pressing "On" on Home Easy remote to start the cycle then press "On" again to stop at the desired level).

Problem I have is that every time the PIR detects movement, the script sends the "On" command and so the light starts to cycle up and down!

Any ideas of a work around? Rather than the script sending the "On" command every time the PIR detects movement, could the script check the state of the light in Domoticz and only send the "On" when it detects that the light is "Off"?
The script is designed to be general, so the workaround may have some catches I suspect, for example I am not sure whether groups respond with whether they are on or off, having said that the workaround should be easy enough, basically just check the status.

The code currently looks like this in script_device_pirs.lua:

Code: Select all

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
Add in an extra if statement to check for current state:

Code: Select all

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
      -- Only switch light / group on if it is currently off
      if otherdevices[c] == 'Off' then
          commandArray[c] = 'On'
          tmess = c..' On - time 0'
          print(tmess)
      end
   end
end
I think the Home Easy device have no direct feedback, so one catch is that if the light doesn't turn on first time, then it will never turn on.

Re: Smart PIR Script Lua

Posted: Monday 07 December 2015 8:46
by frizby66
Thanks Simon, will give it a try tonight and let you know.

BTW, found a reasonable supplier on ebay for the HE stuff. Will put a link on the main forum http://stores.ebay.co.uk/halx-home-automation?

Re: Smart PIR Script Lua

Posted: Monday 07 December 2015 22:01
by frizby66
Worked a treat Simon :-)

Re: Smart PIR Script Lua

Posted: Thursday 31 March 2016 18:14
by frizby66
Got another question about the script and trying to refine it.

Have some lights set to switch on when it is "Nightime", which works great, but wondered if there was a way of extending Nightime to be and hour before sunset and an hour after sunrise. There is a period of the day where is is dark but not technically nightime (hope that make sense)!