Logic Machine Forum
Get last value of trend - 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: Get last value of trend (/showthread.php?tid=1216)



Get last value of trend - baggins - 06.02.2018

Hi,

How do I fetch the last value of a trend?

I have a trend from which I would like periodically  get the last value as shown in the trend graph.

TIA


RE: Get last value of trend - Daniel - 06.02.2018

Hi
Look here http://openrb.com/docs/trends-new.htm
BR


RE: Get last value of trend - baggins - 06.02.2018

(06.02.2018, 13:37)Daniel. Wrote: Hi
Look here  http://openrb.com/docs/trends-new.htm
BR

I did...
I have a test script but I cannot figure out how to get the last value...

Thanks


RE: Get last value of trend - Daniel - 06.02.2018

(06.02.2018, 13:42)baggins Wrote:
(06.02.2018, 13:37)Daniel. Wrote: Hi
Look here  http://openrb.com/docs/trends-new.htm
BR

I did...
I have a test script but I cannot figure out how to get the last value...

Thanks

In did, I couldn't get anything smaller than daily values.


RE: Get last value of trend - admin - 06.02.2018

You have to specify correct resolution in seconds, by default it is set to 1 day - 86400 seconds.


RE: Get last value of trend - baggins - 06.02.2018

(06.02.2018, 16:40)admin Wrote: You have to specify correct resolution in seconds, by default it is set to 1 day - 86400 seconds.

I'm still confused...
I took the example of the documentation and adapted where necessary:

Code:
require('trends')

dates = {
['start'] = { year = 2018, month = 2, day = 5},
['end'] = { year = 2018, month = 2, day = 6 },
}
--fetch current value
day = trends.fetch('CPU load', dates,300)
for i, v in ipairs(day) do log(i,v) end

This gives me 4 results. If I compare those results with the data in the trend, they don't match.

Result from script: 0,19, 0,12, 0,28, 0,34
Last 4 results in trend graph: 0,38, 0,33, 0.41, 0.33

I want to obtain the last result (0.33)...
Edit: if I run "day = trends.fetchone('CPU load', dates)" it returns 0.31 when at the same time in the graph is shows 0.1 as the latest value

BR


RE: Get last value of trend - buuuudzik - 06.02.2018

Here you have what you looking forWink

Code:
require('trends')

-- get data for today
function returnLastTrendValue(trendName)
 dates = {}

 dates['start'] = os.date('*t')
 dates['start'].day = dates['start'].day - 1
 dates['end'] = os.date('*t')
 dates['end'].day = dates['end'].day + 1

 trends.NaN = '*'
 -- fetch previous value
 daily = trends.fetch(trendName, dates, 1)

 i = #daily
 while i > 0 do
   if daily[i] ~= '*' then return daily[i] end
   i=i-1
 end
end

lastValue = returnLastTrendValue('Your trend name')
log(lastValue)

I've tested Trends API some time ago and I've found that to have some data from today you must type not today timestamp but tomorrow and also 1 day before. The last value is the last measured value before midnight e.g. for 5minute resolution 23:55. For taking your maximum resolution you can type instead 86400 only 1. And then you should define undefined value from RRD db and ylou should look from 00:00 to first data which will appearWink


RE: Get last value of trend - baggins - 06.02.2018

Haha, I didn't realize it was that easy!   Wink 

Thanks a bunch!

BR


RE: Get last value of trend - buuuudzik - 06.02.2018

Me tooWink