This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Trend Log functions question
#1
Hi,

I have data from a Photovoltaic Solar PowerPlant energy production already in Trends.

I'm trying to make a script to send an email with a CSV with the production from each day per each row of the CSV at the end of each month.

I have it already sending a CSV with a row for each 30min directly from trends (30min resolution) with the trend_export function.

My question is about changing from 30min per row to each day per row.

I'm trying now directly with the trend log functions:

Code:
require('trends')

-- will fetch data between 2016.04.15 00:00 and 2016.04.16 00:00
dates = {
  ['start'] = { year = 2022, month = 6, day = 1 },
  ['end'] = { year = 2022, month = 7, day = 1 },
}

-- fetch current value
day = trends.fetch('PV Total Energy', dates)
csv = table.concat(day, '\r\n')


If I set the resolution like this, will it calculate the correct value for each day?


Code:
day = trends.fetch('PV Total Energy', dates, 86400)

EDIT: if used this way the values doesn't make much sense.



One more thing, in the visualization, if I export multiple trends to CSV I get each variable in each column. (see attachment)
Any way to do this via scripting in order to send via email ?

   

Thank you
Reply
#2
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.
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')
Reply
#3
Hi,

Thank you.

I mean for example for "Temp. 1" for day 8, seems to have a strange value when comparing the 2 CSVs

   

Regarding your code example, thank you very much.

I think I will also offer the original resolution from Trends in order to let the end customer choose.

Have you got one an example code like the one you posted before, for multiple trends with the timestamp for the original resolution from Trends, like this one bellow from visualization ?

   

Thank you very much !
Reply


Forum Jump: