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
string, required - trend log namedates
table, required - must contain two keys -start
andend
, value for both keys must be either:number
- UTC timestamp (see examples below)table
containingyear
,month
andday
keys; time values (hours, minutes and seconds) are ignored
resolution
number - default trend resolution is used if not specified, set to 86400 to retrieve daily datatimestamps
boolean - (only forfetch
) when set totrue
each returned data entry becomes an array-liketable
where the first element istimestamp
and the second isvalue
Return values:
fetch
returns atable
with values for the given period ornil
on error. Number of values depends on period, resolution and data retention settingsfetchone
returns single value for the given period ornil
on error
Examples #
Basic example #
require('trends')
-- fetch data for the previous day
dates = {}
dates['start'] = os.date('*t')
dates['start'].day = dates['start'].day - 1
dates['end'] = os.date('*t')
-- fetch current value
day = trends.fetchone('Gas', dates)
-- fetch 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)
Fetch using UTC timestamps #
date = os.date('*t') -- current date as a table
time = os.time(date, true) -- convert UTC timestamp
-- fetch data for the past hour
datarange = {
['start'] = time - 3600, -- 1 hour in seconds
['end'] = time,
}
data = trends.fetch('trend name', datarange)
log(data)
Detecting invalid/missing data #
Data point values which contain invalid data or cannot be found are assigned to trends.NaN
.
The default value for trends.NaN
is 0, it can be set to 0 / 0 (NaN - not a number).
require('trends')
-- use "not a number" for invalid values
trends.NaN = 0 / 0
-- fetch 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