LogicMachine Forum
Using the average of a trend log value in a script - Printable Version

+- LogicMachine 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: Using the average of a trend log value in a script (/showthread.php?tid=5617)



Using the average of a trend log value in a script - dnavarro - 15.09.2024

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?


RE: Using the average of a trend log value in a script - Daniel - 16.09.2024

https://forum.logicmachine.net/showthread.php?tid=291&pid=3217#pid3217


RE: Using the average of a trend log value in a script - dnavarro - 16.09.2024

(16.09.2024, 07:29)Daniel Wrote: https://forum.logicmachine.net/showthread.php?tid=291&pid=3217#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!


RE: Using the average of a trend log value in a script - Daniel - 16.09.2024

You can use this:
https://kb.logicmachine.net/libraries/trends/


RE: Using the average of a trend log value in a script - admin - 16.09.2024

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)



RE: Using the average of a trend log value in a script - dnavarro - 16.09.2024

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