Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Moderator: leecollings

Post Reply
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

[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
elprice_dummy_switch_selector.png
elprice_dummy_switch_selector.png (52.26 KiB) Viewed 4822 times
elprice_now_dummy_text.png
elprice_now_dummy_text.png (13.74 KiB) Viewed 4822 times

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
}


Last edited by bjacobse on Monday 07 November 2022 23:45, edited 5 times in total.
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

Somehow I can't attach 4 pictures so here are the switches as they appear in Domoticz
elprice_now_appearence.png
elprice_now_appearence.png (11.97 KiB) Viewed 4820 times
elprice_appearence.png
elprice_appearence.png (14.63 KiB) Viewed 4820 times
marin849
Posts: 43
Joined: Tuesday 27 May 2014 10:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4xxx
Location: Sweden
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by marin849 »

Nice! I will test this.
insippo
Posts: 41
Joined: Sunday 26 June 2016 20:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by insippo »

Please help with this. Not working.
2021-10-18 02:17:00.588 Error: EventSystem: Lua script Elprice did not return a commandArray
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

Maybe you have some empty variables, and then won't get an reply and command array is empty
Find this in your code

Code: Select all

    --domoticz.log(url)
and change to this:

Code: Select all

    domoticz.log(url)
Now you will have your command path in the domoticz logfile, then you can verify if something is missing in your code
norlin
Posts: 2
Joined: Wednesday 01 February 2017 10:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by norlin »

Before I make an effort, is it still possible to get price info from the place the script use?
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by waltervl »

Play a little bit with the url and you will know

Code: Select all

"https://api.energidataservice.dk/datastore_search?resource_id=elspotprices&filters={\"%s\":\"%s\",\"PriceArea\":\"%s\"}"
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Szwuntex
Posts: 9
Joined: Friday 31 July 2015 13:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by Szwuntex »

insippo wrote: Monday 18 October 2021 1:16 Please help with this. Not working.
2021-10-18 02:17:00.588 Error: EventSystem: Lua script Elprice did not return a commandArray
Choose dzVents as script language instead :)
kimhav
Posts: 148
Joined: Tuesday 01 October 2013 8:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Sweden
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by kimhav »

Well, looked simple enough and nothing shows and based on the above I placed the scrip in the /dzVents/scripts as placing in /lua/scripts generated the same error as the above post. This is how the devices shows currently:
elprice_utility_220904.jpg
elprice_utility_220904.jpg (7.59 KiB) Viewed 3154 times
elprice_switch_220904.jpg
elprice_switch_220904.jpg (9.91 KiB) Viewed 3154 times
Not sure what went wrong here.
kimhav
Posts: 148
Joined: Tuesday 01 October 2013 8:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Sweden
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by kimhav »

Right, found the error... :oops: typo in device name so instead of 'Elprice now' I used 'Elprice Now'. So works just fine. Nice function request for the script would be to include currency converter directly based on EUR as base currency and convert to currency of choice and of course to find a free currency converter service.
kimhav
Posts: 148
Joined: Tuesday 01 October 2013 8:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Sweden
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by kimhav »

Noticed today that the current way of fetching the pricing data via the API has changed and it's necessary to tweak the API call in accordance with the updated API guide: https://www.energidataservice.dk/guides/api-guides
jvonzastrow
Posts: 2
Joined: Wednesday 08 July 2020 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by jvonzastrow »

kimhav wrote: Wednesday 07 September 2022 14:56 Noticed today that the current way of fetching the pricing data via the API has changed and it's necessary to tweak the API call in accordance with the updated API guide: https://www.energidataservice.dk/guides/api-guides
@kimhav Have you updated it? Can you share?
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

Hi I have been absent from here (due to house building activities)
I hope I can look into this during the weekend
kimhav
Posts: 148
Joined: Tuesday 01 October 2013 8:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Sweden
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by kimhav »

Hi Bjacobse, stumbled over this fork of your script on facebook which I used for my own tweaking but surely this would provide you input the original script
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

Now I have updated the script, and it looks like it's working again on my Domoticz,

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
}
Last edited by bjacobse on Monday 07 November 2022 23:46, edited 1 time in total.
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

kimhav wrote: Friday 28 October 2022 13:32 Hi Bjacobse, stumbled over this fork of your script on facebook which I used for my own tweaking but surely this would provide you input the original script
Thank you, I looked at the code after I have updated my own script, good that it could be forked to help others
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: Fetch current electricity price for SE3, SE4, DK1, DK2, NO2

Post by bjacobse »

Something seems not be working from the data provider
as this date seems to give an empty result 20 Nov,
https://api.energidataservice.dk/datase ... 22DK2%22]}

but same request 19 Nov is ok
https://api.energidataservice.dk/datase ... 22DK2%22]}
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest