LogicMachine Forum
Presence simulation / os.sleep() VS sleep() - 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: Presence simulation / os.sleep() VS sleep() (/showthread.php?tid=6465)



Presence simulation / os.sleep() VS sleep() - _BG_ - 11.06.2026

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)



RE: Presence simulation / os.sleep() VS sleep() - admin - 11.06.2026

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.