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 that you accept these cookies being set.

Get last 20 minutes average value of object
#1
Hi,

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 

Thanks,
Reply
#2
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
Reply
#3
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 ?
Reply
#4
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
Reply
#5
(24.06.2020, 13:53)Daniel Wrote: I updated the script to work with 2 byte float, You have to write value to the object after you start to log it.

I've tried to make this script to calculate average within the current hour, but is unable to make it work.

Have tried to substitute  time= os.time() - pastTime  with  time= os.time() - os.time("%H") and a couple of other formats
but get the following error 
User script:5: bad argument #1 to 'time' (table expected, got string)
stack traceback:
[C]: in function 'time'
User script:5: in main chunk

Also tried to use a GA as a local reference for this where the value at is updated at every new hour.

How can it calculate values from say when time is 22:48, back towards 22:00
Reply
#6
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
Reply
#7
(14.06.2022, 07:43)Daniel Wrote: 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

That seems to be doing what I wanted, I only had to speed up polling rate to get more accurate values.
Thanks again Daniel  Smile
Reply


Forum Jump: