30.04.2021, 05:25
This should produce what you need. Are you sure that you don't need the time to be included?
Code:
tagname = 'log'
logtime = os.time() - 60 * 60
query = [[
SELECT ol.*, o.name, o.datatype
FROM objectlog ol
JOIN objects o ON ol.address=o.id
JOIN objecttags t ON o.id=t.object
WHERE t.tag=? AND logtime>=?
ORDER BY id DESC
]]
buffer = { 'date;name;value' }
items = db:getall(query, tagname, logtime)
for _, item in ipairs(items) do
id = tonumber(item.address) or 0
logdate = os.date('%Y-%m-%d', math.floor(item.logtime))
etype = item.eventtype
if (etype == 'write' or etype == 'response') and item.datatype then
value = grp.decodevalue(item.datahex, item.datatype)
if type(value) == 'number' then
value = tostring(value):gsub('%.', ',')
end
else
value = ''
end
buffer[ #buffer + 1 ] = string.format('%s;%s;%s',
logdate, item.name or '', tostring(value))
end
csv = table.concat(buffer, '\n')