This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

Get last 20 minutes average value of object

I would like to make a script to get the last 20 minutes average value of the solar brightness, I don't know if it is possible and how to do it.
 thinks it's possbile by getting the database datas but i don't know

If someone have the solution it will be helpfull Smile 

Log object you want the average from and use this script

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 =' .. .. ' 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

  if count > 0 then
    result = math.floor(result / count + 0.5)
Just make sure not to log too many objects as oldest logs are deleted periodically.
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 ?
I updated the script to work with 2 byte float, You have to write value to the object after you start to log it.

Forum Jump: