Page 1 of 1
Script not working as expected...
Posted: Monday 17 February 2025 23:07
by HennyN
Hello HELP
What is wrong with this script??????
Code: Select all
return {
on = { timer = { 'every 2 minutes' }},
execute = function(domoticz, device, timer)
local vandaagKwh = domoticz.devices('Power').counterToday
local vandaagM3Gas = domoticz.devices('Gas').counterToday
local StroomKosten = domoticz.devices('StroomKosten')
local GasKosten = domoticz.devices('GasKosten')
local TotaalKosten = domoticz.devices('EnergieKosten')
local startDateCoolBlue = '2023-10-18'
local endDateCoolBlue = '2024-10-18'
local currentDate = domoticz.time.rawDate
local currentTime = domoticz.time.rawTime
local gasM3Prijs = 0.1356156 -- Coolblue
local kwhPrijs = 0.313136 -- Normaal tarief CoolBlue
if (currentTime == 'between 20:00:00 and 07:00:00') or (domoticz.day == 'Saturday') or (domoticz.day == 'Sunday') then
local kwhPrijs = 0.294744 -- Daltarief CoolBlue
else
local kwhPrijs = 0.313136 -- Normaal tarief CoolBlue
end
-- Vaste kosten in Euro's per dag (zoals vastrecht) CoolBlue
local kwhPrijsVast = 0.949003
local gasM3PrijsVast = 0.657998
-- Kosten berekenen
local KwhKosten = tonumber(domoticz.utils.round( (kwhPrijs * vandaagKwh) + kwhPrijsVast,2))
local GasM3Kosten = tonumber(domoticz.utils.round( (gasM3Prijs * vandaagM3Gas) + gasM3PrijsVast,2))
local KostenTotaal = KwhKosten + GasM3Kosten
print ("===============================================================")
print ("Dit is de kilowattuurprijs die in de berekening wordt gebruikt.")
print (kwhPrijs)
print (gasM3Prijs)
print (currentTime)
print (currentDate)
print ("===============================================================")
local KwhKosten = tonumber(domoticz.utils.round( kwhPrijs * vandaagKwh,2))
local GasM3Kosten = tonumber(domoticz.utils.round( gasM3Prijs * vandaagM3Gas,2))
local KostenTotaal = KwhKosten + GasM3Kosten
-- Kosten updaten
StroomKosten.updateCustomSensor(KwhKosten)
GasKosten.updateCustomSensor(GasM3Kosten)
TotaalKosten.updateCustomSensor(KostenTotaal)
end
}
The result is always:
2025-02-17 22:58:00.386 dzVents: ------ Start internal script: Test:, trigger: "every 2 minutes"
2025-02-17 22:58:00.402 dzVents: ===============================================================
2025-02-17 22:58:00.402 dzVents: Dit is de kilowattuurprijs die in de berekening wordt gebruikt.
2025-02-17 22:58:00.402 dzVents: 0.313136
2025-02-17 22:58:00.402 dzVents: 0.1356156
2025-02-17 22:58:00.402 dzVents: 22:58:00
2025-02-17 22:58:00.402 dzVents: 2025-02-17
2025-02-17 22:58:00.403 dzVents: ===============================================================
2025-02-17 22:58:00.403 dzVents: ------ Finished Test
Between 20:00 and 07:00 the kwhPrijs should be 0.294744 and not 0.313136
So something is wrong in this script..... But I cann't find it. I don't see it.
Help is much appreciated
Regards, Henny
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 0:45
by HvdW
To start with the first expression in the domoticz execute part could be
domoticz.devices('Power').dump()
It will show you the complete output in the log.
You will see that there are different counters for T1 (low) and T2 (high) so the section of evening and weekends is no longer necessary.
The smart meter delivers everything automagically.
Think about using a text sensor to display your data instead of having 3 different display sensors.
I alles AI to have a look at your script and this was the result
Code: Select all
return {
active = true,
on = {
timer = { 'every 2 minutes' }
},
execute = function(domoticz, device, timer)
local vandaagKwh = domoticz.devices('Power').counterToday
local vandaagM3Gas = domoticz.devices('Gas').counterToday
local StroomKosten = domoticz.devices('StroomKosten')
local GasKosten = domoticz.devices('GasKosten')
local TotaalKosten = domoticz.devices('EnergieKosten')
local startDateCoolBlue = '2023-10-18'
local endDateCoolBlue = '2024-10-18'
local currentDate = domoticz.time.rawDate
local currentTime = domoticz.time.rawTime
local gasM3Prijs = 0.1356156 -- Coolblue
local kwhPrijs = 0.313136 -- Normaal tarief CoolBlue
if (currentTime >= '20:00:00' and currentTime < '07:00:00') or (domoticz.day == 'Saturday') or (domoticz.day == 'Sunday') then
kwhPrijs = 0.294744 -- Daltarief CoolBlue
else
kwhPrijs = 0.313136 -- Normaal tarief CoolBlue
end
-- Vaste kosten in Euro's per dag (zoals vastrecht) CoolBlue
local kwhPrijsVast = 0.949003
local gasM3PrijsVast = 0.657998
-- Kosten berekenen
local KwhKosten = tonumber(domoticz.utils.round((kwhPrijs * vandaagKwh) + kwhPrijsVast, 2))
local GasM3Kosten = tonumber(domoticz.utils.round((gasM3Prijs * vandaagM3Gas) + gasM3PrijsVast, 2))
local KostenTotaal = KwhKosten + GasM3Kosten
print ("===============================================================")
print ("Dit is de kilowattuurprijs die in de berekening wordt gebruikt.")
print (kwhPrijs)
print (gasM3Prijs)
print (currentTime)
print (currentDate)
print ("===============================================================")
-- Kosten updaten
StroomKosten.updateCustomSensor(KwhKosten)
GasKosten.updateCustomSensor(GasM3Kosten)
TotaalKosten.updateCustomSensor(KostenTotaal)
end
}
When you declare a local variable inside an if else statement lua wont be able to handle it outside that statement.
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 10:22
by HennyN
Oké, thanks.
I don't quite understand the inpact of your last sentence.
I found the script here:
https://ehoco.nl/stroom-gas-en-waterkos ... -domoticz/
and here is something similar.
https://krijnermerins.nl/domoticz/dagel ... -domoticz/
From the responses, I understand that it works, but so it doesn't with me. Possibly something has changed the script syntax of Domoticz in the meantime
I'm going to have another look at what other data comes out of the smart meter.
Regards, Henny
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 10:35
by HennyN
@HvdW
Your AI generated script doesn't work neither.
Or the script is not right, or I'm doing something wrong.
Regards, Henny
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 21:39
by HvdW
I made this for you.
The only thing you have to do is create a new TexSensor.
In this script it is named 'YourTextSensor'.
Give it any name you like but don't forget to adapt it in the script.
Code: Select all
-- Definieer de prijzen en vaste lasten
local gasM3Prijs = 0.1356156
local kwhPrijsT1 = 0.294744
local kwhPrijsT2 = 0.313136
local kwhPrijsVast = 0.949003 -- vaste lasten stroom per dag
local gasM3PrijsVast = 0.657998 -- vaste lasten gas per dag
-- Helper function
local function spaces(count)
return string.rep(" ", count)
end
return {
on = {
timer = {
'every 1 minutes',
'at 00:00' -- Reset om middernacht
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "----- Energie kosten berekening -----"
},
data = {
-- Variabelen om vorige waarden en cumulatief verbruik op te slaan
previousTotalUsageT1 = { initial = nil },
previousTotalUsageT2 = { initial = nil },
stroomVandaagT1 = { initial = 0 },
stroomVandaagT2 = { initial = 0 },
},
execute = function(domoticz, trigger)
-- Haal de devices op
local powerDevice = domoticz.devices('Power')
local gasDevice = domoticz.devices('Gas')
if trigger.isTimer and trigger.triggerInfo == 'at 00:00' then
-- Reset waarden om middernacht
domoticz.data.stroomVandaagT1 = 0
domoticz.data.stroomVandaagT2 = 0
domoticz.data.previousTotalUsageT1 = powerDevice.rawData[1]
domoticz.data.previousTotalUsageT2 = powerDevice.rawData[2]
domoticz.log("Waarden zijn gereset voor een nieuwe dag.", domoticz.LOG_INFO)
else
-- Haal de huidige waarden op
local totalUsageT1 = powerDevice.rawData[1] -- totalUsage T1 in Watt
local totalUsageT2 = powerDevice.rawData[2] -- totalUsage T2 in Watt
local gasVandaag = gasDevice.counterToday -- gasverbruik vandaag
-- Bereken het stroomverbruik sinds de laatste meting (T1 en T2) en converteer van Watt naar kWh
if domoticz.data.previousTotalUsageT1 ~= nil and domoticz.data.previousTotalUsageT2 ~= nil then
local diffT1 = totalUsageT1 - domoticz.data.previousTotalUsageT1
local diffT2 = totalUsageT2 - domoticz.data.previousTotalUsageT2
if diffT1 >= 0 then -- Voorkom negatieve waarden (bijv. bij meterreset)
domoticz.data.stroomVandaagT1 = domoticz.data.stroomVandaagT1 + (diffT1 / 1000) -- Converteer Watt naar kWh
else
domoticz.log("Negatieve T1 waarde gedetecteerd, mogelijk meterreset", domoticz.LOG_WARNING)
end
if diffT2 >= 0 then -- Voorkom negatieve waarden
domoticz.data.stroomVandaagT2 = domoticz.data.stroomVandaagT2 + (diffT2 / 1000) -- Converteer Watt naar kWh
else
domoticz.log("Negatieve T2 waarde gedetecteerd, mogelijk meterreset", domoticz.LOG_WARNING)
end
else
domoticz.log("Initialisatie van stroom meterstanden", domoticz.LOG_INFO)
end
-- Sla de huidige waarden op voor de volgende iteratie
domoticz.data.previousTotalUsageT1 = totalUsageT1
domoticz.data.previousTotalUsageT2 = totalUsageT2
-- Bereken de kosten voor vandaag
local kostenStroomT1Vandaag = domoticz.data.stroomVandaagT1 * kwhPrijsT1
local kostenStroomT2Vandaag = domoticz.data.stroomVandaagT2 * kwhPrijsT2
local kostenStroomVandaag = kostenStroomT1Vandaag + kostenStroomT2Vandaag + kwhPrijsVast
local kostenGasVandaag = (gasVandaag * gasM3Prijs) + gasM3PrijsVast
local totaleKostenVandaag = kostenStroomVandaag + kostenGasVandaag
-- Log de berekende waarden
domoticz.log(string.format("totalUsageT1 (T1): %.3f Watt", totalUsageT1), domoticz.LOG_DEBUG)
domoticz.log(string.format("totalUsageT2 (T2): %.3f Watt", totalUsageT2), domoticz.LOG_DEBUG)
domoticz.log(string.format("Stroom vandaag (T1): %.3f kWh", domoticz.data.stroomVandaagT1), domoticz.LOG_DEBUG)
domoticz.log(string.format("Stroom vandaag (T2): %.3f kWh", domoticz.data.stroomVandaagT2), domoticz.LOG_DEBUG)
domoticz.log(string.format("Kosten Stroom T1 vandaag: %.3f €", kostenStroomT1Vandaag), domoticz.LOG_DEBUG)
domoticz.log(string.format("Kosten Stroom T2 vandaag: %.3f €", kostenStroomT2Vandaag), domoticz.LOG_DEBUG)
domoticz.log(string.format("Kosten Stroom vandaag: %.3f €", kostenStroomT1Vandaag + kostenStroomT2Vandaag), domoticz.LOG_DEBUG)
domoticz.log(string.format("Gas vandaag: %.3f m³", gasVandaag), domoticz.LOG_DEBUG)
domoticz.log(string.format("Kosten Gas vandaag: %.3f €", kostenGasVandaag), domoticz.LOG_DEBUG)
domoticz.log(string.format("Totale kosten vandaag: %.3f €", totaleKostenVandaag), domoticz.LOG_DEBUG)
-- Gebruik powerDevice.counterToday voor de totale stroom vandaag (dit is al in kWh)
local totalStroomVandaag = powerDevice.counterToday
-- Maak de tekst voor de sensor
local tekst = string.format(
"Gas vandaag: %.3f m³\n" ..
"Stroom vandaag: %.3f kWh\n" ..
"Stroom T1 vandaag: %.3f kWh\n" ..
"Stroom T2 vandaag: %.3f kWh\n" ..
"Vaste kosten Stroom: %.3f €\n" ..
spaces(15) .. "Vaste kosten Gas: %.3f €\n" ..
spaces(15) .. "Kosten T1 vandaag: %.3f €\n" ..
spaces(15) .. "Kosten T2 vandaag: %.3f €\n" ..
spaces(15) .. "Kosten Energie vandaag: %.3f €",
gasVandaag,
totalStroomVandaag,
domoticz.data.stroomVandaagT1,
domoticz.data.stroomVandaagT2,
kwhPrijsVast,
gasM3PrijsVast,
kostenStroomT1Vandaag,
kostenStroomT2Vandaag,
totaleKostenVandaag
)
-- Update de tekstsensor
local textDevice = domoticz.devices('YourTextSensor') -- Zorg dat de naam exact overeenkomt!
if textDevice then
textDevice.updateText(tekst)
else
domoticz.log("Fout: Tekstsensor 'YourTextSensor' niet gevonden!", domoticz.LOG_ERROR)
end
end
end
}
After installing it won't display the data you expect.
After the reset at 00:00 it will show what you expect at 00:01.
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 22:27
by HennyN
@HvdW,
Hello,
I see you are an expert in this matter. I will implement and test it tomorrow. I spent quite some time experimenting again this afternoon, but to no result.
Thanks for now and I'll let you know if it works and gives the result I'm hoping for.
That sensor I'll call ‘EnergieRekening’ I think.
I'm also going to see if I understand how that script is put together and works.
My current contract has no difference in prices, so high and low is the same. The previous one did have different tariffs. That was also the reason I started working on this script. And I found out that
https://krijnermerins.nl/domoticz/dagel ... omoticz/'s script didn't work at all.
I wanted to calculate with my previous supplier's tariffs until 18 October 2024. And from that date with the new tariffs, so that I could compare the costs per year in real terms.
Thanks for your help
Regards Henny
Re: Script not working as expected...
Posted: Tuesday 18 February 2025 23:37
by HvdW
Whatever you do, after saving your script, watch the log.
When your script executes, watch the log.
Re: Script not working as expected...
Posted: Wednesday 19 February 2025 13:50
by HennyN
@HvdW
This works excellent.
With this as a base, I can move on, and see what else I need, and customise it to my needs.
Great work.
Regards,
Henny
Re: Script not working as expected...
Posted: Monday 24 March 2025 21:51
by HennyN