Trend logs

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, required
  • dates - Lua table with two items - start and end, each item must contain year, month, day keys, time values (hours, minutes and seconds) are ignored, required
  • resolution - default trend resolution is used if not specified, set to 86400 to retrieve daily data
  • timestamps - (only for fetch) when set to true each returned data entry becomes a Lua table with timestamp and value 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 settings
  • fetchone 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