Logic Machine Forum
Trend Logs (Max, Min Average) - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Trend Logs (Max, Min Average) (/showthread.php?tid=4037)



Trend Logs (Max, Min Average) - sjfp - 06.05.2022

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


RE: Trend Logs (Max, Min Average) - admin - 09.05.2022

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)



RE: Trend Logs (Max, Min Average) - sjfp - 09.05.2022

(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.