Logic Machine Forum
Light timer script - 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: Light timer script (/showthread.php?tid=917)



Light timer script - Jayce - 26.07.2017

Hello everybody!

I have a light and a PIR that detects movement and I'm trying to create a script that turns on the light after PIR detects movement, waits 120 seconds (without using os.sleep, it does extensive load on logic machine) and then turns off the light. The catch is that I need those 120 seconds to reset as soon as PIR detects another movement while the light is ON and the 120 seconds are already running.

TL;DR: Light needs to be ON while there's movement, without it turning off after 120 seconds and without using os.sleep. Could anybody please help me?

This is what i came up with, it works but the timer doesn't reset at all. After 120 seconds light turns off and then turns on again when there's movement.

PIR object (movement detection):


Quote:value = event.getvalue()

switch = grp.getvalue('1/1/20')

light = grp.getvalue('1/1/6')



if (switch == false and  light == false) then

  if (value == true) then

    grp.write('1/1/6', true)

    grp.write('1/1/10', 120)

  end

end


Countdown object:


Quote:value = event.getvalue()

switch = grp.getvalue('1/1/20')



while (value >= 1) do

  if (switch == true) then

    break

  end

  value = value - 1

  os.sleep(1)

  if (value == 0) then

    grp.write('1/1/6', false)

    break

  end

end

Thank you for any tips.


RE: Light timer script - admin - 26.07.2017

os.sleep does not produce any CPU load. The problem is that each event runs separately and you can end with many copies running at the same time.

One of possible solutions:

Put the latest PIR trigger timer into storage (event script, add a check if PIR is ON):
Code:
storage.set('pir_timer', os.time())

Check timer expiration (resident script, tune sleep time as needed)
Code:
delta = os.time() - storage.get('pir_timer', 0)
if delta >= 120 then
  -- do some action here
end



RE: Light timer script - Jayce - 26.07.2017

It works flawlessly, I never thought it could be done like this. So much easier.. Thank you very much!