09.05.2022, 08:11
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)