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.

Trend Logs (Max, Min Average)
#3
(09.05.2022, 08:11)admin Wrote: This example will calculate min/max/average for the past year data. Keep in mind that this is rather CPU and memory intensive calculation so it's not recommended to run it too often.
Code:
require('trends')

trends.NaN = 0 / 0

dates = {}
dates['start'] = os.date('*t')
dates['start'].year = dates['start'].year - 1
dates['end'] = os.date('*t')

values = trends.fetch('TREND_NAME_HERE', dates)

min, max, sum, cnt = math.huge, -math.huge, 0, 0

for _, value in ipairs(values) do
  if value == value then
    min = math.min(min, value)
    max = math.max(max, value)
    sum = sum + value
    cnt = cnt + 1
  end
end

avg = sum / math.max(1, cnt)

log(min, max, avg)
Thank you once again.
Reply


Messages In This Thread
Trend Logs (Max, Min Average) - by sjfp - 06.05.2022, 13:49
RE: Trend Logs (Max, Min Average) - by admin - 09.05.2022, 08:11
RE: Trend Logs (Max, Min Average) - by sjfp - 09.05.2022, 10:17

Forum Jump: