Logic Machine Forum
Motion detector time limited deactivation - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Motion detector time limited deactivation (/showthread.php?tid=1780)



Motion detector time limited deactivation - Kai-Roger - 08.12.2018

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


RE: Motion detector time limited deactivation - Erwin van der Zwart - 08.12.2018

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


RE: Motion detector time limited deactivation - Kai-Roger - 09.12.2018

(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!


Motion detector time limited deactivation - Kai-Roger - 10.12.2018

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?


RE: Motion detector time limited deactivation - Erwin van der Zwart - 10.12.2018

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


Motion detector time limited deactivation - Kai-Roger - 10.12.2018

Thanks (: