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.

Trends to csv
#1
Hello,

I want to create a csv file for the last 30 days.
I have a script as below, but it creates not that many days. What has to be changed?

/Fredrik

Code:
require('trends')

dates = {}
dates['start'] = os.date('*t')
dates['start'].day = dates['start'].day - 1
dates['end'] = os.date('*t')

now = os.time()

function fetch(tname)
  local res = {}
  local values = trends.fetch(tname, dates)
  local count = #values
  local step = 86400 / count

  for i, value in ipairs(values) do
    table.insert(res, {
      now - (count - i) * step,
      value
    })
  end

  return res
end

buf = {
  fetch('LUFTTEMPERATUR W'),
  fetch('NEDERBÖRDSMÄNGD W'),
  fetch('VINDRIKTNING W'),
  fetch('NEDERBÖRD INTENSITET W'),
  fetch('GENOMSNITTLIG VINDHASTIGHET W'),
  fetch('RELATIV LUFTFUKTIGHET W'),
  fetch('MAX VINDHASTIGHET W'),
  fetch('ABSOLUT LUFTTRYCK W')
}


csv = { 'Id,Site Id,Site Authentication Key,Report Date / Time,Temperature,Precipitation accumulated,Wind direction,Precipitation intensity,Average wind, Humidity,Downwind,Air pressure at station' }

for i, row1 in ipairs(buf[ 1 ]) do
  date = os.date('%d/%m/%y %H:%M', row1[ 1 ])
  value1 = row1[ 2 ]

  row2 = buf[ 2 ][ i ] or {}
  value2 = row2[ 2 ] or 0
 
  row3 = buf[ 3 ][ i ] or {}
  value3 = row3[ 2 ] or 0
 
  row4 = buf[ 4 ][ i ] or {}
  value4 = row4[ 2 ] or 0
 
  row5 = buf[ 5 ][ i ] or {}
  value5 = row5[ 2 ] or 0
 
  row6 = buf[ 6 ][ i ] or {}
  value6 = row6[ 2 ] or 0
 
  row7 = buf[ 7 ][ i ] or {}
  value7 = row7[ 2 ] or 0
 
  row8 = buf[ 8 ][ i ] or {}
  value8 = row8[ 2 ] or 0




 
    csv[ #csv + 1 ] = string.format(' fb6ca12c-3106-ee11-913a-201642ba599e,20230608tey3apeggae67rj4rymrfqtn3c,Grimeljer2023!,%s,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f',
    date, value1, value2, value3, value4, value5, value6, value7, value8)
 

end

csv = table.concat(csv, '\n')
log(csv)

to = 'email@email.com'

settings = {
  -- "from" field, only e-mail must be specified here
  from = ' email@email.com ',
  -- smtp username
  user = ' email@email.com ',
  -- smtp password
  password = 'Password',
  -- smtp server
  server = 'smtp.gmail.com',
  -- smtp server port
  port = 465,
  -- enable ssl, required for gmail smtp
  secure = 'sslv23',
}

subject = 'CSV väder'

smtp = require('socket.smtp')
mime = require('mime')
ltn12 = require('ltn12')

function escape(v)
  return '<' .. tostring(v) .. '>'
end

to = escape(to)

msgt = {
  headers = {
    to = to,
    ['content-type'] = 'text/csv',
    ['content-disposition'] = 'attachment; filename="logs.csv"',
    ['content-transfer-encoding'] = 'BASE64',
    subject = subject,
  },
  body = ltn12.source.chain(
    ltn12.source.string(csv),
    ltn12.filter.chain(mime.encode('base64'), mime.wrap('base64'))
  )
}

settings.source = smtp.message(msgt)
settings.from = escape(settings.from)
settings.rcpt = { to }

res, err = smtp.send(settings)
log(res, err)
Reply


Messages In This Thread
Trends to csv - by jobb@nordstrandsel.se - 06.10.2023, 08:44
RE: Trends to csv - by admin - 06.10.2023, 08:48
RE: Trends to csv - by jobb@nordstrandsel.se - 06.10.2023, 09:12
RE: Trends to csv - by admin - 06.10.2023, 09:23
RE: Trends to csv - by jobb@nordstrandsel.se - 06.10.2023, 16:08
RE: Trends to csv - by admin - 09.10.2023, 06:42
RE: Trends to csv - by jobb@nordstrandsel.se - 09.10.2023, 11:41
RE: Trends to csv - by admin - 09.10.2023, 11:48
RE: Trends to csv - by jobb@nordstrandsel.se - 10.10.2023, 12:55
RE: Trends to csv - by admin - 10.10.2023, 12:56

Forum Jump: