Page 1 of 1

Lua PIR problem - Switching off too early

Posted: Friday 12 May 2017 18:26
by Lorccan
Hi

I have a PIR PIRCeiling and a script script_device_PIRCeiling.lua that should be updating a user variable TIMERCeiling to the value of a var ceilingTimer each time it switches to 'On'. If the switch Ceiling is not already 'On', it should switch it on.

Code: Select all

commandArray={}
ceilingTimer=6
if (deviceChanged['PIRCeiling']=='On' then
	if (other devices['Ceiling']=='Off') then
		commandArray['Ceiling']='On'
	end
	commandArray['Variable:TIMERCeiling']=tostring(ceilingTimer)
end
return commandArray
A timer script script_time_Ceiling.lua should check the value of the user variable TIMERCeiling each time it fires and if it equals 1 should switch-off Ceiling. Otherwise, it should decrement TIMERCeiling by 1.

Code: Select all

commandArray={}
if (uservariables['TIMERCeiling']==1) then
	if (otherdevices['Ceiling']=='On') then
                commandArray['Ceiling']='Off'
        end
else
        commandArray['Variable:TIMERCeiling']=tostring(uservariables['TIMERCeiling']-1)
end
return commandArray
So, the theory is that each time there's a movement, TIMERCeiling is reset to 6 and if no movement is detected for 5-6 minutes Ceiling should be switched off.

The problem is that although the switch on happens correctly (but with a few seconds delay - any ideas about that would be welcome), even though there's significant movement that seems to be triggering the PIR, the timer is switching off the light before people leave the room. Any ideas please? (I'm hoping there's something really stupid that I am missing here, so please be brutal! :) )

Re: Lua PIR problem - Switching off too early

Posted: Saturday 13 May 2017 7:34
by lonebaggie
For whatever reason your time script is being called more than once a minute why not use

commandArray["Ceiling"]='On" For 6

in your main script and drop the the second script all together

Re: Lua PIR problem - Switching off too early

Posted: Saturday 13 May 2017 17:04
by Mooseknuckle
This is what I do with my kitchen light:
Script_device_whatever

Code: Select all

if devicechanged['Motion kitchen'] == 'On' and otherdevices['is dark'] == 'On' and otherdevices['kitchen lights'] == 'Off' then
      commandArray['kitchen lights']= 'On'
      print('Movement in kitchen, lights go on')
Script_time_whatever

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
time = os.date("*t")
commandArray = {}
if otherdevices['kitchen lights'] == 'On' then
 if timedifference(otherdevices_lastupdate['Motion kitchen']) > 300 then
     commandArray['Milight keuken']='Off'
     print('No motion for 5 minutes, kitchen lights go off')
 end
end
return commandArray
The lights now go out after 5 minutes and not when the pir stops detecting movement after 2 minutes (in my case). Maybe this will help :)

Re: Lua PIR problem - Switching off too early

Posted: Monday 22 May 2017 11:51
by Lorccan
Sorry for the delay in responding. I have reached the conclusion that the problem I am having is nothing to do with the scripts, but with the PIRs I have tried to detect continuous movement.

The first one, Philips Hue Motion detector, was too 'clever' and seemed to cache the last movement (last updated) data at the sensor instead of sending an update, so this could not be used in a script.

I then tried another from Friedland (DC44) which has its own problem: it goes to sleep for two minutes each time it detects movement and the sleep time is extended for another two minutes when further movement is detected (so far as I can tell without updating the date/time of the last movement), so it's pretty useless for my purposes.

May I ask which PIR you are using with your script please?

Re: Lua PIR problem - Switching off too early

Posted: Monday 22 May 2017 11:54
by Egregius
Don't they both remain 'On' during that time? At least a Fibaro does that...

Re: Lua PIR problem - Switching off too early

Posted: Monday 22 May 2017 19:10
by Lorccan
Well that's another strange thing: the Friedland one (which I am concentrating on) never seems to go 'On' so far as domoticz is concerned - it's just updating the 'Last Seen', but it doesn't do that when it's sleeping.

I am not keen to start using Fibaro as I don't have z-wave and don't really want to add it. Do you know of any other straightforward PIRs that simply report movement when they see it? (I have both RFXTRX433e & RFLink if that makes any difference.)

Re: Lua PIR problem - Switching off too early

Posted: Monday 22 May 2017 22:52
by freijn
use an ESP8826 Arduino with a Pir connected. THen you can decide everything yourself :-)