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.

Dynamic timers for visualization
#1
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
Reply


Forum Jump: