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)
#1
Hi, is it possible to get the Max, Min and Average values ( all stored data) from each trend name so that they can be added to vis.
Trend Logs
1) Power DB1 : max 2/3/10 min 2/4/10 average 2/5/10
2) Power DB2 : max 3/3/10 min 3/4/10 average 3/5/10
3) Power DB3 : max 4/3/10 min 4/4/10 average 4/5/10
4) Lighting DB1 : max 5/3/10 min 5/4/10 average 5/5/10
Reply
#2
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)
Reply
#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


Forum Jump: