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
#1
Hello,

Thank you everyone for all the help I already got reading other posts :-) I'm new to LogicMachine.

I have a KNX system with a weather station which reads the luminosity outside the house, I want to use that value to determine if it's sunny or cloudy outside, but to avoid errors due to shadows or small clouds covering the sun for few minutes, I would like to use, instead of the point value, the average of the last 30 minutes. Is it possible to use the values stored from the trends to achieve that? otherwise, any other idea on how to get an accurate reading of the su/clouds to use for opening/closing the awnings?
Reply
#2
https://forum.logicmachine.net/showthrea...17#pid3217
------------------------------
Ctrl+F5
Reply
#3
(16.09.2024, 07:29)Daniel Wrote: https://forum.logicmachine.net/showthrea...17#pid3217

Thanks for your reply, I indeed saw that post, but I wanted to use the historical data that I already have stored in the trends log for that specific item, is that not possible?


Thanks!
Reply
#4
You can use this:
https://kb.logicmachine.net/libraries/trends/
------------------------------
Ctrl+F5
Reply
#5
Trends keep 1 hour of object values sampled every minute which you can use. This won't be a true average though because any changes between sampling times are lost.

This example will fetch values from the past 30 minutes and calculate the average value.
Code:
require('trends')

date = os.date('*t')
time = os.time(date, true)

daterange = {
  ['start'] = time - 30 * 60,
  ['end'] = time,
}

data = trends.fetch('my trend', daterange, 60)

avg = 0
count = 0

for index, value in pairs(data) do
  if value and value == value then
    avg = avg + value
    count = count + 1
  end
end

if count > 0 then
  avg = avg  / count
end

log(avg)
Reply
#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


Forum Jump: