This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Motion detector time limited deactivation
#1
Hi

I have made a code that will deactivate a motion detector in a given time that can be set in the group objekt 3/6/9. After this time expires, the motion detector will be active, and recive a "motion not detected" command (a false writen to Forced_state 3/5/9, to reset motion status i the detector).

My problem is this: If i send a "true" to the Event adress for this script, then all works great. If i then send a "false" to the event adress, it does what i is supposed to do. But when i send a "True" once again, and do that before the os.sleep timer has run out of the preset time, then some strange things happends. It's like it's starting multiple os.sleep timers or something.
Is it possible to have the os.sleep "killed" when the event recives a "false"? Is os.sleep the right way to do this?


Code:
Lockdown_Event        = event.getvalue()
Lockdown        = grp.find    ('3/4/9')
Forced_State        = grp.find    ('3/5/9')
Forced_State_length    = grp.getvalue    ('3/6/9')

if Lockdown_Event == true
  then
     os.sleep(Forced_State_length)
     Lockdown       :write(false)
     Forced_State :write(false)
   else
     Forced_State :write(false)
end

BR
Kai-Roger
Reply
#2
Hi,

Try to use this when having a event based script with os.sleep:
Code:
-- Always start your script with this PID check to kill previous instance when needed
tpid = storage.get('PID:' .. _SCRIPTNAME)
if tpid == nil then
   pid = os.getpid()
   storage.set('PID:' .. _SCRIPTNAME, pid)
else
 pid = os.getpid()  
 storage.set('PID:' .. _SCRIPTNAME, pid)
 os.kill(tpid, signal.SIGKILL)
end

-- Put here your normal script with os.sleep()


-- End of script

-- Always end your script with removal of not needed storage data
storage.delete('PID:' .. _SCRIPTNAME)
BR,

Erwin
Reply
#3
(08.12.2018, 23:53)Erwin van der Zwart Wrote: Hi,

Try to use this when having a event based script with os.sleep:
Code:
-- Always start your script with this PID check to kill previous instance when needed
tpid = storage.get('PID:' .. _SCRIPTNAME)
if tpid == nil then
   pid = os.getpid()
   storage.set('PID:' .. _SCRIPTNAME, pid)
else
 pid = os.getpid()  
 storage.set('PID:' .. _SCRIPTNAME, pid)
 os.kill(tpid, signal.SIGKILL)
end

-- Put here your normal script with os.sleep()


-- End of script

-- Always end your script with removal of not needed storage data
storage.delete('PID:' .. _SCRIPTNAME)
BR,

Erwin

Thanks Smile I am not  all that in to scripting yet, but i'm learning. Is all i have to do, just to put my script inside this one, or do i have to change something in your script to adapt it to my system?

I just copy paste it like the description says, and it works brilliant (:
Thanks for quick reply!
Reply
#4
Hi.

Without fully understanding how your code works, i have a follow-up question. Can multiple codes run in parallell? It's no text/variable name dedicated to a temporary storage that have to be unique to to have two or more codes running simultaneously?
BR
Kai-Roger
Reply
#5
Hi,

The script uses the script name as variable in the storage, so if you use it in another script it will automatically have a unique storage name. So yes you can use it in multiple scripts in parallel without the need of making adjustments.

BR,

Erwin
Reply
#6
Thanks (:
BR
Kai-Roger
Reply


Forum Jump: