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.

Turn off light in 4 hours
#1
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
Reply
#2
os.sleep() function? 

it stops execution of current scrpit only, am i right?
Reply
#3
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.
Reply
#4
Thank you very much! I thought that os.sleep() would not be a good idea from a resource point of view.
Reply
#5
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?
Reply
#6
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)
Reply
#7
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
Reply
#8
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
Reply


Forum Jump: