Posts: 4646
Threads: 24
Joined: Aug 2017
Reputation:
207
Log object you want the average from and use this script
Code:
logaddress = '1/2/0'
pastTime = 1200 --in seconds
addr = grp.find(logaddress)
time= os.time() - pastTime
objects = db:getall('SELECT dataraw, logtime, datahex FROM objectlog WHERE address =' .. addr.id .. ' AND logtime >'.. time )
local result, count, value = 0, 0
for _, objvalue in ipairs(objects) do
value = busdatatype.decode(objvalue.datahex, addr.datatype)
result = result + value
count = count + 1
end
if count > 0 then
result = math.floor(result / count + 0.5)
log(result)
end
Just make sure not to log too many objects as oldest logs are deleted periodically.
------------------------------
Ctrl+F5
Posts: 21
Threads: 7
Joined: Apr 2019
Reputation:
0
Thank you Daniel !
It's look like it doesn't return the average...
My object's datatype is a 2 byte floating point.
any idea ?
Posts: 4646
Threads: 24
Joined: Aug 2017
Reputation:
207
I updated the script to work with 2 byte float, You have to write value to the object after you start to log it.
------------------------------
Ctrl+F5
Posts: 4646
Threads: 24
Joined: Aug 2017
Reputation:
207
I would just calculate how many seconds are in current hour and use it for pastTime like this
Code:
now = os.date('*t')
pastTime = now.min*60 +now.sec
------------------------------
Ctrl+F5