Page 1 of 1
Dynamic Price for Energy - How-To ?
Posted: Thursday 12 December 2024 13:09
by oxygen
Bottom line is:
Dynamic Price uses a sensor that timeout by default at 60 mins.
If the device is not updated every hour but only once or twice a day, the value will be "timed-out", and won't be used for the dynamic price.
Solutions
Option A: Increase the timeout to "a lot of minutes" to cover a full update period (1440 for a full day)
Option B: Refresh/Re-update the sensor value once every hour (even if no value change)
====
Original Post:
I'm trying to leverage the Dynamic Price for the Energy costs (instead of the Low/High or Simple) tarifs.
But I'm not sure how it is supposed to work.
Currently, I'm setting the price in my Custom Counter at the time of price change.
That is 10.10 PM and 6.10 AM (effectively it's at 10PM & 6AM, but that doesn't make much difference)
And then I leave it for the rest of the day.
Somehow, it doesn't work.
The "cost" is not properly calculated.
So how is it supposed to work ?
Why should I set it "every minute" ?
Why doesn't it take the "last "active" value ?
Or am I missing something ?
Re: Dynamic Price for Energy - How-To ?
Posted: Thursday 12 December 2024 16:23
by waltervl
You can create a dummy custom sensor where the current active tariff is in (eg 0.25 for 25 cents per kWh). That custom sensor can be used for the (dynamic) electricity cost calculation by Domoticz through menu Setup-settings, tab counters.
Same principle for Gas cost calculation.
You can update the Gas and Electricity tariff custom sensor as soon as the tariff changes and from that time these new tariffs will be used for cost calculation.
Re: Dynamic Price for Energy - How-To ?
Posted: Friday 13 December 2024 15:59
by oxygen
This is the current approach I've chosen (update as soon as the tariff changes). But it doesn't work.
It seems that the custom sensor needs to be continuously updated, even if there is no price change for the Dynamic Price to "work".
If the Custom Sensor is not updated, it seems as if the computation (consumption x price) is done with a "null price" for the Tariff.
Currently, my script is the following:
Code: Select all
-- Routine to set the Dynamic Price of Tempo based on the current used color
if not ( item.isHTTPResponse ) then
if domoticz.time.matchesRule('at 22:10') then
if (electricity_bleu.usage > 0) then
--usage1 is LP
-- Set TempoPrice to HC/Bleu
tempo_price_device.updateCustomSensor(tempo_bleu_hc_kwh)
domoticz.log('Set Tempo Price for bleu HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_blanc.usage > 0) then
-- Set TempoPrice to HC/Blanc
tempo_price_device.updateCustomSensor(tempo_blanc_hc_kwh)
domoticz.log('Set Tempo Price for blanc HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_rouge.usage > 0) then
-- Set TempoPrice to HC/Rouge
tempo_price_device.updateCustomSensor(tempo_rouge_hc_kwh)
domoticz.log('Set Tempo Price for rouge HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
return
end
if domoticz.time.matchesRule('at 06:10') then
if (electricity_bleu.usage > 0) then
--usage1 is LP
-- Set TempoPrice to HP/Bleu
tempo_price_device.updateCustomSensor(tempo_bleu_hp_kwh)
domoticz.log('Set Tempo Price for bleu HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_blanc.usage > 0) then
-- Set TempoPrice to HP/Blanc
tempo_price_device.updateCustomSensor(tempo_blanc_hp_kwh)
domoticz.log('Set Tempo Price for blanc HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_rouge.usage > 0) then
-- Set TempoPrice to HP/Rouge
tempo_price_device.updateCustomSensor(tempo_rouge_hp_kwh)
domoticz.log('Set Tempo Price for rouge HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
return
end
end
The Tariff Sensor is the following:
One of the Energy Sensor provides the following:

Another one:

Energy Dashboard is also completely "off":
One can see that the cost is not properly calculated.
It looks as if the cost is calculated for 1hr (between 6:10 and 7:10) and nothing is calculated during the rest of the day.
I'm fine in updating every 5 mins the Custom Sensor, but it feels an overkill.
Imho, the dynamic price should use the last "updated" value as the applicable price.
Re: Dynamic Price for Energy - How-To ?
Posted: Friday 13 December 2024 17:58
by waltervl
Sorry, cannot help you further here. Your way of working seems correct. No idea why it is not working for you.
Re: Dynamic Price for Energy - How-To ?
Posted: Friday 13 December 2024 19:28
by habahabahaba
Show the full code.
What is the trigger for the script? Time or device?
if domoticz.time.matchesRule('at 22:10') then - you are trying to catch the event at the exact time.
What if to give some difference like
if domoticz.time.matchesRule('at 22:09-22:11') then ... ?
Re: Dynamic Price for Energy - How-To ?
Posted: Friday 13 December 2024 22:44
by HvdW
I agree with @habahabahaba ; share the code
Or to start with: where do you collect the prices.
If you live in the Netherlands you'd better use the Enever Dutch Electricity/Gas Price Feed plugin.
If not show us the URL you're using.
Re: Dynamic Price for Energy - How-To ?
Posted: Saturday 14 December 2024 7:48
by gizmocuz
Shortlog values are stored every 5 minutes by default.
What value did you enter in the settings page (Other) for Sensor Timeout? (Is this still the default value of 60?)
Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 11:33
by oxygen
gizmocuz wrote: ↑Saturday 14 December 2024 7:48
Shortlog values are stored every 5 minutes by default.
What value did you enter in the settings page (Other) for Sensor Timeout? (Is this still the default value of 60?)
Sensor timeout is still 60 minutes indeed.
I understand I would need to re-update the value then every 60 minutes to keep it active ?
The current script is :
Code: Select all
return {
on = {
timer = {
'at *:02',
'at *:15',
'at *:30',
'at *:45',
'at 23:54',
'at 23:59',
'at 00:04',
'at 06:10',
'at 22:10',
},
devices = {
'TeleinfokWh Jours Bleus'
},
httpResponses = {
scriptVar .. "*"
}
},
data = {
energyCosts = { initial = {} }
},
logging = {
level = domoticz.LOG_INFO,
marker = 'Tarif Tempo',
},
execute = function(domoticz, item)
-- ********************************************************************* Your changes below this line *************
-- input Devices
local electricity_bleu = domoticz.devices("TeleinfokWh Jours Bleus") -- P1 device or comment this line
local electricity_blanc = domoticz.devices("TeleinfokWh Jours Blancs") -- P1 device or comment this line
local electricity_rouge = domoticz.devices("TeleinfokWh Jours Rouges") -- P1 device or comment this line
--local gas = dz.devices("Gas") -- Gas meter or comment this line
local tempo_price_device = domoticz.devices("TempoPrice")
-- outPut Devices
local electricityCost = domoticz.devices("CostTempoBleu") -- define this virtual sensor as Custom sensor or comment this line when not used
local electricityCostBleu = domoticz.devices("CostTempoBleu") -- define this virtual sensor as Custom sensor or comment this line when not used
local electricityCostBlanc = domoticz.devices("CostTempoBlanc") -- define this virtual sensor as Custom sensor or comment this line when not used
local electricityCostRouge = domoticz.devices("CostTempoRouge") -- define this virtual sensor as Custom sensor or comment this line when not used
--local gasCost = dz.devices("gasCost today") -- define this virtual sensor as Custom sensor or comment this line when not used
local tempo_bleu_hc_kwh = tonumber(domoticz.variables('tempo_bleu_hc_kwh').value)
local tempo_bleu_hp_kwh = tonumber(domoticz.variables('tempo_bleu_hp_kwh').value)
local tempo_blanc_hc_kwh = tonumber(domoticz.variables('tempo_blanc_hc_kwh').value)
local tempo_blanc_hp_kwh = tonumber(domoticz.variables('tempo_blanc_hp_kwh').value)
local tempo_rouge_hc_kwh = tonumber(domoticz.variables('tempo_rouge_hc_kwh').value)
local tempo_rouge_hp_kwh = tonumber(domoticz.variables('tempo_rouge_hp_kwh').value)
local function updateCustomSensor(device, value)
local currentValue = device.rawData[1]
if value ~= tonumber(currentValue) then -- Update only needed when new value is different fom previous one
domoticz.log(device.name .. " ==>> previous value: " .. currentValue .. " ; new value " .. value,domoticz.LOG_INFO)
device.updateCustomSensor(value)
else
domoticz.log(device.name .. " ==>> No Change / Error in Value",domoticz.LOG_INFO)
end
end
-- Routine to set the Dynamic Price of Tempo based on the current used color
if not ( item.isHTTPResponse ) then
if domoticz.time.matchesRule('at 22:10') then
if (electricity_bleu.usage > 0) then
--usage1 is LP
-- Set TempoPrice to HC/Bleu
tempo_price_device.updateCustomSensor(tempo_bleu_hc_kwh)
domoticz.log('Set Tempo Price for bleu HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_blanc.usage > 0) then
-- Set TempoPrice to HC/Blanc
tempo_price_device.updateCustomSensor(tempo_blanc_hc_kwh)
domoticz.log('Set Tempo Price for blanc HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_rouge.usage > 0) then
-- Set TempoPrice to HC/Rouge
tempo_price_device.updateCustomSensor(tempo_rouge_hc_kwh)
domoticz.log('Set Tempo Price for rouge HC : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
return
end
if domoticz.time.matchesRule('at 06:10') then
if (electricity_bleu.usage > 0) then
--usage1 is LP
-- Set TempoPrice to HP/Bleu
tempo_price_device.updateCustomSensor(tempo_bleu_hp_kwh)
domoticz.log('Set Tempo Price for bleu HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_blanc.usage > 0) then
-- Set TempoPrice to HP/Blanc
tempo_price_device.updateCustomSensor(tempo_blanc_hp_kwh)
domoticz.log('Set Tempo Price for blanc HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
if (electricity_rouge.usage > 0) then
-- Set TempoPrice to HP/Rouge
tempo_price_device.updateCustomSensor(tempo_rouge_hp_kwh)
domoticz.log('Set Tempo Price for rouge HP : ' .. tempo_price_device.sensorValue,domoticz.LOG_INFO)
end
return
end
end
end
}
I've removed some unneeded section as this script is also doing other stuff to fetch value from the Teleinfo.
tl;dr:
It decides what color the day is based on which color the teleinfo is giving some usage.
I'm doing this because the Teleinfo somehow is not updated properly "daily" when the color changes. So to ensure I'm selecting the proper one, I'm basing it on the ACTUAL consumption, not theoretical message/value.
Adding to the issue:
Here is the last few days of price sensor, again proving that the sensor updates properly. But isn't used properly (most likely due to the sensor timeout). I don't necessarily want to modify the timeout for ALL sensors, but only for this one ...)

Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 15:12
by gizmocuz
Yes, you need to update at least every 60 seconds if your sensor timeout is 60 seconds.
Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 15:31
by waltervl
gizmocuz wrote: ↑Monday 16 December 2024 15:12
Yes, you need to update at least every 60 seconds if your sensor timeout is 60 seconds.
According documentation timeout is in minutes so you should update the sensor every hour?
https://wiki.domoticz.com/Application_S ... or_Timeout
Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 16:02
by oxygen
So i've modified the script to update at least one time every one. And extend the timeout to 65 mins to ensure a full overlapp.
And now it "works" as intended.
However, for the dynamic tariff based on a custom device, this behavior feels wrong to me

The timeout for this kind of device is "wrong".
Either it should be discardable by sensor if the custom sensor is to be kept
Or it should be possible to have a specific sensor used for the dynamic tariff that would not timeout at all.
Eitherway, it should be more explicit in the documentation 
Not a lot of energy vendor change tarif every hour, so there is a very high risk that the calculation won't work because of that

Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 17:29
by waltervl
oxygen wrote: ↑Monday 16 December 2024 16:02
Not a lot of energy vendor change tarif every hour, so there is a very high risk that the calculation won't work because of that
I do not know where you live, but in the Netherlands it is very common to have an energy contract with dynamic tariffs changing ever x minutes!
I personally have a fixed tariff (not even day and night), so I just use the tariffs from menu settings
Before this I (and others) was not aware that a custom sensor behaves like this for cost calculation.....
Edit, it is documented now:
https://wiki.domoticz.com/Application_S ... alculation
Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 17:42
by HvdW
If it is only cost calculation you can do it like this:
Code: Select all
return {
on = {
devices = { 'Actual Price' },
timer = { 'at 23:59' }
},
logging = {
level = domoticz.LOG_DEBUG,
marker = 'Cost Calculation'
},
data = {
costT1 = { initial = 0 }, -- daltarief
costT2 = { initial = 0 }, -- normaal tarief
dynamicCost = { initial = 0 },
lastUsage1 = { initial = 0 },
lastUsage2 = { initial = 0 },
lastActualPrice = { initial = 0 },
stroomOverdag = { initial = 0 },
},
execute = function(domoticz, triggeredItem)
local now = os.time()
local currentDate = os.date('%d-%m-%Y %H:%M', now)
local logFile = "/home/hein/electricity-costs.csv"
-- Haal de actuele verbruiksgegevens op
local powerDevice = domoticz.devices('Power')
local usage1 = powerDevice.usage1
local usage2 = powerDevice.usage2
local actualPrice = domoticz.devices('Actual Price').sensorValue
-- Bereken het verschil in verbruik sinds de laatste meting
local deltaUsage1 = usage1 - domoticz.data.lastUsage1
local deltaUsage2 = usage2 - domoticz.data.lastUsage2
-- Update de laatste verbruikswaarden
domoticz.data.lastUsage1 = usage1
domoticz.data.lastUsage2 = usage2
-- Vaste tarieven (in euro per kWh)
local costT1 = 0.2750 -- Normaal tarief
local costT2 = 0.2340 -- Daltarief
-- Bereken de kosten voor vast tarief
domoticz.data.costT1 = domoticz.data.costT1 + (deltaUsage1 * costT1)
domoticz.data.costT2 = domoticz.data.costT2 + (deltaUsage2 * costT2)
-- Bereken de kosten voor dynamisch tarief met de prijs van de voorgaande periode
domoticz.data.dynamicCost = domoticz.data.dynamicCost + ((deltaUsage1 + deltaUsage2) * domoticz.data.lastActualPrice)
-- Update de laatste dynamische prijs
domoticz.data.lastActualPrice = actualPrice
-- voor het vergelijken even de hoeveelheid stroom tussen zon op en zon onder
local sunrise = domoticz.time.sunriseInMinutes
local sunset = domoticz.time.sunsetInMinutes
local currentTime = domoticz.time.minutesSinceMidnight
if currentTime > sunrise and currentTime < sunset then
-- hier moet stroom sunrise komen en later stroomsunset en daarna van elkaar aftrekken
domoticz.data.stroomOverdag = domoticz.devices('Power').counterToday
end
-- Log de kosten
domoticz.log('Kosten T1 vandaag: €' .. domoticz.data.costT1, domoticz.LOG_DEBUG)
domoticz.log('Kosten T2 vandaag: €' .. domoticz.data.costT2, domoticz.LOG_DEBUG)
domoticz.log('Dynamische kosten vandaag: €' .. domoticz.data.dynamicCost, domoticz.LOG_DEBUG)
domoticz.log('Kosten T1 vandaag: €' .. math.floor((domoticz.data.costT1/1000)*100+0.5)/100, domoticz.LOG_DEBUG)
domoticz.log('Kosten T2 vandaag: €' .. math.floor((domoticz.data.costT2/1000)*100+0.5)/100, domoticz.LOG_DEBUG)
domoticz.log('Dynamische kosten vandaag: €' .. math.floor((domoticz.data.dynamicCost/1000)*100+0.5)/100, domoticz.LOG_DEBUG)
domoticz.log('Dynamische prijs nu : €' .. actualPrice, domoticz.LOG_DEBUG)
domoticz.log('stroomOverdag :' .. domoticz.data.stroomOverdag, domoticz.LOG_DEBUG)
-- Schrijf de gegevens naar het CSV-bestand
local file = io.open(logFile, "a")
file:write(currentDate, ";", deltaUsage1/1000, ";", deltaUsage2/1000, ";", math.floor((domoticz.data.dynamicCost/1000)*100+0.5)/100,
";", math.floor(((domoticz.data.costT1+domoticz.data.costT2)/1000)*100+0.5)/100, ";", math.floor((domoticz.data.costT1/1000)*100+0.5)/100,
";", math.floor((domoticz.data.costT2/1000)*100+0.5)/100, ";", actualPrice, ";", domoticz.data.stroomOverdag, "\n")
file:close()
-- Reset de kosten aan het einde van de dag
if os.date('%H:%M') == '23:59' then
domoticz.data.costT1 = 0
domoticz.data.costT2 = 0
domoticz.data.dynamicCost = 0
-- domoticz.data.stroomOverdag = 0
end
end
}
In this code T1 and T1 are low and high tarif respectively.
Dynamic costs is calculated from a sensor driven by Enever Dutch Electricity/Gas Price Feed plugin.
Whenever the dynamic tarif changes the script is triggered and calculates costs using the delta and the tarif of the past hour.
I wrote this script to get insight if it is profitable to change from normal tarif to dynamic tarif.
The sensor from the plugin behaves like this:

- dynamic.jpg (77.01 KiB) Viewed 996 times
Re: Dynamic Price for Energy - How-To ?
Posted: Monday 16 December 2024 19:47
by oxygen
waltervl wrote: ↑Monday 16 December 2024 17:29
I do not know where you live, but in the Netherlands it is very common to have an energy contract with dynamic tariffs changing ever x minutes!
In France.
And we only have day/night (in a very subtle way with EJP or Tempo)
Or weekdays/weekend for some specific 3rd party providers.
I'm not aware of any contract with hourly price changes in Fr.