Because of the new way the internal updateDevice function is used in domoticz, a change has to be made in dzVents scripts using incremental counters.
I already posted an update for this functionality in this topic
Moderator: leecollings
Because of the new way the internal updateDevice function is used in domoticz, a change has to be made in dzVents scripts using incremental counters.
There are quite a number of variations of this script flying around on this forum even with the same version number. So to ensure that a solution for this will work for your script, please share the script that you use now. Also helpful if you tell the device names / -types / -subtypes of the devices to receive the Total usage and Total delivery.
Code: Select all
-- dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
local fetchIntervalMins = 5 -- (Integer) Minutes frequence of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.8'
return {
on = {
timer = { 'every ' .. fetchIntervalMins .. ' Minutes' }
},
logging = {
-- level = domoticz.LOG_DEBUG, -- Uncomment this line to override the dzVents global logging setting
marker = 'SME '.. ScriptVersion
},
execute = function(dz, item)
-- The following need updated for your environment get the 'Idx' or 'Name' of the Device tab.
local P1data = 211 -- Electra, P1 Smart Meter device
local idxu1 = 214 -- Meter Usage low, Virtual device, counter incremental
local idxu2 = 215 -- Meter Usage High, Virtual device, counter incremental
local idxr1 = 216 -- Meter Return Low, Virtual device, counter incremental
local idxr2 = 217 -- Meter Return High, Virtual device, counter incremental
local idxcons = 218 -- Meter Actual Usage, Virtual device, counter incremental
local idxprod = 219 -- Meter Actual Production, Virtual device, counter incremental
-- Get values from device P1Data of the Smart Meter
local SMdata = dz.devices(P1data)
local function updateCounter(idx,value)
dz.devices(idx).updateCounter(value)
dz.log("Set " .. dz.devices(idx).name .. " to: ",dz.LOG_DEBUG)
end
-- Update the device and Debug meassages with the accessory values from table SMdata
updateCounter(idxu1,SMdata.usage1)
updateCounter(idxu2,SMdata.usage2)
updateCounter(idxr1,SMdata.return1)
updateCounter(idxr2,SMdata.return2)
updateCounter(idxcons,SMdata.usage)
updateCounter(idxprod,SMdata.usageDelivered)
end
}
You can copy / paste text in your post. Select it and press the </> button at the top of the edit window. That will place the text / code in a code window.
You will have to add two virtual (incremental counter) devices like the ones you already added and replace the xxx in below script with the idx or 'names' of these new devices and copy / paste this script to replace your current script.dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
Code: Select all
-- dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
local fetchIntervalMins = 5 -- (Integer) Minutes frequence of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.9'
return {
on = {
timer = { 'every ' .. fetchIntervalMins .. ' Minutes' }
},
logging = {
level = domoticz.LOG_DEBUG, -- Uncomment this line to override the dzVents global logging setting
marker = 'SME '.. ScriptVersion
},
execute = function(dz, item)
-- The following need updated for your environment get the Idx or 'Name' of the Device tab.
local P1data = 211 -- Electra, P1 Smart Meter device
local idxu1 = 214 -- Meter Usage low, Virtual device, counter incremental
local idxu2 = 215 -- Meter Usage High, Virtual device, counter incremental
local idxr1 = 216 -- Meter Return Low, Virtual device, counter incremental
local idxr2 = 217 -- Meter Return High, Virtual device, counter incremental
local idxcons = 218 -- Meter Actual Usage, Virtual device, counter incremental
local idxprod = 219 -- Meter Actual Production, Virtual device, counter incremental
local idxuTotal = xxx -- Meter Usage low, Virtual device, counter incremental
local idxrTotal = xxx -- Meter returnlow, Virtual device, counter incremental
-- Get values from device P1Data of the Smart Meter
local SMdata = dz.devices(P1data)
local function updateCounter(idx,value)
dz.devices(idx).updateCounter(value)
dz.log("Set " .. dz.devices(idx).name .. " to: ",dz.LOG_DEBUG)
end
-- Update the device and Debug meassages with the accessory values from table SMdata
updateCounter(idxu1,SMdata.usage1)
updateCounter(idxu2,SMdata.usage2)
updateCounter(idxr1,SMdata.return1)
updateCounter(idxr2,SMdata.return2)
updateCounter(idxcons,SMdata.usage)
updateCounter(idxprod,SMdata.usageDelivered)
updateCounter(idxuTotal,( SMdata.usage1 + SMdata.usage2 ))
updateCounter(idxrTotal,( SMdata.return1 + SMdata.return2 ))
end
}
daily total can be viewed in the log page. Or do you mean something else ?
Counters can be updated with positive and negative values but if the total becomes negative you will see strange big positive numbers because of the used number type in the underlying code.
Understand.
Do you already dump the domoticz smartmeter data to the CSV file or do you need help with that?
Sorry but I miss your point here. I don't think this will show your net consumption.
Code: Select all
Dumps daily data as csv file
requires sqlite3
install command on linux: sudo apt install sqlite3
install command on openwrt: opkg install sqlite3-cli
install command on synology: sudo /opt/bin/opkg install sqlite3-cli
History:
20191231 first public release
20200102 Add sudo chmod to make csvfile readable for all
]]--
return
{
on =
{
timer =
{
"at 17:25",
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "sqliteDump"
},
execute = function(dz)
-- ======================= enter you settings below this line ==================
local path = "/home/pi/domoticz/" -- full qualified path to your domoticz directory
local database = "domoticz.db" -- database filename + extension
local sqlite = "/usr/bin/sqlite3" -- location of your sqlite3 tool -- which sqlite3
local allTemperatures = "131" -- comma separated list of temperature devices to export
local allUsages = "44" -- comma separated list
local allSolar = "48" -- comma separated list
local csvFile = "/home/pi/domoticz2excel.csv"
-- ======================= No modification needed below this line ==================
local copy = "safeCopy.db"
local baseCommand = "sudo " .. sqlite .. " -header -csv " .. copy
local closure = " >> " .. csvFile
local collects = {}
collects.meters = 'select a.id as meter, a.name as naam, b.Value as value, b.counter as counter from devicestatus a , meter_calendar b where a.id = b.devicerowid and a.id in (' .. allUsages .. ',' .. allSolar ..');'
collects.temperatures = 'select a.id as temperature, a.name as naam, b.temp_min as min, b.temp_max as max, b.temp_avg as avg, b.date as date from devicestatus a , temperature_calendar b where a.id = b.devicerowid and a.id in (' .. allTemperatures .. ') order by a.id;'
local function osCommand(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local function sqliteDump()
local result, rc
if dz.utils.fileExists(path .. database) then
if dz.utils.fileExists(sqlite) then
result, rc = osCommand('cp ' .. path .. database .. ' ' .. copy)
if rc ~= 0 then return rc, result end
result, rc = osCommand('rm -f ' .. csvFile)
if rc ~= 0 then return rc, result end
for type, sql in pairs (collects) do
result, rc = osCommand(baseCommand .. ' "' .. sql .. '" ' .. closure)
if rc ~= 0 then return rc, result end
end
result, rc = osCommand('rm -f ' .. copy)
if rc ~= 0 then return rc, result end
result, rc = osCommand('sudo chmod 777 ' .. csvFile)
if rc ~= 0 then return rc, result end
else
return -1,"sqlite3 not installed / not found"
end
else
return -1,"wrong path to database"
end
return 0, commandOutput
end
-- main program
local rc, result = sqliteDump()
if rc ~= 0 then dz.log(result,dz.LOG_ERROR) end
end
}
Only when you do this at 23:50 latest and only if you are 100% sure you have a positive result.
Code: Select all
--[[
Dumps daily data as csv file
requires sqlite3
install command on linux: sudo apt install sqlite3
install command on openwrt: opkg install sqlite3-cli
install command on synology: sudo /opt/bin/opkg install sqlite3-cli
History:
20191231 first public release
20200102 Add sudo chmod to make csvfile readable for all
20200608 Add multimeters (for smartmeter)
]]--
return
{
on =
{
timer =
{
"at 17:25",
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "sqliteDump"
},
execute = function(dz)
-- ======================= enter you settings below this line ==================
local path = "/home/pi/domoticz/" -- full qualified path to your domoticz directory
local database = "domoticz.db" -- database filename + extension
local sqlite = "/usr/bin/sqlite3" -- location of your sqlite3 tool -- which sqlite3
local allTemperatures = "131" -- comma separated list of temperature devices to export
local allUsages = "44" -- comma separated list
local allSolar = "48" -- comma separated list
local allMultiMeters = "918"
local csvFile = "/home/pi/domoticz2excel.csv"
-- ======================= No modification needed below this line ==================
local copy = "safeCopy.db"
local baseCommand = "sudo " .. sqlite .. " -header -csv " .. copy
local closure = " >> " .. csvFile
local collects = {}
collects.meters = 'select a.id as meter, a.name as naam, b.Value as value, b.counter as counter, from devicestatus a , meter_calendar b where a.id = b.devicerowid and a.id in (' .. allUsages .. ',' .. allSolar ..');'
collects.temperatures = 'select a.id as temperature, a.name as naam, b.temp_min as min, b.temp_max as max, b.temp_avg as avg, b.date as date from devicestatus a , temperature_calendar b where a.id = b.devicerowid and a.id in (' .. allTemperatures .. ') order by a.id;'
collects.multiMeters = 'select a.id as smartMeter, a.name as naam, b.counter1 as consumedLow, b.counter2 as producedLow, b.counter3 as consumedHigh, b.counter2 as producedHigh, b.date as date from devicestatus a , multimeter_calendar b where a.id = b.devicerowid and a.id in (' .. allMultiMeters .. ') order by b.date desc;'
local function osCommand(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local function sqliteDump()
local result, rc
if dz.utils.fileExists(path .. database) then
if dz.utils.fileExists(sqlite) then
result, rc = osCommand('cp ' .. path .. database .. ' ' .. copy)
if rc ~= 0 then return rc, result end
result, rc = osCommand('rm -f ' .. csvFile)
if rc ~= 0 then return rc, result end
for type, sql in pairs (collects) do
result, rc = osCommand(baseCommand .. ' "' .. sql .. '" ' .. closure)
if rc ~= 0 then return rc, result end
end
result, rc = osCommand('rm -f ' .. copy)
if rc ~= 0 then return rc, result end
result, rc = osCommand('sudo chmod 777 ' .. csvFile)
if rc ~= 0 then return rc, result end
else
return -1,"sqlite3 not installed / not found"
end
else
return -1,"wrong path to database"
end
return 0, commandOutput
end
-- main program
local rc, result = sqliteDump()
if rc ~= 0 then dz.log(result,dz.LOG_ERROR) end
end
}
Code: Select all
2020-06-14 11:29:08.634 Error: dzVents: Error: (3.0.9) error loading module 'Slimme meter ophalen' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Slimme meter ophalen.lua':
2020-06-14 11:29:08.634 ...ripts/dzVents/generated_scripts/Slimme meter ophalen.lua:1: syntax error near 'daily'
Users browsing this forum: Amazon [Bot] and 1 guest