Eggybert wrote:Cleaned the P1 script a little and added some more comments, it still works the same:
Code: Select all
return {
-- 'active' controls if this entire script is considered or not
active = true, -- set to false to disable this script
logging = {
level = domoticz.LOG_INFO, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level
marker = "P1 toon"
},
on = {
timer = {
'every minute'
}
},
execute = function(domoticz)
--Replace all dev_2 for your number you can find @ http://192.168.178.29/hdrv_zwave?action=getDevices
--Replace IDX number GasIDX and ElectricityIDX
--make the User Valriables ToonIP is a string: 192.168.1.1
--make sure Dzvents is active under settings/other
local ToonIP = domoticz.variables('UV_ToonIP').value
local GasIDX = 17
local ElectricityIDX = 16
--Handle json, select your system
--local json = assert(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows
--local json = assert(loadfile "/home/maes/domoticz/scripts/lua/JSON.lua")() -- For Linux
local json = assert(loadfile "/volume1/@appstore/domoticz/var/scripts/lua/JSON.lua")() -- For Synology
json.strictTypes = true
--Ask toon for the latest data
local handle2 = assert(io.popen(string.format('curl http://%s/hdrv_zwave?action=getDevices.json', ToonIP)))
local ZwaveInfo = handle2:read('*all')
handle2:close()
--Found this by luck, no idea why it works :P but it removes the . between the numbers
ZwaveInfo = string.gsub(ZwaveInfo, "dev_2.", "dev_2")
ZwaveInfo = string.gsub(ZwaveInfo, "dev_2:", "dev_2\":")
--decode the json string to lua table
local jsonZwaveInfo = json:decode(ZwaveInfo)
--if the table is empty something went wrong
if jsonZwaveInfo == nil then
domoticz.log('<font color="green">No data from toon', domoticz.LOG_INFO)
return
end
--the following data is interesting for us
--dev_2.1:CurrentGasQuantity
--Elec high
--dev_2.3:CurrentElectricityFlow
--dev_2.3:CurrentElectricityQuantity
--Elec low
--dev_2.5:CurrentElectricityQuantity
--dev_2.5:CurrentElectricityFlow
--Elec produced high
--dev_2.4:CurrentElectricityQuantity
--dev_2.4:CurrentElectricityFlow
--Elec produced low
--dev_2.6:CurrentElectricityQuantity
--dev_2.6:CurrentElectricityFlow
local CurrentGasQuantity = tonumber(jsonZwaveInfo["dev_21"]["CurrentGasQuantity"])
--print(CurrentGasQuantity .. " CurrentGasQuantity")
local CurrentElectricityQuantityHoog = tonumber(jsonZwaveInfo["dev_23"]["CurrentElectricityQuantity"])
--print(CurrentElectricityQuantityHoog .. "kWh CurrentElectricityQuantityHoog")
local CurrentElectricityFlowHoog = tonumber(jsonZwaveInfo["dev_23"]["CurrentElectricityFlow"])
--print(CurrentElectricityFlowHoog .. "W CurrentElectricityFlowHoog")
local CurrentElectricityQuantityLaag = tonumber(jsonZwaveInfo["dev_25"]["CurrentElectricityQuantity"])
--print(CurrentElectricityQuantityLaag .. "kWh CurrentElectricityQuantityLaag")
local CurrentElectricityFlowLaag = tonumber(jsonZwaveInfo["dev_25"]["CurrentElectricityFlow"])
--print(CurrentElectricityFlowLaag .. "W CurrentElectricityFlowLaag")
--not interested when the current power is made
local totalPower = CurrentElectricityFlowHoog + CurrentElectricityFlowLaag
--print(totalPower .. "W totalPower")
local CurrentElectricityDeliveredLaag = tonumber(jsonZwaveInfo["dev_24"]["CurrentElectricityQuantity"])
--print(CurrentElectricityDeliveredLaag .. "kWh CurrentElectricityDeliveredLaag")
local CurrentElectricityDeliveredFlowLaag = tonumber(jsonZwaveInfo["dev_24"]["CurrentElectricityFlow"])
--print(CurrentElectricityDeliveredFlowLaag .. "W CurrentElectricityFlowLaag")
local CurrentElectricityDeliveredHoog = tonumber(jsonZwaveInfo["dev_26"]["CurrentElectricityQuantity"])
--print(CurrentElectricityDeliveredHoog .. "kWh CurrentElectricityDeliveredLaag")
local CurrentElectricityDeliveredFlowHoog = tonumber(jsonZwaveInfo["dev_26"]["CurrentElectricityFlow"])
--print(CurrentElectricityDeliveredFlowHoog .. "W CurrentElectricityFlowLaag")
local totalDeliveredPower = CurrentElectricityDeliveredFlowHoog + CurrentElectricityDeliveredFlowLaag
--print(totalDeliveredPower .. "W totalPower")
--USAGE1= energy usage meter tariff 1
--USAGE2= energy usage meter tariff 2
--RETURN1= energy return meter tariff 1
--RETURN2= energy return meter tariff 2
--CONS= actual usage power (Watt)
--PROD= actual return power (Watt)
--updateP1(usage1, usage2, return1, return2, cons, prod): Function. Updates the device. Supports command options.
domoticz.log('<font color="green">P1 from toon: ' .. CurrentElectricityQuantityLaag/1000 .. 'kwh USAGE1 | ' ..CurrentElectricityQuantityHoog/1000 .. 'kwh USAGE2 | ' .. CurrentElectricityDeliveredLaag/1000 .. 'kwh RETURN1 | ' .. CurrentElectricityDeliveredHoog/1000 .. 'kwh RETURN2 | ' .. totalPower .. 'w consumed | '.. totalDeliveredPower .. 'w produced', domoticz.LOG_INFO)
domoticz.devices(ElectricityIDX).updateP1(CurrentElectricityQuantityLaag, CurrentElectricityQuantityHoog, CurrentElectricityDeliveredLaag, CurrentElectricityDeliveredHoog, totalPower, totalDeliveredPower)
--update gas
domoticz.log('<font color="green">Gas from toon: ' .. CurrentGasQuantity/1000 .. 'm3', domoticz.LOG_INFO)
domoticz.devices(GasIDX).updateGas(CurrentGasQuantity)
end
}
And here is the code for non smart meters for toon, thanks Hypermobile for testing for me (sorry for the counter resets

)
*15-02-2018 fixed a bug in the code
*16*02*2018 fixed gas bug but the electricity is still wrong
Code: Select all
return {
-- 'active' controls if this entire script is considered or not
active = true, -- set to false to disable this script
logging = {
level = domoticz.LOG_INFO, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level
marker = "Power toon"
},
on = {
timer = {
'every minute'
}
},
data = {
lastKwhMemory = { history = true, maxItems = 1 },
lastGasMemory = { history = true, maxItems = 1 },
BackupGas = { history = true, maxItems = 1 }
},
execute = function(domoticz)
--Replace all dev_3 for your number you can find @ http://***.***.***.***/hdrv_zwave?action=getDevices
--Replace IDX number to your IDX numers. Gas and electic instance/counter
--make the User Valriables ToonIP is a string: 192.168.1.1
--make sure Dzvents is active under settings/other
--first time this script starts it will take 3 cycles(3min) to fill all the values
local ToonIP = domoticz.variables('UV_ToonIP').value
local GasIDX = 371
local ElectricityIDX = 372
--Handle json, select your system
--local json = assert(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows
--local json = assert(loadfile "/home/maes/domoticz/scripts/lua/JSON.lua")() -- For Linux
local json = assert(loadfile "/volume1/@appstore/domoticz/var/scripts/lua/JSON.lua")() -- For Synology
json.strictTypes = true
--Ask toon for the latest data
local handle = assert(io.popen(string.format('curl http://%s/hdrv_zwave?action=getDevices.json', ToonIP)))
local ZwaveInfo = handle:read('*all')
handle:close()
--Found this by luck, no idea why it works :P but it removes the . between the numbers
ZwaveInfo = string.gsub(ZwaveInfo, "dev_3.", "dev_3")
ZwaveInfo = string.gsub(ZwaveInfo, "dev_3:", "dev_3\":")
--decode the json string to a lua table
local jsonZwaveInfo = json:decode(ZwaveInfo)
--if the lua table is emty stop the script
if jsonZwaveInfo == nil then
domoticz.log('<font color="green">No data from Toon', domoticz.LOG_INFO)
return
end
--first time this script runs the lastMemory values need to be filled
if(domoticz.data.lastKwhMemory.size == 0 or domoticz.data.lastGasMemory.size == 0) then
domoticz.log('<font color="green">Last memory empty', domoticz.LOG_INFO)
domoticz.data.lastKwhMemory.add(0)
domoticz.data.lastGasMemory.add(0)
return
end
--On the first write of the day to the gas counter the counter will show 0 (bug in domoticz) use the backup vlaue instead
if(domoticz.data.BackupGas.size == 0) then
domoticz.data.BackupGas.add(0)
end
--the following data is interesting for us
--dev_2.1:CurrentGasQuantity
--Elec
--dev_2.2:CurrentElectricityQuantity
--dev_2.2:CurrentElectricityFlow
local CurrentGasQuantity = tonumber(jsonZwaveInfo["dev_31"]["CurrentGasQuantity"])
--print(CurrentGasQuantity .. " CurrentGasQuantity")
local CurrentElectricityQuantity = tonumber(jsonZwaveInfo["dev_32"]["CurrentElectricityQuantity"])
--print(CurrentElectricityQuantity .. "kWh CurrentElectricityQuantity")
local CurrentElectricityFlow = tonumber(jsonZwaveInfo["dev_32"]["CurrentElectricityFlow"])
--print(CurrentElectricityFlow .. "W CurrentElectricityFlow")
local TempElectricity = 0
--if the value from toon is higher then the memory from last loop and the memory from last loop is not 0
if(CurrentElectricityQuantity >= domoticz.data.lastKwhMemory.getLatest().data and domoticz.data.lastKwhMemory.getLatest().data ~= 0) then
--Calcukate new value 201 = 200(user variable) + (101(new value) - 100(memory last loop))
--TempElectricity = KwhMemory + (CurrentElectricityQuantity - domoticz.data.lastKwhMemory.getLatest().data)
TempElectricity = domoticz.devices(ElectricityIDX).WhTotal + (CurrentElectricityQuantity - domoticz.data.lastKwhMemory.getLatest().data)
--Electric usage
--WhActual: Number. Current Watt usage.
--updateEnergy(energy): Function. In Watt. Supports command options.
domoticz.log('<font color="green">Power from toon: ' .. TempElectricity/1000 .. 'kwh USAGE | ' .. CurrentElectricityFlow .. 'w consumed | ', domoticz.LOG_INFO)
domoticz.devices(ElectricityIDX).updateElectricity(CurrentElectricityFlow,TempElectricity)
--save data for next loop
domoticz.data.lastKwhMemory.add(CurrentElectricityQuantity)
else
--lastKwhMemory is emtpy so no idea how much usage there was. set for next loop. Or for some reason the memory had a higher value.
domoticz.data.lastKwhMemory.add(CurrentElectricityQuantity)
end
--Gas
local TempGas = 0
--check if the counter is higher then lastGasMemory and the lastGasMemory is not 0
if(CurrentGasQuantity >= domoticz.data.lastGasMemory.getLatest().data and domoticz.data.lastGasMemory.getLatest().data ~= 0) then
--only at 0:00 the value will be 0 (bug in domoticz) if 0 use the backup
if(domoticz.devices(GasIDX).counter ~= 0) then
--Calculate new value 201 = 200 + (101 - 100)
TempGas = (domoticz.devices(GasIDX).counter*1000) + (CurrentGasQuantity - domoticz.data.lastGasMemory.getLatest().data)
domoticz.data.BackupGas.add(TempGas)
else
TempGas = domoticz.data.BackupGas.getLatest().data + (CurrentGasQuantity - domoticz.data.lastGasMemory.getLatest().data)
domoticz.data.BackupGas.add(TempGas)
end
--counter: Number. Value in m3
--updateGas(usage): Function. Usage in dm3 (liters). Supports command options.
domoticz.log('<font color="green">Gas from toon: ' .. TempGas/1000 .. 'm3', domoticz.LOG_INFO)
domoticz.devices(GasIDX).updateGas(TempGas)
--for next loop
domoticz.data.lastGasMemory.add(CurrentGasQuantity)
else
--lastKwehMemory is emtpy so no idea how much usage there was. set for next loop
domoticz.data.lastGasMemory.add(CurrentGasQuantity)
end
end
}
Hi Eggybert
There is no data showing up in domoticz at my side. Can you help me out here?
I get the following information in the domoticz log:
2018-03-07 16:30:00.270 dzVents: Info: P1 toon: ------ Start internal script: P1 toon:, trigger: every minute
2018-03-07 16:30:00.282 dzVents: Info: P1 toon:
No data from toon
2018-03-07 16:30:00.282 dzVents: Info: P1 toon: ------ Finished P1 toon
the url to from my toon returns the following information:
{
"dev_settings_device":{"uuid":"e1487b54-7dc2-4c93-a45a-22d88965da53", "name":"settings_device", "internalAddress":"settings_device", "type":"settings_device", "supportsCrc":"0", "location":"(null)"},
"dev_2":{"uuid":"4547cb41-7c14-438e-ab01-ac925db85af5", "name":"HAE_METER_v2", "internalAddress":"2", "type":"HAE_METER_v2", "supportsCrc":"1", "ccList":"22 3c 3d 3e 56 60 70 72 7a 86 8b 73", "supportedCC":"22 3c 3d 3e 56 60 70 72 7a 86 8b 73", "IsConnected":"1", "DeviceName":"", "HealthValue":"10", "location":"(null)"},
"dev_2.1":{"uuid":"186117b7-22bc-4582-aafb-46761b8070a8", "name":"HAE_METER_v2_1", "internalAddress":"2.1", "type":"gas", "supportsCrc":"0", "CurrentGasFlow":"90.00", "CurrentGasQuantity":"3088259.00", "location":"(null)"},
"dev_2.2":{"uuid":"778999db-cf9e-47bd-91dd-8aa53bc1a0e2", "name":"HAE_METER_v2_2", "internalAddress":"2.2", "type":"elec", "supportsCrc":"0", "CurrentElectricityFlow":"NaN", "CurrentElectricityQuantity":"NaN", "location":"(null)"},
"dev_2.3":{"uuid":"6f1f31ba-9650-40db-b7c9-729de830afe1", "name":"HAE_METER_v2_3", "internalAddress":"2.3", "type":"elec_delivered_nt", "supportsCrc":"0", "CurrentElectricityFlow":"388.00", "CurrentElectricityQuantity":"14258455.00", "location":"(null)"},
"dev_2.4":{"uuid":"c3e6e7dc-7997-4fdd-8a47-7029071e1a78", "name":"HAE_METER_v2_4", "internalAddress":"2.4", "type":"elec_received_nt", "supportsCrc":"0", "CurrentElectricityFlow":"0.00", "CurrentElectricityQuantity":"3773.00", "location":"(null)"},
"dev_2.5":{"uuid":"9f92dc88-93b9-475c-b630-e6104cba9412", "name":"HAE_METER_v2_5", "internalAddress":"2.5", "type":"elec_delivered_lt", "supportsCrc":"0", "CurrentElectricityFlow":"0.00", "CurrentElectricityQuantity":"17635602.00", "location":"(null)"},
"dev_2.6":{"uuid":"06ce1a22-b71e-4737-a7d1-f555fb444b4f", "name":"HAE_METER_v2_6", "internalAddress":"2.6", "type":"elec_received_lt", "supportsCrc":"0", "CurrentElectricityFlow":"0.00", "CurrentElectricityQuantity":"807.00", "location":"(null)"}
}