since the dutch goverment decided to stop the "Salderingsregeling" in 2027, i am considering
- moving away from my fixed prices energy contract to a dynamic contract
- and/or installing a home battery
Since i could not find good calculations on how to compare the different options and all the calculation provided by the energy providers are bullshit, I decided to simulate both, by creating sensors which
- Calculate the Energy cost for a fixed contract (in this case Vattenfal) and a Dynamic contract (in this case Tibber)
- Calculate the same, but then simulating i have a 13 kwh battery
and a dzventz script to populate the sensors.
This way i can let it run for a while and see which option is most attractive
Here's the code, feel free to try it out if you have the face the same choice as i do..
Code: Select all
-- This script handles everything with Energy, current functionality
--
-- calculates actual power usage and the cost for energy in 2 variants (fixed, dynamic contract)
-- also includes simulation of a home battery
return {
logging = {
level = domoticz.LOG_DEBUG, -- Adjust to your needs
marker = 'Energy2'
},
on = {
devices = {
'Slimme Meter', -- Name of P1 Smart Meter device,
}
},
data = {
oldusage1 = { initial = 0 },
oldusage2 = { initial = 0 },
oldreturn1 = { initial = 0 },
oldreturn2 = { initial = 0 },
},
execute = function(domoticz, SM)
function logObject(obj)
for key, value in pairs(obj) do
domoticz.log(key .. ": " .. tostring(value),domoticz.LOG_DEBUG)
end
end
-- constants
local KwhCost = 0.2324 -- Actual energy price for Vattenfal (constant contract). Set in EUR
local ReturnDeliveryCost = 0.1230 -- Terugleverkosten. Set in EUR
local BatteryCapacity = 13000 -- Set capacity size of battery simulation in Wh
-- input devices
local SE=domoticz.devices(31) -- Electricity device (usage, counter) in which the actual. Solar Output is stored. Type = Electic Usage Device
local CAH=domoticz.devices(1793) --virtual electricity device of your chard at home device. Type = Electic Usage Device
local AEP=domoticz.devices(1992) -- Actual Energy Price (Dynamic). Type = Custom Sensor which contains price in EUR
-- output for net Stroom Verbruik calculation
local SV=domoticz.devices(32) -- Virtual Electricity device (usage, counter) in which the actual power consumption will be stored
-- output devices for Tibber (dynamic) / Vattenfal (fixed) comparison
local TK=domoticz.devices(1998) -- Tibber cost. Counter Device. With Custom Unit EUR and divider 100
local VFK=domoticz.devices(2001) -- Vattenfal cost. Counter Device. With Custom Unit EUR and divider 100
-- output devices for Home Battery simulation
local VB=domoticz.devices(1997) -- Virtual Battery. Custom Device with Unit Wh
local TBS=domoticz.devices(1999) -- Tibber (Battery Simulation).. cost if there would have been a home battery. Counter Device. With Custom Unit EUR and divider 100
local VBS=domoticz.devices(2000) -- Vattenfal (Battery Simulation).. cost if there would have been a home battery. Counter Device. With Custom Unit EUR and divider 100
-- calculate real energy consumption
local virtuelemeterstand=SE.WhTotal+SM.usage1+SM.usage2-SM.return1-SM.return2
local vermogen=SM.usage-SM.usageDelivered+SE.actualWatt
domoticz.log("SV.updateElectsricity("..vermogen..","..virtuelemeterstand..")", domoticz.LOG_FORCE)
SV.updateElectricity(vermogen,virtuelemeterstand)
-- calculate energy cost
if (domoticz.data.oldusage1==0 or domoticz.data.oldusage2==0 or domoticz.data.oldreturn1==0 or domoticz.data.oldreturn2==0) then
domoticz.log("no previous values, ignoring measurement",domoticz.LOG_DEBUG)
else
domoticz.log('Updating Vattenfal cost',domoticz.LOG_DEBUG)
usedwhs=SM.usage1+SM.usage2-domoticz.data.oldusage1-domoticz.data.oldusage2
returnedwhs=SM.return1+SM.return2-domoticz.data.oldreturn1-domoticz.data.oldreturn2
domoticz.log('Usage='..usedwhs..' Wh, returned='..returnedwhs..' Wh',domoticz.LOG_DEBUG)
-- Update Vattenfal cost counter
UsedCost=usedwhs/1000*KwhCost
ReturnEarnings=returnedwhs/1000*(KwhCost-ReturnDeliveryCost)
domoticz.log('To pay='..UsedCost..' EUR, Earned='..ReturnEarnings..' EUR',domoticz.LOG_DEBUG)
VFK.updateCounter(VFK.counter*100+(UsedCost-ReturnEarnings)*100)
domoticz.log('Updating Energy Cost ('..VFK.name..')'..' with '..VFK.counter+(UsedCost-ReturnEarnings),domoticz.LOG_FORCE)
-- Update Tibber cost counter
domoticz.log('Actual energy price = '..AEP.sensorValue..' EUR',domoticz.LOG_DEBUG)
AdditionalCost=(usedwhs-returnedwhs)/1000*AEP.sensorValue
TK.updateCounter(TK.counter*100+AdditionalCost*100)
domoticz.log('Updating Energy Cost ('..TK.name..')'..' with '..TK.counter+AdditionalCost,domoticz.LOG_FORCE)
-- Simulate Home Battery
domoticz.log('Current Battery USage = '..VB.sensorValue..'Wh ('..VB.sensorValue/BatteryCapacity*100 ..'%)',domoticz.LOG_DEBUG)
-- Make sure we always maintain the values
NewCapacity=VB.sensorValue
newusedwhs=usedwhs
newreturnedwhs=returnedwhs
-- Check if we have to charge the virtual battery
if (returnedwhs<BatteryCapacity-VB.sensorValue) then
domoticz.log('Virtual Battery charging with '..returnedwhs..'wh',domoticz.LOG_DEBUG)
newreturnedwhs=0
NewCapacity=NewCapacity+returnedwhs
else
domoticz.log('No need to charge virtual battery',domoticz.LOG_DEBUG)
end
-- Check if we have to discharge the virtual battery
if (usedwhs>0 and usedwhs<VB.sensorValue) then
domoticz.log('Virtual Battery discharging with '..usedwhs..'wh',domoticz.LOG_DEBUG)
newusedwhs=0
NewCapacity=NewCapacity-usedwhs
else
domoticz.log('No need to discharge virtual battery',domoticz.LOG_DEBUG)
end
-- update Virtual Battery values
VB.updateCustomSensor(NewCapacity)
-- Update Vattenfal Virtual cost counter
UsedCost=newusedwhs/1000*KwhCost
ReturnEarnings=newreturnedwhs/1000*(KwhCost-ReturnDeliveryCost)
domoticz.log('To pay='..UsedCost..' EUR, Earned='..ReturnEarnings..' EUR',domoticz.LOG_DEBUG)
VBS.updateCounter(VBS.counter*100+(UsedCost-ReturnEarnings)*100)
domoticz.log('Updating Energy Cost ('..VBS.name..')'..' with '..VBS.counter+(UsedCost-ReturnEarnings),domoticz.LOG_FORCE)
-- Update Tibber `Virtual cost counter
domoticz.log('Actual energy price = '..AEP.sensorValue..' EUR',domoticz.LOG_DEBUG)
AdditionalCost=(newusedwhs-newreturnedwhs)/1000*AEP.sensorValue
TBS.updateCounter(TBS.counter*100+AdditionalCost*100)
domoticz.log('Updating Energy Cost ('..TBS.name..')'..' with '..VBS.counter+AdditionalCost,domoticz.LOG_FORCE)
end
-- updating counters
domoticz.data.oldusage1=SM.usage1
domoticz.data.oldusage2=SM.usage2
domoticz.data.oldreturn1=SM.return1
domoticz.data.oldreturn2=SM.return2
end
}