Logic Machine Forum
Dynamic timers for visualization - Printable Version

+- Logic Machine 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: Dynamic timers for visualization (/showthread.php?tid=262)



Dynamic timers for visualization - edgars - 04.04.2016

Task: There are 6 group addresses 1/1/1, 1/1/2...1/1/6
If any of these grp addresses are triggered to value 1 (RFID card is triggered), we launch respective timer for 10 min. (i.e. there are 5 independent timers). Once timer is over, we write 0 to this grp address. Also, make possible these timers dynamically appear on visualization.

Script:

Resident / sleep time = 1

1/1/1..6 - objects where we write 1 when RFID card is triggered (boolean) 
2/1/1..6 - objects where timer value is written starting 10 min from last reading (14 byte ASCII string) 

Code:
if not timers then
 timers = {
   -- input object: last update
   -- output object: formatted time value
   { '1/1/1', '2/1/1' },
   { '1/1/2', '2/1/2' },
   { '1/1/3', '2/1/3' },
   { '1/1/4', '2/1/4' },
   { '1/1/5', '2/1/5' },
   { '1/1/6', '2/1/6' },
 }

 function fmttime(sec)
   local min = math.floor(sec / 60)
   return string.format('%d:%02d', min, sec % 60)
 end

 maxtime = 10 * 60 -- 10 minutes in seconds
end

now = os.time()
for _, timer in ipairs(timers) do
 inaddr = timer[ 1 ]
 outaddr = timer[ 2 ]

 obj = grp.find(inaddr)
 if obj then
   delta = maxtime - now + obj.updatetime

   if delta > -3 then
     delta = math.max(0, delta)
     grp.update(outaddr, fmttime(delta), dt.string)
   end
 end
end