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.

Using the average of a trend log value in a script
#6
Thanks again for pointing me on the right direction, here is my code in case it's useful for someone else:

Code:
require('trends')
-- get data for today
dates = {}
dates['start'] = os.date('*t')
dates['end'] = os.date('*t')
dates['end'].day = dates['end'].day + 1

-- fetch values
today = trends.fetch('LUMINOSIDAD', dates)

-- initialize an array to store the rows
stored_data = {}

-- check if any data was returned
if today and #today > 0 then
  for i, row in ipairs(today) do
    -- store each row in the array
    if row ~=0 then -- Only when it has value, 0 means no value (future) 1 is the minimal value for the sensor.
      table.insert(stored_data, row)
    end
  end
else
  log('No data found for today')
end

-- get the last 6 items (30 min, reported every 5 min) and average them
local luminosidad_30_min = 0.0
local start_index = math.max(1, #stored_data - 5) -- ensure we don't go below index 1
for i = start_index, #stored_data do
  luminosidad_30_min = luminosidad_30_min + stored_data[i]
end

luminosidad_30_min = luminosidad_30_min/6
Reply


Messages In This Thread
RE: Using the average of a trend log value in a script - by dnavarro - 16.09.2024, 11:49

Forum Jump: