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.

Average power for the last clock hour
#8
I couldn't understand how to transform that to a script that could be useful, so I googled lua average value and found this in another thread:

-- storage key
key = 'temp_average'
-- time between average value send (in seconds)
maxtime = 60 * 60
-- resulting average value
result = '4/3/34'

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

Is there a way to make this start the count at the beginning of the clock hour?
Reply


Messages In This Thread
RE: Average power for the last clock hour - by Rune - 12.11.2022, 23:24

Forum Jump: