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.

Csv file as attachment
#14
(23.12.2022, 13:00)admin Wrote: Create a scheduled script that runs every hour. Modify ids table as needed: key is id in the CSV, value is the trend log name. Also fill in correct mail settings and to variable.

Code:
ids = {
  ['707057500075005871'] = 'Temperature',
  ['213123123123123123'] = 'Humidity',
}

require('trends')

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

buf = {}
now = os.time()

for id, tname in pairs(ids) do
  values = trends.fetch(tname, dates)
  count = #values
  step = 86400 / count

  for i, value in ipairs(values) do
    buf[ #buf + 1 ] = {
      now - (count - i) * step,
      id,
      value
    }
  end
end

table.sort(buf, function(a, b)
  return a[ 1 ] > b[ 1 ]
end)

for i, row in ipairs(buf) do
  row[ 1 ] = os.date('%Y-%m-%d %H:%M', row[ 1 ])
  buf[ i ] = table.concat(row, ',')
end

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

to = 'to@example.com'

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

subject = 'CSV'

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)



Hi,

I've adapted this code for use in a couple of my logic machines, and what I've found is that when I run this, the datetime column doesn't match up with the values when I compare them to the data in the trend logs. The data which is shown is from about 10hrs45 before that time. In the image below, the left part is data that I got after running the script at 10:30am BST. On the right is the data from the logic machine trend logs export that actually matches up with this data - so 21/08/2023 20:15 on the trend logs corresponds to 22/08/2023 07:00 in the emailed data.

   

I've done some digging and it looks like the issue arises in the code  
values = trends.fetch(tname, dates)
What could be the reason for this?
Reply


Messages In This Thread
Csv file as attachment - by PassivPluss - 23.12.2022, 00:54
RE: Csv file as attachment - by admin - 23.12.2022, 08:21
RE: Csv file as attachment - by PassivPluss - 23.12.2022, 10:39
RE: Csv file as attachment - by admin - 23.12.2022, 13:00
RE: Csv file as attachment - by PassivPluss - 26.12.2022, 05:19
RE: Csv file as attachment - by mreds5 - 22.08.2023, 14:41
RE: Csv file as attachment - by admin - 27.12.2022, 07:35
RE: Csv file as attachment - by PassivPluss - 07.01.2023, 15:27
RE: Csv file as attachment - by PassivPluss - 25.01.2023, 20:36
RE: Csv file as attachment - by admin - 26.01.2023, 10:33
RE: Csv file as attachment - by PassivPluss - 03.02.2023, 07:45
RE: Csv file as attachment - by admin - 03.02.2023, 09:11
RE: Csv file as attachment - by PassivPluss - 10.02.2023, 07:22
RE: Csv file as attachment - by admin - 10.02.2023, 08:26

Forum Jump: