I try to translate a LUA script to a Dzvents script.
This script compute price of electricity consumption and compare the difference between prices of 2 contracts.
Electricity counter HP and HC are provided by a french Linky counter and read in a P1 smart meter
To compute difference in a step, old values are stored in a Domoticz USER VARIABLES
So, when I tried to read the Domoticz variables, I obtain a log message :
2019-11-28 15:47:00.276 Status: dzVents: Info: Compteur HPHC2: ------ Start external script: script_time_tarif_HPHC.lua:, trigger: every minute
2019-11-28 15:47:00.297 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Smart meter Linky: P1 smart meter energy device adapter
2019-11-28 15:47:00.298 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Cout EDF: Counter device adapter
2019-11-28 15:47:00.299 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Difference cout EDF HPHC-STD: Custom sensor device adapter
2019-11-28 15:47:00.299 Status: dzVents: Debug: Compteur HPHC2: ==>> new_HP ->8031867<- new_HC ->5982363<-
2019-11-28 15:47:00.299 Status: dzVents: Error (2.4.19): Compteur HPHC2: An error occured when calling event handler script_time_tarif_HPHC
2019-11-28 15:47:00.299 Status: dzVents: Error (2.4.19): Compteur HPHC2: ...oticz/scripts/dzVents/scripts/script_time_tarif_HPHC.lua:52: attempt to index global 'domoticz' (a nil value)
2019-11-28 15:47:00.299 Status: dzVents: Info: Compteur HPHC2: ------ Finished script_time_tarif_HPHC.lua
Code: Select all
return {
--
--~/domoticz/scripts/lua/script_device_tarif_HPHC.lua
--auteur : Philippe Marsault
--version : 1.0
--MAJ : 23/11/2019
--création : 23/11/2019
--Principe :
--Calcul du cout en euros pour le tarif Heures pleines / heures creuses
--Calcul du cout en euros pour le tarif standard
--Calcul de la différence de cout en euros HPHC - standart
--Data
--tarif consommation HP HC et standard - unité : €/Kwh
--tarif abonnement HP/HC et standard - unité : €/mois
--taxes - unité : €/Kwh
--taxes fixe - unité : €/mois
--References :
--https://www.domoticz.com/wiki/LUA_commands
--https://www.domoticz.com/wiki/DzVents:_next_generation_LUA_scripting#Command_options_.28delay.2C_duration.2C_event_triggering.29
--https://www.domoticz.com/forum/viewtopic.php?f=61&t=25031
--
on = { timer = { "every minute" }}, -- using every minute because Stroom and Gas are updated very frequently
logging = { level = domoticz.LOG_DEBUG, -- Remove -- on these two lines if you want debug logging
marker = "Compteur HPHC2" },
-- data = { old_HP = { initial = 0 },
-- old_HC = { initial = 0 } },
execute = function(dz)
-- Devices input
local new_HP = dz.devices("Smart meter Linky").usage1
local new_HC = dz.devices("Smart meter Linky").usage2
-- Devices output
local cout_HPHCdevice = dz.devices("Cout EDF")
local diff_prixdevice = dz.devices("Difference cout EDF HPHC-STD")
-- Price TTC from EDF (euros/Wh)
local tarif_cons_HP = 0.0001710
local tarif_cons_HC = 0.0001320
local tarif_cons_STD = 0.0001555
dz.log(" ==>> new_HP ->" .. tostring(new_HP) .. "<- new_HC ->" .. tostring(new_HC) .. "<-",dz.LOG_DEBUG)
-- Previous values stored in Domoticz user variables "Compteur_HP" and "Compteur_HC"
local old_HP = domoticz.variables("Compteur_HP").value -- Lecture valeur precedente compteur HP
local old_HC = domoticz.variables("Compteur_HC").value -- Lecture valeur precedente compteur HC
dz.log(" ==>> old_HP ->" .. tostring(old_HP) .. "<- old_HC ->" .. tostring(old_HC) .. "<-",dz.LOG_DEBUG)
-- Simplified computation (only comsuption - no taxes - no transfer)
--Actual contrat
local cout_HPHC = (new_HP - domoticz.data.old_HP)*tarif_cons_HP + (new_HC - domoticz.data.old_HC)*tarif_cons_HC
--Base contrat for comparison
local cout_STD = (new_HP - domoticz.data.old_HP + new_HC - domoticz.data.old_HC)*tarif_cons_STD
local diff_prix = cout_HPHC - cout_STD
-- Prepare text
local cout_HPHCText = tostring(dz.utils.round( cout_HPHC,5)):gsub("%.",",") -- rounded to 5 decimals and replace dot by comma
local diff_prixText = tostring(dz.utils.round( diff_prix,5)):gsub("%.",",") -- rounded to 5 decimals and replace dot by comma
local function updateTextDeviceCheckFirst(device,text)
if text ~= device.text then -- Update only needed when new text is different fom previous text
dz.log(device.name .. " ==>> previous text: " .. device.text .. " ; new text " .. text,dz.LOG_DEBUG)
device.updateCustomSensor(text)
end
end
local function incrementCompteur(device,text)
dz.log(device.name .. " ==>> previous text: " .. device.text .. " ; new text " .. text,dz.LOG_DEBUG)
device.incrementCounter(text)
end
-- call function to use with a dummy custom sensor
updateTextDeviceCheckFirst( diff_prixdevice, diff_prixText)
-- call function to use with a dummy managed Counter
incrementCompteur( cout_HPHCdevice, cout_HPHCText)
--store values for the next step
domoticz.variables("Compteur_HP").set(new_HP)
domoticz.variables("Compteur_HC").set(new_HC)
end
}
Philippe