dzVents script to load "energieweerbericht"

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

dzVents script to load "energieweerbericht"

Post by willemd »

For anyone interested to load the data of the "energieweerbericht" of the Netherlands Energy Dashboard, here is a dzVents script to do exactly that.
https://ned.nl/nl/energieweerbericht#Energieweerbericht

Energieweerbericht is the predicted solar and wind energy production for the week ahead for The Netherlands. The scripts loads the daily totals and that info can be used to make some guess of the future dynamic energy prices for the week ahead (combined with other info). With some changes to the API parameters you could also load hourly data or only data for a specific region. Please check the NED.nl API documentation.

For my own use, I am displaying a stacked bar in dashticz of the two counters to see the total predicted production.

To use the script you have to create two managed counters, one to hold the predicted wind energy production and one to hold the predicted solar energy production. You also need an account at NED.nl and need to request an API token and store that token in a Domoticz user variable. Please adapt the IDX numbers on line 16,17 and 18 for your installation.

Code: Select all

return {
	on = {
		timer = {
			'every hour' -- time trigger
		},
		httpResponses = {
			'NEDtrigger' -- must match with the callback passed to the openURL command
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'energieweerbericht',
	},
	execute = function(domoticz, item)
	    
	    local idxNEDwindforecast=223 -- two managed counters to hold the values as received in the API response
	    local idxNEDsolarforecast=224
	    local idxNEDtoken=23  -- token to be requested at NED.nl and to be stored in a user variable
	    
	    local NEDtoken=domoticz.variables(idxNEDtoken).value

        local startdate=os.date("%Y-%m-%d")
        local cleardate=os.date("%Y-%m-%d",os.time()-24*60*60) -- yesterday
        local enddate=os.date("%Y-%m-%d",os.time()+7*24*60*60)  -- data can be requested 7 days ahead

        local baseURL="https://api.ned.nl/v1/utilizations?"

		if (item.isTimer) then
		    -- first request wind data, type is 1
		    local URLparameters="point=0&type=1&granularity=6&granularitytimezone=1&classification=1&activity=1&validfrom[after]="..startdate.."&validfrom[strictly_before]="..enddate  
		    -- check API documentation at NED.nl for more info about parameters
            local totalURL=baseURL..URLparameters
			domoticz.openURL({
				url = totalURL,
				method = 'GET',
				callback = 'NEDtrigger', -- see httpResponses above.
				headers = { ['X-AUTH-TOKEN'] = NEDtoken ,
				            ['accept'] = 'application/xml'
				            },
			})
			-- now request solar data, type is 2 
			URLparameters="point=0&type=2&granularity=6&granularitytimezone=1&classification=1&activity=1&validfrom[after]="..startdate.."&validfrom[strictly_before]="..enddate
            totalURL=baseURL..URLparameters
			domoticz.openURL({
				url = totalURL,
				method = 'GET',
				callback = 'NEDtrigger', -- see httpResponses above.
				headers = { ['X-AUTH-TOKEN'] = NEDtoken ,
				            ['accept'] = 'application/xml'
				            },
			})
		end

		if (item.isHTTPResponse) then  -- process the API response
            -- this will be done once for each type separately
			if (item.ok) then
			    --domoticz.log('start dumptable ned', domoticz.LOG_INFO)
    			--domoticz.utils.dumpTable(item.xml.response.item)  -- dumpTable can be used for debugging
    			--domoticz.log('end dumptable ned', domoticz.LOG_INFO)
    			
				if (item.isXML) then
				    -- clear yesterday's data from the meter table (but not meter_calendar)
				    domoticz.openURL("http://127.0.0.1:8080/json.htm?type=command&param=deletedatapoint&idx="..idxNEDwindforecast.."&date="..cleardate.."%2000:00:00")
                    domoticz.openURL("http://127.0.0.1:8080/json.htm?type=command&param=deletedatapoint&idx="..idxNEDsolarforecast.."&date="..cleardate.."%2000:00:00")
				    
				    -- process future values for week ahead
                    for day=1,7 do
                        local requestType=domoticz.utils.stringSplit(item.xml.response.item[day].type,"/")[3]
                        local capacity=item.xml.response.item[day].capacity
                        local capacityDate=domoticz.utils.stringSplit(item.xml.response.item[day].validto,"T")[1]
                        domoticz.log("type "..requestType.." date "..capacityDate.." capacity "..capacity,domoticz.LOG_INFO)
                        
                        if requestType=="1" then  -- wind date
                            -- this will insert into the meter_calendar table but the future values will not show up in the graph, only the past and current.
                            domoticz.devices(idxNEDwindforecast).updateHistory(capacityDate,"0"..";"..capacity)                           
                             -- this will insert into the meter table and those future values will show up in the graph
                             domoticz.devices(idxNEDwindforecast).updateHistory(capacityDate.." 00:00:00","0;"..capacity)
                             if capacityDate==startdate then
                                domoticz.devices(idxNEDwindforecast).updateCounter(capacity) -- counter icon shows value of today
                            end    
                        else
                            if requestType=="2" then -- solar data
                                domoticz.devices(idxNEDsolarforecast).updateHistory(capacityDate,"0"..";"..capacity) -- this will insert into the meter_calendar table but the future values will not show up in the domoticz graph, only the past and current.
                                domoticz.devices(idxNEDsolarforecast).updateHistory(capacityDate.." 00:00:00","0;"..capacity) -- this will insert into the meter table and those future values will show up in the graph
                                if capacityDate==startdate then
                                    domoticz.devices(idxNEDsolarforecast).updateCounter(capacity)
                                end    
                            end   
                        end    
		            end
				else
				    domoticz.log('is not xml', domoticz.LOG_INFO) 
				end        			
			else
				domoticz.log('There was a problem handling the NED request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}
HvdW
Posts: 612
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: dzVents script to load "energieweerbericht"

Post by HvdW »

Great Willemd!
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest