16.09.2022, 10:09
Use this for a single group address. CSV is rather pointless in this case. This script simply shows dates and values in the message body.
Code:
id = buslib.encodega('0/0/1')
to = { 'to1@example.com', 'to2@example.com' }
subject = 'Alarm logs'
logtime = os.time() - 24 * 60 * 60 -- last 24 hours
buffer = {}
query = [[
SELECT o.datatype, ol.datahex, ol.logtime, ol.eventtype
FROM objectlog ol
JOIN objects o ON ol.address=o.id
WHERE o.id=? AND ol.logtime >= ?
ORDER BY ol.id DESC
]]
for _, row in ipairs(db:getall(query, id, logtime)) do
if row.datatype and row.eventtype == 'write' then
data = grp.decodevalue(row.datahex, row.datatype)
logdate = os.date('%Y.%m.%d %H:%M:%S', row.logtime)
buffer[ #buffer + 1 ] = logdate .. ' ' .. tostring(data)
end
end
message = table.concat(buffer, '\r\n')
mail(to, subject, message)