User variables  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
MarsaultP
Posts: 38
Joined: Wednesday 27 November 2019 13:31
Target OS: Linux
Domoticz version: 2024.7
Location: France
Contact:

User variables

Post by MarsaultP »

Hello,
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
}

Thank you for your contributions ansd remarks

Philippe
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: User variables

Post by boum »

Replace domoticz.variables by dz.variables since dz is the first parameter of your function.

In the documentation wiki, this first parameter is usually also named domoticz, so the examples there use domoticz.anything. In you case, it is dz or you could replace all instances of dz to domoticz including the function parameter.

As a workaround, you could still put in local domoticz = dz as the fist line of your function (so you won't miss an occurrence) :D
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: User variables

Post by boum »

You've got also some domoticz.data in there, but your data table on top of the script is commented.
MarsaultP
Posts: 38
Joined: Wednesday 27 November 2019 13:31
Target OS: Linux
Domoticz version: 2024.7
Location: France
Contact:

Re: User variables

Post by MarsaultP »

Ok, thanks, now that work well except "managed counter"

I whish to use a counter to have cumulative cost during the day because the script compute cost cout_HPHC for each step of Domoticz loop.
So, I use dummy managed counter because I can obtain cumulative value and also choose label of axe ...

A idea about the error ?

The log is :
2019-11-28 18:21:00.542 Status: dzVents: Info: Compteur HPHC2: ------ Start external script: script_time_tarif_HPHC.lua:, trigger: every minute
2019-11-28 18:21:00.563 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Smart meter Linky: P1 smart meter energy device adapter
2019-11-28 18:21:00.564 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Cout EDF: Counter device adapter
2019-11-28 18:21:00.565 Status: dzVents: Debug: Compteur HPHC2: Processing device-adapter for Difference cout EDF HPHC-STD: Custom sensor device adapter
2019-11-28 18:21:00.566 Status: dzVents: Debug: Compteur HPHC2: ==>> new_HP ->8036503<- new_HC ->5982428<-
2019-11-28 18:21:00.566 Status: dzVents: Debug: Compteur HPHC2: ==>> old_HP ->8036475<- old_HC ->5982428<-
2019-11-28 18:21:00.566 Status: dzVents: Debug: Compteur HPHC2: ==>> cout_HPHCText ->0,00479
2019-11-28 18:21:00.566 Status: dzVents: Debug: Compteur HPHC2: ==>> diff_prixText ->0,00043
2019-11-28 18:21:00.566 Status: dzVents: Error (2.4.19): Compteur HPHC2: An error occured when calling event handler script_time_tarif_HPHC
2019-11-28 18:21:00.566 Status: dzVents: Error (2.4.19): Compteur HPHC2: ...oticz/scripts/dzVents/scripts/script_time_tarif_HPHC.lua:78: attempt to call field 'incrementCounter' (a nil value)
2019-11-28 18:21:00.567 Status: dzVents: Info: Compteur HPHC2: ------ Finished script_time_tarif_HPHC.lua


and the code updated is :

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 = dz.variables("Compteur_HP").value -- Lecture valeur precedente compteur HP
		local old_HC = dz.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 - old_HP)*tarif_cons_HP + (new_HC - old_HC)*tarif_cons_HC
		--Base contrat for comparison
		local cout_STD  = (new_HP - old_HP + new_HC - 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
		
		dz.log(" ==>> cout_HPHCText ->" .. tostring(cout_HPHCText),dz.LOG_DEBUG)
		dz.log(" ==>> diff_prixText ->" .. tostring(diff_prixText),dz.LOG_DEBUG)
        		
		local function incrementCompteur(device,text)
                --dz.log(device.name .. " ==>> previous text: " .. device.text .. " ; new text " .. text,dz.LOG_DEBUG)
                device.incrementCounter(text) 
        end
        
        -- update diff_prix dummy custom sensor
		diff_prixdevice.updateCustomSensor(diff_prix)
		-- call function to use with a dummy managed Counter
        --incrementCompteur( cout_HPHCdevice, cout_HPHCText) 
		cout_HPHCdevice.incrementCounter(cout_HPHC)

		--store values for the next step
		dz.variables("Compteur_HP").set(new_HP)
		dz.variables("Compteur_HC").set(new_HC)
        
    end
}

Philippe
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: User variables

Post by waaren »

MarsaultP wrote: Thursday 28 November 2019 18:27 A idea about the error ?
The incrementCounter method is not available in dzVents 2.4.19. It was introduced in 2.4.23
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MarsaultP
Posts: 38
Joined: Wednesday 27 November 2019 13:31
Target OS: Linux
Domoticz version: 2024.7
Location: France
Contact:

Re: User variables  [Solved]

Post by MarsaultP »

Ok, a good reason ! Thanks !
How I can update Dzvents in Domoticz ? By extracting recent version on Domoticz Github ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: User variables

Post by waaren »

MarsaultP wrote: Thursday 28 November 2019 19:38 Ok, a good reason ! Thanks !
How I can update Dzvents in Domoticz ? By extracting recent version on Domoticz Github ?
No dzVents is fully integrated in domoticz. You can only safely update dzVents by updating domoticz.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest