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.

Presence simulation / os.sleep() VS sleep()
#1
Hi,
I'm building a presence simulation script. A resident script (60s interval) picks a random light from a list of 4, turns it on for a random time between 1–3 hours, then turns it off. Presence mode is controlled by a virtual on/off object.

Logic: Presence mode ON → random lamp ON → os.sleep(random 1-3h) → lamp OFF. 
I've tested os.sleep() and sleep(). Both work, script DOES NOT run again (60s interval ) while "sleeping". 

Questions:
1. What is the difference between sleep() and os.sleep()? Are there any practical differences in behaviour?
2. Will os.sleep() for 1–3 hours cause any load on the LM?
3. Is resident script the right approach for this, or is there a better way?

Thanks!

BG


PS. I know there is a dedicated app for presence simulation, but I have specific client requirements that need a custom implementation.


Code:
-------------------------------------------------- -- Presence simulation -------------------------------------------------- local lights = {   { addr = '40/3/1', name = 'lamp 1' },   { addr = '40/3/2', name = 'lamp 2' },   { addr = '40/3/3', name = 'lamp 3 '},   { addr = '40/3/4', name = 'lamp 4' }, } local active = grp.getvalue('40/3/10') -- presence mode ON/OFF if not active then return end -------------------------------------------------- math.randomseed(os.time()) local pick     = lights[ math.random(#lights) ] local duration = math.random(3600, 10800) log('Presence simulation -> ON  ' .. pick.name .. '  ' .. math.floor(duration / 60) .. ' min') grp.write(pick.addr, true) os.sleep(duration) grp.write(pick.addr, false) log('Presence simulation -> OFF ' .. pick.name)
Reply
#2
1. sleep() and os.sleep() are the same functions
2./3. It's fine if you don't have many scripts like this. LM can potentially run out of memory and reboot if there are too many scripts running in parallel.
Reply


Forum Jump: