Hi,
I removed most of my cusomizations from this version. I have not tested it but it should help you when you have a DSMR 4 meter.
– Modified version of dutchSmartMeter from Brutgrink
-- Service IDs we use
MY_SERVICE_ID = "urn:brutgrink-com:serviceId:dutchSmartMeter"
---------------------------------------------------------------------------------------------
-- Utility functions
---------------------------------------------------------------------------------------------
-- Log wrapper so we can quickly turn it off and on for debugging
function do_writeLog(sMessage)
– luup.log(sMessage)
end
– Do a optimized Get and Set function
local function luup_var_get(name, service, device)
return luup.variable_get(service or MY_SERVICE_ID, name, device or THIS_DEVICE)
end
– Only set variable when changed. This reduces Vera memory writes and logging.
local function luup_var_set (name, value, service, device)
service = service or MY_SERVICE_ID
device = device or THIS_DEVICE
local old = luup_var_get(name, service, device)
if tostring(value) ~= old then
luup.variable_set(service, name, value, device)
end
end
– Thanks to guessed for this snippet.
function findChild(parentDevice, label)
for k, v in pairs(luup.devices) do
if (v.device_num_parent == parentDevice and v.id == label) then
return k
end
end
-- Dump a copy of the Global Module list for debugging purposes.
for k, v in pairs(luup.devices) do
do_writeLog("Device Number: " .. k ..
" v.device_type: " .. tostring(v.device_type) ..
" v.device_num_parent: " .. tostring(v.device_num_parent) ..
" v.id: " .. tostring(v.id))
end
end
---------------------------------------------------------------------------------------------
-- Module startup
---------------------------------------------------------------------------------------------
function dutchSmartMeter_Startup(lul_device)
THIS_DEVICE = lul_device
do_writeLog("Starting dutchSmartMeter device: " .. tostring(lul_device))
-- get child devices
for k, v in pairs(luup.devices) do
if (v.device_num_parent == lul_device) then
do_writeLog( "Found child device, lets save! id " .. tostring(v.id) .. " device " .. tostring(v.device_type))
if (tostring(v.device_type) == POWER_SERVICE_ID) then
powerMeterCount = powerMeterCount + 1
powerMeterDevice[powerMeterCount] = v.id
end
end
end
-- Assure device variables exist
local gasMeter = luup_var_get("Gas")
local activeTariff = luup_var_get("ActiveTariff")
local meterType = luup_var_get("MeterType")
if ((gasMeter or "") == "") then
luup_var_set("Gas", 0)
end
if ((activeTariff or "") == "") then
luup_var_set("ActiveTariff", 0)
end
if ((meterType or "") == "") then
luup_var_set("MeterType", "Unknown")
end
-- Create devices needed if not exist
addPowerMeterDevice("ImportRate1")
addPowerMeterDevice("ImportRate2")
addPowerMeterDevice("ExportRate1")
addPowerMeterDevice("ExportRate2")
end
-- Add device is not yet found
function addPowerMeterDevice(meterID)
-- If we can't find a device add a new child meterID
if findChild(THIS_DEVICE, meterID) == nil then
local child_devices = luup.chdev.start(THIS_DEVICE);
-- First add all 'old' children to the tree
for teller = 1, powerMeterCount do
luup.chdev.append(THIS_DEVICE, child_devices, powerMeterDevice[teller], "Powermeter " .. powerMeterDevice[teller] ,
POWER_SERVICE_ID,"D_PowerMeter1.xml","","",false)
end
-- Now add the new device to the tree
do_writeLog("Tree buildup, now add " .. meterID)
luup.chdev.append(THIS_DEVICE, child_devices, meterID, "Powermeter " .. meterID ,
POWER_SERVICE_ID,"D_PowerMeter1.xml","","",false)
powerMeterCount = powerMeterCount + 1
powerMeterDevice[powerMeterCount] = meterID
-- Synch the new tree with the old three
do_writeLog("Start sync")
luup.chdev.sync(THIS_DEVICE, child_devices)
do_writeLog("End sync")
end
end
---------------------------------------------------------------------------------------------
-- Smart Meter Data row has been received via serial. Process
---------------------------------------------------------------------------------------------
function dutchSmartMeterIncoming(data)
local meterGridUsage = 0
if (string.len(data) > 0) then
-- Check if we have to process gas meter. Tested only with DMSR 4 meter!
if (string.sub(data, 1, 10) == "0-1:24.2.1") then
do_writeLog("Gas meter: [" .. string.match(string.match(data, "(%d+.%d+*m3)"), "%d+.%d+") .. "]")
luup_var_set("Gas", math.floor(tonumber(string.match(string.match(data, "(%d+.%d+*m3)"), "%d+.%d+"))))
-- Check if we have to process energy IMPORT meter(s)
elseif (string.sub(data, 1, 7) == "1-0:1.8") then
local meterValue = tonumber(string.match(string.sub(data,10), "%d+.%d+"))
local meterID = string.match(string.sub(data,9), "%d+")
do_writeLog(string.format("Power meter ImportRate%s: %f",meterID,meterValue))
luup_var_set("KWH", math.floor(meterValue), ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ImportRate" .. meterID))
-- Set meterstand afname value (T1 ot T2) value
luup_var_set("meterT"..meterID, math.floor(meterValue))
-- Check if we have to process energy EXPORT meter(s)
elseif (string.sub(data, 1, 7) == "1-0:2.8") then
local meterValue = tonumber(string.match(string.sub(data,10), "%d+.%d+"))
local meterID = string.match(string.sub(data,9), "%d+")
do_writeLog(string.format("Power meter ExportRate%s: %f",meterID,meterValue))
luup_var_set("KWH", math.floor(meterValue), ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ExportRate" .. meterID))
-- Set meterstand teruglever value (T1- ot T2-) value
luup_var_set("meterT"..meterID.."m", math.floor(meterValue))
-- Check current tariff
elseif (string.sub(data, 1, 11) == "0-0:96.14.0") then
do_writeLog("Currently active tariff: [" .. string.match(string.sub(data,12), "%d+") .. "]")
luup_var_set("ActiveTariff", tonumber(string.match(string.sub(data,12), "%d+")))
-- Check current watts import
elseif (string.sub(data, 1, 9) == "1-0:1.7.0") then
-- get active tariff variable
local activeTariff = luup_var_get("ActiveTariff")
do_writeLog("CurrentWattsImport: [" .. string.match(string.sub(data,10), "%d+.%d+") .. "]")
do_writeLog("CurrentActiveTariffImport: [" .. activeTariff .. "]")
-- Current Grid import value
if (tonumber(string.match(string.sub(data,10), "%d+.%d+"))*1000.0 > 0) then
luup_var_set("Watts", tonumber(string.match(string.sub(data,10), "%d+.%d+"))*1000.0)
end
-- import rate 2
luup_var_set("Watts", tonumber(string.match(string.sub(data,10), "%d+.%d+"))*1000.0, ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ImportRate" .. activeTariff))
-- import rate 1
luup_var_set("Watts", 0, ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ImportRate" .. (1+(activeTariff % 2))))
-- Check current watts export
elseif (string.sub(data, 1, 9) == "1-0:2.7.0") then
-- get active tariff variable
local activeTariff = luup_var_get("ActiveTariff")
do_writeLog("CurrentWattsExport: [" .. string.match(string.sub(data,10), "%d+.%d+") .. "]")
do_writeLog("CurrentActiveTariffExport: [" .. activeTariff .. "]")
-- Current Grid export value
if (tonumber(string.match(string.sub(data,10), "%d+.%d+"))*1000.0 > 0) then
luup_var_set("Watts", tonumber(string.match(string.sub(data,10), "%d+.%d+"))*-1000.0)
end
-- export rate 2
luup_var_set("Watts", tonumber(string.match(string.sub(data,10), "%d+.%d+"))*-1000.0, ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ExportRate" .. activeTariff))
-- export rate 1
luup_var_set("Watts", "-0", ENGERGY_SERVICE_ID, findChild(THIS_DEVICE,"ExportRate" .. (1+(activeTariff % 2))))
-- Check meter type
elseif (string.sub(data,1,1) == "/") then
do_writeLog("MeterType: [" .. string.sub(data,2) .. "]")
luup_var_set("MeterType", string.sub(data,2))
else
-- Do not log this unless really needed. New smart meters produce a lot of data.
– do_writeLog(string.format(“Meter data (not processed): %q”,data))
end
end
end
</functions>