Logic Machine Forum
Get last 20 minutes average value of object - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Get last 20 minutes average value of object (/showthread.php?tid=2696)



Get last 20 minutes average value of object - idhe - 24.06.2020

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,


RE: Get last 20 minutes average value of object - Daniel - 24.06.2020

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.


RE: Get last 20 minutes average value of object - idhe - 24.06.2020

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 ?


RE: Get last 20 minutes average value of object - Daniel - 24.06.2020

I updated the script to work with 2 byte float, You have to write value to the object after you start to log it.


RE: Get last 20 minutes average value of object - Odd Egil Aasheim - 13.06.2022

(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


RE: Get last 20 minutes average value of object - Daniel - 14.06.2022

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



RE: Get last 20 minutes average value of object - Odd Egil Aasheim - 25.06.2022

(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