Home Battery Simulation and dynamic vs fixed energy contract comparison

Moderator: leecollings

Post Reply
akamming
Posts: 364
Joined: Friday 17 August 2018 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by akamming »

Hi,

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
}

JanJaap
Posts: 210
Joined: Thursday 12 October 2017 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Dev
Location: the Netherlands
Contact:

Re: Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by JanJaap »

Hey, Nice script! I just decided to do it and have since a couple of months 2x5 kWh Sessy batteries (and obviously created the Sessy plugin ) and switched to a dynamic contract at Vandebron. However am since then working on 2 things:
1 script to make a smart control of the batteries, for which I first made a plugin to read in solar forecast with the goal to empty the batteries at the 2 price peaks in the morning and evening if the solar panels will produce enough
2 some way to asses the earnings of the battery vs situation of only solar panels with variable contract

Second point I did not start on yet as point 1 is not progressing so fast (have more hobbies ;) )

At least my energy consumption has decreased a lot vs before the batteries, but can't put that into euro's yet...... I'll take some inspiration from your script as soon as #1 is done.

Btw I'm going with the assumption the ROI on the batteries will be considerable so hoping to just aid to the environment
RPi 3, Domoticz dev version, Aeon ZWave stick (with a whole bunch of slaves), Zigbee using Zigbee2MQTT, Nest thermo, P1 smart meter on RPi Zero
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by jannl »

Basically my Solaredge setup does all that automatically (more or less).

Do you also 'load' the battery with your script when the prices are low compared to other moments during the day and the sun is not shining all that much? Sometimes it is even more efficient (pricewise) to use load from the grid for the house (or car) and charge the battery from the sun for later use, based on the current price.

Do you use a fixed value for costs? As far as I remember, when the price of a kWh is negative, you won't get the complete costs 'gesaldeeerd' I think (see the Tibber website).

Nevertheless, your script should give you a good indication I think.

Considering the ROI, I tell everyone for now it justs cost money. However, around october (after one year of Tibber and a battery) I hope to prove myself wrong (at least a bit).
lost
Posts: 659
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by lost »

jannl wrote: Tuesday 15 April 2025 9:10 Considering the ROI, I tell everyone for now it justs cost money. However, around october (after one year of Tibber and a battery) I hope to prove myself wrong (at least a bit).
PV+Battery historically only made sense to provide electricity for buildings in secluded places that could not be linked to country electric providers network. So this may only make sense from an economic POV (and nothing else, like ecology etc) if electricity prices rise up to an unsustainable point and/or subventions (equipment/sold electric prices that also makes providers prices rise a self-fulfilling prophety)!

No real progress basically changed this this situation for now: PV yields did not significantly progress. Batteries did (pushed by electric cars) but their efficiency cost a lot and fire hazard is almost impossible to manage. See recent histories of ro-ro's that just sink when (for now) only a few % of their car load were electric ones and ignited, on vessels very well equipped in fire management systems (petrol cars also take fire, but at least can be extinguished), unlimited quantity of water to pump all around, handled by well fire trained crews... Ferries companies were scared up to considering not boarding electric or even hybrid cars anymore.

So, better keep batteries out of the home: If one day I have to consider storage, this'll be for the hot tank in a best effort way the days when sun rises!
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by jannl »

So far, seeing my results, I do not (completely) agree, escpecially in combination with a dynamic contract.
HvdW
Posts: 600
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Home Battery Simulation and dynamic vs fixed energy contract comparison

Post by HvdW »

Screenshot_20250517_113832_Firefox.jpg
Screenshot_20250517_113832_Firefox.jpg (210.02 KiB) Viewed 236 times
I am on fixed tariff and comparing with dynamic.
These are the results.
- normal behavior
- not accounting terugleverkosten from fixed
- no battery
- EV
When switching from fixed to dynamic I presume I'll change my behaviour.
Conclusion: changing to dynamic will bring some savings.
So before changing to dynamic there is a need for risk calculation.
What if dynamic prices rise in winter and one cannot diminish electricity consumption.
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest