Fetch current electricity price for SE3, SE4, DK1, DK2, NO2
Posted: Sunday 03 November 2019 8:29
[EDIT 2022-11-07, to use the the API on the server as it was changed]
I have created this script as I have my electricity cost/price based on nordpools actual electricity proce.
The webservice I fetch data from only support the following areas: DK1, DK2, SE3, SE4, NO2, I assume it's due to it's a danish webservice, so unfortunately not usable for everyone
It gets price in EUR and I locally convert those to DKK (exchange rate 7,46), the dummy selector show a traffic light, green, yellow or red, depending on what you have set limits to in script
Note that price fetched are EUR per Mega What hour, (MWh) which are converted to kWh in the script
create one text dummy device, and one dummy switch selector, as shown in pictures below. Note that the most expensive price in selector is Red and have value 0, which is the switch set to OFF, so it should be fairly easy to check if lamp is off then don't use electricity
I have created this script as I have my electricity cost/price based on nordpools actual electricity proce.
The webservice I fetch data from only support the following areas: DK1, DK2, SE3, SE4, NO2, I assume it's due to it's a danish webservice, so unfortunately not usable for everyone
It gets price in EUR and I locally convert those to DKK (exchange rate 7,46), the dummy selector show a traffic light, green, yellow or red, depending on what you have set limits to in script
Note that price fetched are EUR per Mega What hour, (MWh) which are converted to kWh in the script
create one text dummy device, and one dummy switch selector, as shown in pictures below. Note that the most expensive price in selector is Red and have value 0, which is the switch set to OFF, so it should be fairly easy to check if lamp is off then don't use electricity
Code: Select all
--[[
Author : Bjacobse
Description : Get current Electricity price for chosen area, set Electricity pric in text device and dummy switch.
Dummy switch selector show pricing as traffic light, to be used for enable/disable switches for heating
]]--
return {
-- optional active section,
-- when left out the script is active
-- note that you still have to check this script
-- as active in the side panel
active = {
true, -- either true or false, or you can specify a function
},
-- trigger
on = {
-- timer riggers
--timer = { 'every minute' },
timer = { 'every hour' },
httpResponses = { 'electricitycostresponse' }
},
--[[
-- custom logging level for this script
logging = {
level = domoticz.LOG_DEBUG,
marker = "Electricity Price"
},
]]--
-- actual event code
execute = function(domoticz, item)
-- execute when timer is trigger
if (item.isTimer) then
local myTimezone = 'HourDK'
--area to chose between SE3, SE4, DK1, DK2, NO2
-- The service provisder energidataservice.dk is only supporting above areas
local myPricearea = 'DK2'
-- ISO 8601 timestamp
--Somehow only whole hour is giving results, so set min=00
local myDateTime = (os.date("%Y-%m-%dT%H")..":00")
-- Need end hour, which is next hour
local myDateTimeEnd = (os.date("%Y-%m-%dT%H", os.time(os.date('*t')) + 3600)..":00")
--domoticz.log(myDateTimeEnd)
-- { = %7B
-- " = %22
-- } = %7D
-- https://api.energidataservice.dk/dataset/Elspotprices?start=2022-11-07T19:00&end=2022-11-07T20:00&filter={%22PriceArea%22:[%22DK1%22]}
local url = string.format("https://api.energidataservice.dk/dataset/Elspotprices?start=%s&end=%s&filter={\"PriceArea\":[\"%s\"]}",myDateTime, myDateTimeEnd, myPricearea)
-- url to logfile
--domoticz.log(url)
domoticz.openURL({
url = url,
method = 'GET',
callback = 'electricitycostresponse'
})
end
-- execute after http response
if (item.isHTTPResponse and item.ok) then
-- 0.03887 = 29øre
-- 0.0536= 40 øre
-- 0.0670 = 50øre
local greenCostEUR_kWh = 0.0536
-- Yellow is interval between green and red
-- 0.08 = 60 øre
-- 0.10 = 75 øre
local redCostEUR_kWh = 0.08
--domoticz.log("http response ok")
-- force to handle as json
-- we know it is json but dzVents cannot detect this
-- convert to Lua
local json = domoticz.utils.fromJSON(item.data)
-- json is now a Lua table
--domoticz.log(json.records[1].SpotPriceEUR)
local myCurrentpriceEUR = (json.records[1].SpotPriceEUR)
myCurrentpriceEUR = myCurrentpriceEUR /1000
--domoticz.log((myCurrentpriceEUR .. "EUR/kWh"))
--Uncommnet below to get price in EUR/Kwh
-- update text device with current electricity price
--domoticz.devices('Elprice now').updateText((myCurrentpriceEUR .. " EUR/MWh"))
--convert to DKK, 7,46dkk/EUR. Change to 2 decimal points
local convert = tostring(tonumber(string.format("%.2f", (json.records[1].SpotPriceEUR) * 7.46/10)))
-- Denmark prefer to have comma as separator and not a dot (which in DK is a thousand separator)
-- replace . with , Remember that . is special char and needs to be %.
convert = convert:gsub("%.", ",")
--domoticz.log(convert)
--comment below to reomve price in øre/kWh
local myCurrentpriceDKK = (convert)
-- update text device with current electricity price
domoticz.devices('Elprice now').updateText((myCurrentpriceDKK .. " Øre/kWh"))
--Set traffic light colour
if tonumber(myCurrentpriceEUR) >= redCostEUR_kWh then
domoticz.devices('Elprice').switchSelector(0)
--domoticz.log("Red")
end
--if tonumber(myCurrentpriceEUR) < yellowCostEUR_kWh then
if ((tonumber(myCurrentpriceEUR) < redCostEUR_kWh) and (tonumber(myCurrentpriceEUR) > greenCostEUR_kWh)) then
--domoticz.devices('Elprice').switchSelector("Yellow")
domoticz.devices('Elprice').switchSelector(10)
--domoticz.log("yellow")
end
if tonumber(myCurrentpriceEUR) <= greenCostEUR_kWh then
domoticz.devices('Elprice').switchSelector(20)
--domoticz.log("Green")
end
end
end
}