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.

the average value of tagged objects
#15
No, this script cannot be used to calculate averages for a single group address because it does not store any historical info.

Since your sensor sends data periodically, you can map an event script to 13/2/9 which will store values and calculate average once 10 minutes pass.
Code:
-- storage key
key = 'temp_average'
-- time between average value send (in seconds)
maxtime = 10 * 60
-- resulting average value
result = '13/2/11'

time = os.time()

data = storage.get(key)
if not data then
  data = { values = {}, time = time }
end

value = event.getvalue()
table.insert(data.values, value)

delta = time - data.time
if delta >= maxtime or delta < 0 then
  storage.delete(key)

  avg = 0
  count = #data.values
  for _, value in ipairs(data.values) do
    avg = avg + value
  end

  grp.write(result, avg / count)
else
  storage.set(key, data)
end
Reply


Messages In This Thread
RE: the average value of tagged objects - by admin - 31.01.2017, 07:48

Forum Jump: