16.09.2024, 10:36
Trends keep 1 hour of object values sampled every minute which you can use. This won't be a true average though because any changes between sampling times are lost.
This example will fetch values from the past 30 minutes and calculate the average value.
This example will fetch values from the past 30 minutes and calculate the average value.
Code:
require('trends')
date = os.date('*t')
time = os.time(date, true)
daterange = {
['start'] = time - 30 * 60,
['end'] = time,
}
data = trends.fetch('my trend', daterange, 60)
avg = 0
count = 0
for index, value in pairs(data) do
if value and value == value then
avg = avg + value
count = count + 1
end
end
if count > 0 then
avg = avg / count
end
log(avg)