Logic Machine Forum
Trends_Log - 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: Trends_Log (/showthread.php?tid=771)



Trends_Log - Rodriguez - 03.05.2017

Hello, I would like to know how I can put a trend to a function, to see the data of this in a graph.
I was watching this document:
Http://openrb.com/docs/trends.htm
But I do not understand if this code should be added to the function or created separately. Also I get error with the library: genohm-scada.trends
Thanks...


RE: Trends_Log - buuuudzik - 04.05.2017

For 99% you are using Logic Machine with firmware after april 2016 so you should use this API:

http://openrb.com/docs/trends-new.htm


RE: Trends_Log - Rodriguez - 04.05.2017

Hi,
Sorry, that code I should add to the function that I set the trend?


RE: Trends_Log - buuuudzik - 04.05.2017

What do you want to do? Add new trend(1) or download the data(2) from existing trend and operation on this data?

If you want 1) add new trend you should use menu, this has nothing with scripting.

If you want 2) download data then you should do exactly what is in API. For example when you want download last year daily data of trend "Gas" you should paste in a script:
Code:
require('trends')

trend_name = "Gas"

-- get data for the past year
dates = {}
dates['start'] = os.date('*t')
dates['start'].year = dates['start'].year - 1
dates['end'] = os.date('*t')

-- fetch previous value
yearly = trends.fetch(trend_name, dates, 86400)

Then you can operate freely on such data(normal table). You can use it also in function. All is in the manual of Logic Machine and on:
http://openrb.com/docs/


RE: Trends_Log - Rodriguez - 05.05.2017

Hi,
Could you average the data obtained between certain dates? 

Thanks for your help. Big Grin


RE: Trends_Log - buuuudzik - 05.05.2017

For calculating average value for some period you should use trends.fetchone() function instead trends.fetch().

You can also look here:
http://forum.logicmachine.net/showthread.php?tid=719&highlight=trend+api


RE: Trends_Log - Rodriguez - 05.05.2017

Should I work in a resident function?

Another question is, in the graphic part, the data that is shown is the average of all the data that was handled in that time interval?
For example, I put a resolution trend of 5 minutes.


RE: Trends_Log - buuuudzik - 05.05.2017

It depends what you want do. What is your task?


RE: Trends_Log - Rodriguez - 07.05.2017

In the first instance I want to make a test of logic, so I generated a basic function that generates random numbers and that creates a trend. But I do not know if the data that shows me in the graph is an average of the interval, I also want to know the average between certain dates of these data.


RE: Trends_Log - buuuudzik - 07.05.2017

Following the API it is the average from selected period. You should only know that the minimum resolution is 1 day(from 00:00 to 23:59). If you want check what is the average from e.g. 8:00-16:00 you should use fetch function and calculate this.


RE: Trends_Log - Rodriguez - 08.05.2017

OK, what kind of function should I handle the average?
To be able to download this data should I add some library?
Thank you. Big Grin


RE: Trends_Log - buuuudzik - 08.05.2017

This is little example for calculating average value:
Code:
require('trends')

-- get data for the last day from 00:00 to 23:59
dates = {}
dates['start'] = os.date('*t')
dates['start'].day = dates['start'].day - 1
dates['end'] = os.date('*t')

-- fetch previous value
daily = trends.fetch('Wind', dates)

sum = 0
for k,v in ipairs(daily) do
 sum = sum + v
end

average = sum/(#daily)
log(average)