Have Fun !!
Code: Select all
--[[
script to create a CSV file with following layout.
now,inside,target,outside,lounge,greenhouse,heating [header row]
22:10,21.2,7.0,12.7,21.3,11.8,0 [oldest data: 24 hours minus 10 mins. ago]
22:20,21.1,7.0,12.7,21.3,11.8,0 [next data: 10 mins. later]
...
22:00,21.1,7.0,9.4,22.0,8.0,0 [latest data]
History:
20200108: Start coding
20200110: Start testing on life situation (thanks to MikeF)
20200122: First public release
]]--
return
{
on =
{
timer =
{
-- already quite busy at round minutes so shift a couple of minutes. Choose the one you prefer by uncommenting the line
'at *:03', 'at *:13', 'at *:23', 'at *:33', 'at *:43', 'at *:53',
-- 'every 10 minutes',
},
},
data =
{
rows =
{
history = true, maxItems = 300, maxHours = 48, -- max is whatever is reached first (what will be written to csv is a subset of this)
}
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
marker = 'createCSV',
},
execute = function(dz)
local dataTable = {}
-----------------------------------------------------------------------------------------------------------
-- Enter header line of your csv text file (or nil if you don't need one )
-- Enter devicenames / methods in the required order. This will determine what attribute-values will be written to the csvFile
-- Enter location/name of your csv text file
-- Enter time-span to be written to csv file
-----------------------------------------------------------------------------------------------------------
local header = 'now,inside,target,outside,lounge,greenhouse,heating'
dataTable[1] = dz.time.rawTime:sub(1, 4) .. '0' -- consistent times even if script is not triggered precisely every x0 minutes
dataTable[2] = dz.utils.round(dz.devices('inside').temperature, 1)
dataTable[3] = dz.utils.round(dz.devices('target').temperature, 1)
dataTable[4] = dz.utils.round(dz.devices('outside').temperature, 1)
dataTable[5] = dz.utils.round(dz.devices('lounge').temperature, 1)
dataTable[6] = dz.utils.round(dz.devices('greenhouse').temperature, 1)
dataTable[7] = dz.devices('heating').active and 5 or 0
local output = '/tmp/myFile.csv' -- location/name of your csv text file
local span = '23:59:00' -- time-span to be written to csv file
-----------------------------------------------------------------------------------------------------------
-- No changes required below these lines
-----------------------------------------------------------------------------------------------------------
local function makePersistentData()
local row = {}
for index, value in ipairs(dataTable) do
row[index] = value
end
return row
end
local function writeCSV()
local relevantRows = dz.data.rows.subsetSince(span)
local csvFile = assert(io.open(output, "w"))
if header then csvFile:write(header ..'\n') end
for index = #relevantRows, 1, -1 do
dz.log(table.concat(relevantRows[index].data, ','), dz.LOG_DEBUG)
csvFile:write(table.concat(relevantRows[index].data, ',') ..'\n')
end
csvFile:close()
dz.log((#relevantRows) .. ' data-rows written.', dz.LOG_DEBUG)
end
-- main
dz.data.rows.add(makePersistentData()) -- store in persistent historical data
writeCSV()
end
}