Excellent news.
so are there any quick tips on using Lua to recall the last X data points ?
Yes. There’s two possible routes:
[ol][li]if you have direct file system access from where you’re running your Lua to the whisper database, then you can use a single function call to recover data,[/li]
[li]if you have IP access to the machine running the DataGraph daemon, you can use HTTP to retrieve it in CSV or JSON formats.[/li][/ol]
If you’re running the basic configuration with DataYours installed on your openLuup machine writing to a local file system then you can do both or either of the above.
[hr]
Using the Whisper database directly:
You just need to know where you told DataYours to put the Whisper database. It’s whatever value the LOCAL_DATA_DIR variable holds, for me it’s “whisper/”.
This little bit of code:
local whisper = require "L_DataWhisper"
local name = "OutdoorTemp.w"
local time = os.time() - 60*60
local tv = whisper.fetch ("whisper/"..name..".wsp", time)
print(pretty (tv))
…will return the last hour’s worth of data from the channel “OutdoorTemp.w”. The print statement shows you the returned data structure:
{
ipairs = function: 0xa0fd18,
times = {1458119700,1458123300,300},
values = {7.5,nil,7.7,nil,7.6,nil,7.7,nil,8.2,nil,8.4,n = 13}
}
which contains:
[ul][li]ipairs - an iterator to walk you through the data points[/li]
[li]times - a three element array with the start, finish, and sample rate times (in seconds)[/li]
[li]values - an array, quite possibly containing some nil values, and a table entry “n” giving the actual number of samples[/li][/ul]
Whilst you can delve directly into the values array and do some math to recover the corresponding sample times, the ipairs iterator will do that for you, returning an index number, the sample value, and the sample time. You use it like this:
for i,v,t in tv:ipairs() do
print (i,t,v)
end
which would print out the result:
1 1458119700 7.5
2 1458120000 nil
3 1458120300 7.7
4 1458120600 nil
5 1458120900 7.6
6 1458121200 nil
7 1458121500 7.7
8 1458121800 nil
9 1458122100 8.2
10 1458122400 nil
11 1458122700 8.4
12 1458123000 nil
13 1458123300 nil
This is probably the easiest way to go. If you want more readability on specifying the times to [tt]whisper.fetch()[/tt] then use [tt]os.time{year=2016, month=3, day=16, hour=10}[/tt] or something like that.
[hr]
Using an HTTP request to retrieve data:
A request of this form:
http://openLuupIP:3480/data_request?id=lr_render&target=OutdoorTemp.w&from=-1h&format=json
will retrieve the latest data in JSON format:
[{
"target": "OutdoorTemp.w"
, "datapoints": [
[7.5, 1458119700],
[null, 1458120000],
[7.7, 1458120300],
[null, 1458120600],
[7.6, 1458120900],
[null, 1458121200],
[7.7, 1458121500],
[null, 1458121800],
[8.2, 1458122100],
[null, 1458122400],
[8.4, 1458122700],
[null, 1458123000]
]
}]
…which is easily decoded with [tt]json.decode()[/tt], or you can get it in (a rather bizarre) CSV format:
http://openLuupIP:3480/data_request?id=lr_render&target=OutdoorTemp.w&from=-1h&format=csv
like this:
OutdoorTemp.w,2016-03-16 09:50:00,nil
OutdoorTemp.w,2016-03-16 09:55:00,8.2
OutdoorTemp.w,2016-03-16 10:00:00,nil
OutdoorTemp.w,2016-03-16 10:05:00,8.4
OutdoorTemp.w,2016-03-16 10:10:00,nil
OutdoorTemp.w,2016-03-16 10:15:00,8.5
OutdoorTemp.w,2016-03-16 10:20:00,nil
OutdoorTemp.w,2016-03-16 10:25:00,8.3
OutdoorTemp.w,2016-03-16 10:30:00,nil
OutdoorTemp.w,2016-03-16 10:35:00,8.2
OutdoorTemp.w,2016-03-16 10:40:00,nil
OutdoorTemp.w,2016-03-16 10:45:00,8.4
OutdoorTemp.w,2016-03-16 10:50:00,nil
If you leave off the [tt]&format[/tt] parameter, or give it the value of “svg” then you will retrieve an HTML file which, when displayed in a browser will give you a plot of the data. You could issue this request via a [tt]luup.inet.wget “http://…”[/tt] call.
This should help you to get started. Ask again if you need further help.