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.

Light timer script
#1
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.
Reply
#2
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
Reply
#3
It works flawlessly, I never thought it could be done like this. So much easier.. Thank you very much!
Reply


Forum Jump: