06.07.2022, 08:59
What do you mean that "the values doesn't make much sense"? Are they different from the data view in trends UI monthly mode?
This example exports daily data for multiple trends for the past month. You can add more trend name to the trendlist table as needed.
This example exports daily data for multiple trends for the past month. You can add more trend name to the trendlist table as needed.
Code:
require('trends')
trendlist = {
'Temperature',
'Humidity',
}
now = os.date('*t')
dates = {
['start'] = { year = now.year, month = now.month - 1, day = 1 },
['end'] = { year = now.year, month = now.month, day = 1 }
}
if dates.start.month == 0 then
dates.start.month = 12
dates.start.year = dates.start.year - 1
end
year, month = dates.start.year, dates.start.month
rows = {}
datas = {}
days = 0
row = { '""' }
for _, name in ipairs(trendlist) do
data = trends.fetch(name, dates, 86400) or {}
days = math.max(days, #data)
datas[ #datas + 1 ] = data
row[ #row + 1 ] = string.format('%q', name)
end
rows[ #rows + 1 ] = table.concat(row, ',')
for day = 1, days do
row = {}
row[ #row + 1 ] = string.format('%d.%02d.%02d', year, month, day)
for _, data in ipairs(datas) do
row[ #row + 1 ] = data[ day ] or ''
end
rows[ #rows + 1 ] = table.concat(row, ',')
end
csv = table.concat(rows, '\r\n')