Page 1 of 1

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

Posted: Sunday 03 November 2019 8:29
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 4824 times
elprice_now_dummy_text.png
elprice_now_dummy_text.png (13.74 KiB) Viewed 4824 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
}



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

Posted: Sunday 03 November 2019 8:30
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 4822 times
elprice_appearence.png
elprice_appearence.png (14.63 KiB) Viewed 4822 times

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

Posted: Thursday 16 April 2020 20:44
by marin849
Nice! I will test this.

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

Posted: Monday 18 October 2021 1:16
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

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

Posted: Monday 01 November 2021 12:50
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

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

Posted: Thursday 07 April 2022 15:39
by norlin
Before I make an effort, is it still possible to get price info from the place the script use?

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

Posted: Thursday 07 April 2022 19:12
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\"}"

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

Posted: Monday 11 April 2022 17:53
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 :)

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

Posted: Sunday 04 September 2022 20:12
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 3156 times
elprice_switch_220904.jpg
elprice_switch_220904.jpg (9.91 KiB) Viewed 3156 times
Not sure what went wrong here.

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

Posted: Monday 05 September 2022 23:06
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.

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

Posted: Wednesday 07 September 2022 14:56
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

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

Posted: Monday 26 September 2022 23:27
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?

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

Posted: Friday 28 October 2022 12:49
by bjacobse
Hi I have been absent from here (due to house building activities)
I hope I can look into this during the weekend

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

Posted: Friday 28 October 2022 13:32
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

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

Posted: Monday 07 November 2022 23:22
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
}

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

Posted: Monday 07 November 2022 23:29
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

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

Posted: Sunday 20 November 2022 19:59
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]}