Trend log functions #
Load the library:
require('trends')
Library functions #
trends.fetch(name, dates, resolution, timestamps)
#
trends.fetchone(name, dates, resolution)
#
Fetch one or many values for the given period
Parameters:
name
- trend log name, requireddates
- Lua table with two items -start
andend
, each item must containyear
,month
,day
keys, time values (hours, minutes and seconds) are ignored, requiredresolution
- default trend resolution is used if not specified, set to 86400 to retrieve daily datatimestamps
- (only forfetch
) when set totrue
each returned data entry becomes a Luatable
withtimestamp
andvalue
fields
Return values:
fetch
returns Lua table with values for the given period or nil on error. Number of values depends on period, resolution and data retention settingsfetchone
returns single value for the given period or nil on error
Examples #
require('trends')
-- will fetch data between 2016.04.15 00:00 and 2016.04.16 00:00
dates = {
['start'] = { year = 2016, month = 4, day = 15 },
['end'] = { year = 2016, month = 4, day = 16 },
}
-- fetch current value
day = trends.fetchone('Gas', dates)
-- 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('Gas', dates, 86400)
trends.NaN
value is used for points which contain invalid values or cannot be found.
The default value is 0, but it can also be set to 0 / 0 (NaN - not a number).
require('trends')
-- use "not a number" for invalid values
trends.NaN = 0 / 0
-- get data for the past year
dates = {}
dates['start'] = os.date('*t')
dates['start'].year = dates['start'].year - 1
dates['end'] = os.date('*t')
value = trends.fetchone('Hot Water', dates)
-- NaN ~= NaN means value was not found
if value ~= value then
return
end