LogicMachine Forum
Turn off light in 4 hours - Printable Version

+- LogicMachine 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: Turn off light in 4 hours (/showthread.php?tid=43)



Turn off light in 4 hours - RSV4 - 18.07.2015

Hi all

Is there an easy way to switch off a light after a fix time without using a lot of resources on the LM?

Best regards


RE: Turn off light in 4 hours - Pawel - 18.07.2015

os.sleep() function? 

it stops execution of current scrpit only, am i right?


RE: Turn off light in 4 hours - admin - 20.07.2015

If you want to turn objects that were on for a certain time, you need a scheduled or a resident script with large sleep time.

Add "AutoOff" tag to all objects that should be turned off after a certain time. The script below will work for both binary on/off and scaled 1-byte objects. Note that each "on" telegram will reset the auto-off timer for each object.

Code:
-- maximum time that object can be on, in seconds ontime = 4 * 60 * 60 -- current timestamp, in seconds now = os.time() -- get all objects with AutoOff tag objects = grp.tag('AutoOff') -- check all objects for _, object in ipairs(objects) do  -- object is on  if toboolean(object.data) then    delta = now - object.updatetime    -- timer expired, turn off    if delta >= ontime then      object:write(0)    end  end end

Using os.sleep and event script is not a good idea - each event script runs in a separate process which will be active for 4 hours. This will certainly exhaust all LM memory sooner or later. The system will reboot automatically in this case.


RE: Turn off light in 4 hours - RSV4 - 20.07.2015

Thank you very much! I thought that os.sleep() would not be a good idea from a resource point of view.


RE: Turn off light in 4 hours - Pawel - 20.07.2015

There is some new(for me) commend (method?):  "object:write(0)". There is somewhere in manual comment about it? Maybe some other colon usage example?


RE: Turn off light in 4 hours - admin - 21.07.2015

From Lua docs about colon (:) usage on strings:

Quote:The string library provides all its functions inside the table string. It also sets a metatable for strings where the __index field points to the string table. Therefore, you can use the string functions in object-oriented style. For instance, string.byte(s, i) can be written as s:byte(i).


Short example for objects:
Code:
obj = grp.find('1/1/1') -- both lines do exactly the same thing obj:write(false) grp.write(obj.address, false, obj.datatype)



RE: Turn off light in 4 hours - edgars - 22.01.2016

Another timer example.

Task: If there is 0 sent to grp address AND it stays for 30 min at this value, switch on relay for 3 min. This is used in sauna installation to drain water after sauning is finished.

Event script, input is binary object:

Code:
value = event.getvalue() -- turn off timer and turn relay off if value then  storage.delete('timer')  grp.write('1/1/1', false) -- start timer if not already started else  timer = storage.get('timer')  if not timer then    storage.set('timer', 0)  end end

Scheduled script - run every minute:

Code:
-- check if timer is running timer = storage.get('timer') if timer then  timer = timer + 1  timer.set('timer', timer)  -- turn relay on after 30 minutes  if timer == 30 then    grp.write('1/1/1', true)  -- turn relay off after 33 minutes    elseif timer == 33 then    grp.write('1/1/1', false)  end end



RE: Turn off light in 4 hours - managementboy - 01.02.2016

Thanks! Here my version of the code. I use an object with a slider to allow us to set the auto-off value in minutes via the Visualization. I have it in a resident script with 60 seconds update. Works well.


Code:
ontime = grp.getvalue('9/4/0') * 60 -- autooff group in minutes * 60 seconds -- current timestamp, in seconds now = os.time() -- get all objects with AutoOff tag objects = grp.tag('AutoOff') -- check all objects for _, object in ipairs(objects) do  -- object is on  istrue = toboolean(object.data)  if istrue then    delta = now - object.updatetime    -- timer expired, turn off    if delta >= ontime then      object:write(0)    end  end end