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.

Loop through variables
#2
Use arrays/tables to store data like this. Untested modified version of you code:
Code:
obj = db:getall('SELECT name FROM objects WHERE name LIKE "%STATUS_OCCUPIED%"')

data = {}
keys = { '02', '03_04', '05' }
for fid = 1, 5 do
  floor = {}
  for _, key in ipairs(keys) do
    floor[key] = {
      count = 0,
      occ = 0,
    }
  end
  data[fid] = floor
end

for _, name in ipairs(obj) do
  adr = grp.find(name.name)
  gaWeight = string.gsub(name.name, 'STATUS', 'WEIGHT')
  weight = grp.getvalue(gaWeight)
  value = toboolean(grp.getvalue(name.name))
  floor = tonumber(string.split(adr.address, '/')[2])
  agg = string.split(string.split(adr.name, '360.')[2], ' ')[1]
  
  --Oppretter adresse for vekting dersom den ikke finnes og setter vekttallet til 1.
  if grp.find(gaWeight) == nil then
    address = grp.create({
        datatype = dt.uint16,
        virtual = true,
        name = gaWeight,
        comment = '',
      })
    grp.write(gaWeight, 1)
    weight = 1
  end

  if data[floor] and data[floor][agg] then
    data[floor][agg].count = data[floor][agg].count + 1
    if value then
      data[floor][agg].occ = data[floor][agg].occ + weight
    end
  end
end

log(data)
Reply


Messages In This Thread
Loop through variables - by Trond Hoyem - 11.02.2021, 14:06
RE: Loop through variables - by admin - 11.02.2021, 14:28
RE: Loop through variables - by Trond Hoyem - 11.02.2021, 14:43
RE: Loop through variables - by Trond Hoyem - 12.02.2021, 13:53

Forum Jump: