16.09.2024, 11:49
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