31.01.2017, 07:48
No, this script cannot be used to calculate averages for a single group address because it does not store any historical info.
Since your sensor sends data periodically, you can map an event script to 13/2/9 which will store values and calculate average once 10 minutes pass.
Since your sensor sends data periodically, you can map an event script to 13/2/9 which will store values and calculate average once 10 minutes pass.
Code:
-- storage key
key = 'temp_average'
-- time between average value send (in seconds)
maxtime = 10 * 60
-- resulting average value
result = '13/2/11'
time = os.time()
data = storage.get(key)
if not data then
data = { values = {}, time = time }
end
value = event.getvalue()
table.insert(data.values, value)
delta = time - data.time
if delta >= maxtime or delta < 0 then
storage.delete(key)
avg = 0
count = #data.values
for _, value in ipairs(data.values) do
avg = avg + value
end
grp.write(result, avg / count)
else
storage.set(key, data)
end