Buienradar json problem  [Solved]

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

Moderator: leecollings

Post Reply
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Buienradar json problem

Post by Maxx »

Hello,

The following script to check the "buienradar" json has been working without problems until some weeks ago.

Code: Select all

return {
	on = {
		httpResponses = {
			'Weather_Callback'
		},
		timer = {'every minute'},
		
	},

    logging =   {   level                   = domoticz.LOG_DEBUG,
                    marker                  = "Buienradar" },	
        
	
	execute = function(domoticz, item)

		if (item.isTimer) then
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'https://data.buienradar.nl/2.0/feed/json',
				method = 'GET',
				callback = 'Weather_Callback', 
			})
		end

		if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
			if (item.statusCode == 200) and (item.isJSON) then
                    domoticz.log('JSON was detected')
                        domoticz.log(item)
                        local Temperature       = item.json['actual']['stationmeasurements'][10]['temperature']
                        local Name              = item.json['actual']['stationmeasurements'][10]['airpressure']
                        local gevoelsTemp       = item.json['actual']['stationmeasurements'][10]['feeltemperature']
                        local Gust              = item.json['actual']['stationmeasurements'][10]['windgusts']
                        local Speed             = item.json['actual']['stationmeasurements'][10]['windspeed']
                        local SpeedBft          = item.json['actual']['stationmeasurements'][10]['windspeedBft']
                        local Sun               = item.json['actual']['stationmeasurements'][10]['sunpower']
                        local Rain24h           = item.json['actual']['stationmeasurements'][10]['rainFallLast24Hour']
                        local Rain1h            = item.json['actual']['stationmeasurements'][10]['rainFallLastHour']
                        local winddirection     = item.json['actual']['stationmeasurements'][10]['winddirectiondegrees']
                        local winddirectionstr  = item.json['actual']['stationmeasurements'][10]['winddirection']
                        local Humidity          = item.json['actual']['stationmeasurements'][10]['humidity']
                        local airpressure       = item.json['actual']['stationmeasurements'][10]['airpressure']
                        local sunpower          = item.json['actual']['stationmeasurements'][10]['sunpower']
                        local Rain              = item.json['actual']['stationmeasurements'][10]['precipitation']
                        local Zicht             = item.json['actual']['stationmeasurements'][10]['visibility']
			else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end
    end
end
}
Now it gives the following error, it seems the json coming back is not recognized as a json anymore. When I enter https://data.buienradar.nl/2.0/feed/json in a browser it shows a good looking json.

Any ideas?

Spoiler: show
2019-08-01 14:39:00.292 Status: dzVents: Info: Buienradar: Timer event
2019-08-01 14:39:00.292 Status: dzVents: Debug: Buienradar: OpenURL: url = https://data.buienradar.nl/2.0/feed/json
2019-08-01 14:39:00.292 Status: dzVents: Debug: Buienradar: OpenURL: method = GET
2019-08-01 14:39:00.292 Status: dzVents: Debug: Buienradar: OpenURL: post data = nil
2019-08-01 14:39:00.292 Status: dzVents: Debug: Buienradar: OpenURL: headers = nil
2019-08-01 14:39:00.292 Status: dzVents: Debug: Buienradar: OpenURL: callback = Weather_Callback
2019-08-01 14:39:00.293 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
2019-08-01 14:40:00.313 Status: dzVents: Info: Buienradar: ------ Start internal script: Buienradar:, trigger: every minute
2019-08-01 14:40:00.314 Status: dzVents: Info: Buienradar: Timer event
2019-08-01 14:40:00.314 Status: dzVents: Debug: Buienradar: OpenURL: url = https://data.buienradar.nl/2.0/feed/json
2019-08-01 14:40:00.314 Status: dzVents: Debug: Buienradar: OpenURL: method = GET
2019-08-01 14:40:00.314 Status: dzVents: Debug: Buienradar: OpenURL: post data = nil
2019-08-01 14:40:00.314 Status: dzVents: Debug: Buienradar: OpenURL: headers = nil
2019-08-01 14:40:00.314 Status: dzVents: Debug: Buienradar: OpenURL: callback = Weather_Callback
2019-08-01 14:40:00.315 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
2019-08-01 14:40:00.887 Status: dzVents: Info: Buienradar: ------ Start internal script: Buienradar: HTTPResponse: "Weather_Callback"
2019-08-01 14:40:00.888 Status: dzVents: Info: Buienradar: HTTP reponse was ok
2019-08-01 14:40:00.892 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
2019-08-01 14:40:00.888 Error: dzVents: Error: (2.4.26) Buienradar: There was a problem handling the request
2019-08-01 14:40:00.891 Error: dzVents: Error: (2.4.26) Buienradar: {["isJSON"]=false, ["isScene"]=false, ["protocol"]="HTTP/2", ["ok"]=true, ["isSecurity"]=false, ["isVariable"]=false, ["statusText"]="OK", ["headers"]={["content-type"]="application/json", ["content-encoding"]="gzip", ["vary"]="Accept-Encoding", ["access-control-allow-headers"]="Origin, X-Requested-With, Content-Type, Accept-Encoding", ["x-powered-by"]="ASP.NET", ["access-control-max-age"]="86400", ["strict-transport-security"]="max-age=2592000", ["request-context"]="appId=cid-v1:edc7228b-6b2c-4316-88f0-d7cb6d4cb317", ["access-control-allow-origin"]="*", ["access-control-allow-methods"]="GET,POST,PUT", ["access-control-allow-credentials"]="false", ["date"]="Thu, 01 Aug 2019 12:40:00 GMT", ["server"]="Kestrel", ["content-length"]="7571", ["cache-control"]="public, max-age=32"}, ["isHTTPResponse"]=true, ["baseType"]="httpResponse", ["trigger"]="Weather_Callback", ["data"]="{"$id":"1","buienradar":{"$id":"2","copyright":"(C)opyright Buienradar / RTL. Alle rechten voorbehouden","terms":"Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."},"actual":{"$id":"3","actualradarurl":"https://api.buienradar.nl/image/1.0/Rad ... asurements":[{"$id":"4","stationid":6391,"stationname":"Meetstation Arcen","lat":51.5,"lon":6.2,"regio":"Venlo","timestamp":"2019-08-01T14:20:00","weatherdescription":"Mix van opklaringen en middelbare of lage bewolking","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Arnhem","lat":52.07,"lon":5.88,"regio":"Arnhem","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Berkhout","lat":52.65,"lon":4.98,"regio":"Berkhout","timestamp":"2019-08-01T14:20:00","weatherdescription":"Mix van opklaringen en middelbare of lage bewolking","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Cadzand","lat":51.38,"lon":3.38,"regio":"Cadzand","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation De Bilt","lat":52.1,"lon":5.18,"regio":"Utrecht","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Den Helder","lat":52.92,"lon":4.78,"regio":"Den Helder","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Eindhoven","lat":51.45,"lon":5.42,"regio":"Eindhoven","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... eetstation Ell","lat":51.2,"lon":5.77,"regio":"Weert","timestamp":"2019-08-01T14:20:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/ima ... emperature"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Maxx wrote: Thursday 01 August 2019 15:01 The script to check the "buienradar" json has been working without problems until some weeks ago.
Now it gives the following error, it seems the json coming back is not recognized as a json anymore. When I enter https://data.buienradar.nl/2.0/feed/json in a browser it shows a good looking json.
I guess this is only part of your script as it does nothing with the values from the JSON but it does work without error on my test system.
I added some log statements to find out what the returnCode is on your system.

Code: Select all

return {
	on = {
		httpResponses = {
			'Weather_Callback'
		},
		timer = {'every minute'},
		
	},

    logging =   {   level                   = domoticz.LOG_DEBUG,
                    marker                  = "Buienradar" },	
        
	
	execute = function(domoticz, item)

		if (item.isTimer) then
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'https://data.buienradar.nl/2.0/feed/json',
				method = 'GET',
				callback = 'Weather_Callback', 
			})
		end

		if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
			if (item.statusCode == 200) and (item.isJSON) then
                    domoticz.log('JSON was detected')
                        domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_FORCE)
                        domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_FORCE)
                        domoticz.log('temperature: ' .. item.json.actual.stationmeasurements[10].temperature,domoticz.LOG_FORCE)
                        local Temperature       = item.json['actual']['stationmeasurements'][10]['temperature']
                        local Name              = item.json['actual']['stationmeasurements'][10]['airpressure']
                        local gevoelsTemp       = item.json['actual']['stationmeasurements'][10]['feeltemperature']
                        local Gust              = item.json['actual']['stationmeasurements'][10]['windgusts']
                        local Speed             = item.json['actual']['stationmeasurements'][10]['windspeed']
                        local SpeedBft          = item.json['actual']['stationmeasurements'][10]['windspeedBft']
                        local Sun               = item.json['actual']['stationmeasurements'][10]['sunpower']
                        local Rain24h           = item.json['actual']['stationmeasurements'][10]['rainFallLast24Hour']
                        local Rain1h            = item.json['actual']['stationmeasurements'][10]['rainFallLastHour']
                        local winddirection     = item.json['actual']['stationmeasurements'][10]['winddirectiondegrees']
                        local winddirectionstr  = item.json['actual']['stationmeasurements'][10]['winddirection']
                        local Humidity          = item.json['actual']['stationmeasurements'][10]['humidity']
                        local airpressure       = item.json['actual']['stationmeasurements'][10]['airpressure']
                        local sunpower          = item.json['actual']['stationmeasurements'][10]['sunpower']
                        local Rain              = item.json['actual']['stationmeasurements'][10]['precipitation']
                        local Zicht             = item.json['actual']['stationmeasurements'][10]['visibility']
			else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
				domoticz.log(item.data, domoticz.LOG_ERROR)
			end
    end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Buienradar json problem

Post by Maxx »

Hi Waaren,Thanks for your quick reply, I ran your script and this is the result
(I am running beta version 4.11038 btw)
Spoiler: show

Code: Select all

 2019-08-01 16:47:00.871 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
2019-08-01 16:47:00.868 Error: dzVents: Error: (2.4.26) Buienradar: There was a problem handling the request
2019-08-01 16:47:00.868 Error: dzVents: Error: (2.4.26) Buienradar: statusCode: 200
2019-08-01 16:47:00.868 Error: dzVents: Error: (2.4.26) Buienradar: isJSON: false
2019-08-01 16:47:00.869 Error: dzVents: Error: (2.4.26) Buienradar: {"$id":"1","buienradar":{"$id":"2","copyright":"(C)opyright Buienradar / RTL. Alle rechten voorbehouden","terms":"Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."},"actual":{"$id":"3","actualradarurl":"https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512","sunrise":"2019-08-01T06:01:00","sunset":"2019-08-01T21:30:00","stationmeasurements":[{"$id":"4","stationid":6391,"stationname":"Meetstation Arcen","lat":51.5,"lon":6.2,"regio":"Venlo","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"W","temperature":23.3,"groundtemperature":24.0,"feeltemperature":25.3,"windgusts":3.4,"windspeed":1.1,"windspeedBft":1,"humidity":53.0,"precipitation":0.0,"sunpower":222.0,"rainFallLast24Hour":0.0,"rainFallLastHour":0.0,"winddirectiondegrees":271},{"$id":"5","stationid":6275,"stationname":"Meetstation Arnhem","lat":52.07,"lon":5.88,"regio":"Arnhem","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"WZW","airpressure":1016.0,"temperature":21.0,"groundtemperature":21.7,"feeltemperature":21.0,"visibility":22300.0,"windgusts":9.0,"windspeed":5.3,"windspeedBft":3,"humidity":70.0,"precipitation":0.0,"sunpower":181.0,"rainFallLast24Hour":0.8,"rainFallLastHour":0.0,"winddirectiondegrees":247},{"$id":"6","stationid":6249,"stationname":"Meetstation Berkhout","lat":52.65,"lon":4.98,"regio":"Berkhout","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt en regen","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/q.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/q","winddirection":"WZW","temperature":18.8,"groundtemperature":18.3,"feeltemperature":18.8,"visibility":12300.0,"windgusts":8.5,"windspeed":4.5,"windspeedBft":3,"humidity":89.0,"precipitation":0.0,"sunpower":329.0,"rainFallLast24Hour":5.7,"rainFallLastHour":0.2,"winddirectiondegrees":255},{"$id":"7","stationid":6308,"stationname":"Meetstation Cadzand","lat":51.38,"lon":3.38,"regio":"Cadzand","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"W","windgusts":7.1,"windspeed":5.7,"windspeedBft":4,"winddirectiondegrees":272},{"$id":"8","stationid":6260,"stationname":"Meetstation De Bilt","lat":52.1,"lon":5.18,"regio":"Utrecht","timestamp":"2019-08-01T16:30:00","weatherdescription":"Mix van opklaringen en middelbare of lage bewolking","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/b.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/b","winddirection":"WNW","airpressure":1015.9,"temperature":21.8,"groundtemperature":22.8,"feeltemperature":21.8,"visibility":33100.0,"windgusts":8.0,"windspeed":4.1,"windspeedBft":3,"humidity":64.0,"precipitation":0.0,"sunpower":468.0,"rainFallLast24Hour":5.5,"rainFallLastHour":0.0,"winddirectiondegrees":285},{"$id":"9","stationid":6235,"stationname":"Meetstation Den Helder","lat":52.92,"lon":4.78,"regio":"Den Helder","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"WZW","airpressure":1015.0,"temperature":19.6,"groundtemperature":20.9,"feeltemperature":19.6,"visibility":21000.0,"windgusts":10.9,"windspeed":7.8,"windspeedBft":4,"humidity":80.0,"precipitation":0.0,"sunpower":444.0,"rainFallLast24Hour":0.0,"rainFallLastHour":0.0,"winddirectiondegrees":247},{"$id":"10","stationid":6370,"stationname":"Meetstation Eindhoven","lat":51.45,"lon":5.42,"regio":"Eindhoven","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"W","airpressure":1016.6,"temperature":22.9,"groundtemperature":24.5,"feeltemperature":25.2,"visibility":34200.0,"windgusts":7.8,"windspeed":5.4,"windspeedBft":3,"humidity":55.0,"precipitation":0.0,"sunpower":476.0,"rainFallLast24Hour":0.6,"rainFallLastHour":0.0,"winddirectiondegrees":259},{"$id":"11","stationid":6377,"stationname":"Meetstation Ell","lat":51.2,"lon":5.77,"regio":"Weert","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"W","temperature":23.3,"groundtemperature":24.2,"feeltemperature":25.3,"visibility":36700.0,"windgusts":8.2,"windspeed":5.2,"windspeedBft":3,"humidity":53.0,"precipitation":0.0,"sunpower":284.0,"rainFallLast24Hour":0.0,"rainFallLastHour":0.0,"winddirectiondegrees":271},{"$id":"12","stationid":6321,"stationname":"Meetstation Euro platform","lat":52.0,"lon":3.28,"regio":"Noordzee","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png","graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c","winddirection":"ZW","airpressure":1017.0,"visibility":22800.0,"windgusts":8.7,"windspeed":6.1,"windspeedBft":4,"winddirectiondegrees":231},{"$id":"13","stationid":6350,"stationname":"Meetstation Gilze Rijen","lat":51.57,"lon":4.93,"regio":"Gilze Rijen","timestamp":"2019-08-01T16:30:00","weatherdescription":"Zwaar bewolkt","iconurl":"https://www.buie
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Maxx wrote: Thursday 01 August 2019 16:48 I ran your script and this is the result
Can you try this

Code: Select all

return 
{
    on = 
	{
        httpResponses = { 'Weather_Callback' },
		timer = {'every minute'},
    },

    logging = { level = domoticz.LOG_DEBUG, marker = "Buienradar" },    

    execute = function(domoticz, item)

        if (item.isTimer) then
            domoticz.log('Timer event')
            domoticz.openURL({
                url = 'https://data.buienradar.nl/2.0/feed/json',
                method = 'GET',
                callback = 'Weather_Callback', 
            })
        end

        if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
            if item.ok then
                domoticz.log('ITEM is OK')
                local rawJSON = domoticz.utils.fromJSON(item.data)
                if rawJSON == nil then 
                    domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                    domoticz.log('Badly formatted data: ' .. string.sub(item.data,1,240) .. ', etc.....', domoticz.LOG_ERROR)
                    return
                end    
                local myStation = rawJSON.actual.stationmeasurements[10]
                
                domoticz.log('temperature: ' .. myStation.temperature,domoticz.LOG_FORCE)
                
                local Temperature       = myStation.temperature
                local Name              = myStation.airpressure
                local gevoelsTemp       = myStation.feeltemperature
                local Gust              = myStation.windgusts
                local Speed             = myStation.windspeed
                local SpeedBft          = myStation.windspeedBft
                local Sun               = myStation.sunpower
                local Rain24h           = myStation.rainFallLast24Hour
                local Rain1h            = myStation.rainFallLastHour
                local winddirection     = myStation.winddirectiondegrees
                local winddirectionstr  = myStation.winddirection
                local Humidity          = myStation.humidity
                local airpressure       = myStation.airpressure
                local sunpower          = myStation.sunpower
                local Rain              = myStation.precipitation
                local Zicht             = myStation.visibility
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
                domoticz.log(item.data, domoticz.LOG_ERROR)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Buienradar json problem

Post by Maxx »

Hi Waaren,

That works!

But what can be the problem reading the json directly, it worked before?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Maxx wrote: Thursday 01 August 2019 17:46 What can be the problem reading the json directly, it worked before?
No clue. It still works from my system. Did you change anything recently wrt network, system or security ?
Please add

Code: Select all

domoticz.utils.dumpTable(item.headers)
just below

Code: Select all

if (item.isHTTPResponse) then
This will show some additional header information from the httpResponse return from buienradar.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Buienradar json problem

Post by Maxx »

I did a fresh install and uploaded the database, no changes.

The line domoticz.utils.dumpTable(item.headers) does not add anything to the log
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Maxx wrote: Thursday 01 August 2019 20:27 I did a fresh install and uploaded the database, no changes.
A fresh install in itself is a big change in my book :)
The line domoticz.utils.dumpTable(item.headers) does not add anything to the log
It should, so probably your log setting is different than mine. I log normal,status,error (see /etc/init.d/domoticz.sh)

can you try with this

Code: Select all

return 
{
    on = 
    {
        httpResponses = { 'Weather_Callback' },
        timer = {'every minute'},
    },

    logging = { level = domoticz.LOG_DEBUG,
                marker = "Buienradar" },    

    execute = function(domoticz, item)

        if (item.isTimer) then
            domoticz.log('Timer event')
            domoticz.openURL({
                url = 'https://data.buienradar.nl/2.0/feed/json',
                method = 'GET',
                callback = 'Weather_Callback', 
            })
        end

        if (item.isHTTPResponse) then
            domoticz.log('---- headers begin ---------- ')
            domoticz.utils.dumpTable(item.headers)
            domoticz.log('---- headers end ---------- ')
            domoticz.log('HTTP reponse was ok')
            if item.ok then
                domoticz.log('ITEM is OK')
                local rawJSON = domoticz.utils.fromJSON(item.data)
                if rawJSON == nil then 
                    domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                    domoticz.log('Badly formatted data: ' .. string.sub(item.data,1,240) .. ', etc.....', domoticz.LOG_ERROR)
                    return
                end    
                local myStation = rawJSON.actual.stationmeasurements[10]
                
                domoticz.log('temperature: ' .. myStation.temperature,domoticz.LOG_FORCE)
                
                local Temperature       = myStation.temperature
                local Name              = myStation.airpressure
                local gevoelsTemp       = myStation.feeltemperature
                local Gust              = myStation.windgusts
                local Speed             = myStation.windspeed
                local SpeedBft          = myStation.windspeedBft
                local Sun               = myStation.sunpower
                local Rain24h           = myStation.rainFallLast24Hour
                local Rain1h            = myStation.rainFallLastHour
                local winddirection     = myStation.winddirectiondegrees
                local winddirectionstr  = myStation.winddirection
                local Humidity          = myStation.humidity
                local airpressure       = myStation.airpressure
                local sunpower          = myStation.sunpower
                local Rain              = myStation.precipitation
                local Zicht             = myStation.visibility
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
                domoticz.log(item.data, domoticz.LOG_ERROR)
            end
        end
    end
}
Result should look something like:
Spoiler: show

Code: Select all

2019-08-01 20:39:01.866  Status: dzVents: Info:  Handling httpResponse-events for: "Weather_Callback
2019-08-01 20:39:01.867  Status: dzVents: Info:  Buienradar: ------ Start internal script: (dzVents) isJon: HTTPResponse: "Weather_Callback"
2019-08-01 20:39:01.948  Status: dzVents: Info:  Buienradar: ---- headers begin ----------
2019-08-01 20:39:01.948  Status: dzVents: > Date: Thu, 01 Aug 2019 18:39:01 GMT
2019-08-01 20:39:01.948  Status: dzVents: > Strict-Transport-Security: max-age=2592000
2019-08-01 20:39:01.948  Status: dzVents: > Content-Length: 7453
2019-08-01 20:39:01.948  Status: dzVents: > Access-Control-Allow-Methods: GET,POST,PUT
2019-08-01 20:39:01.948  Status: dzVents: > Content-Encoding: gzip
2019-08-01 20:39:01.948  Status: dzVents: > Request-Context: appId=cid-v1:01018ffc-b4e0-408d-b7a9-5477ae2b08cb
2019-08-01 20:39:01.948  Status: dzVents: > Access-Control-Allow-Credentials: false
2019-08-01 20:39:01.949  Status: dzVents: > Access-Control-Max-Age: 86400
2019-08-01 20:39:01.949  Status: dzVents: > Content-Type: application/json
2019-08-01 20:39:01.949  Status: dzVents: > Vary: Accept-Encoding
2019-08-01 20:39:01.949  Status: dzVents: > X-Powered-By: ASP.NET
2019-08-01 20:39:01.949  Status: dzVents: > Cache-Control: public, max-age=14
2019-08-01 20:39:01.949  Status: dzVents: > Connection: keep-alive
2019-08-01 20:39:01.949  Status: dzVents: > Server: Kestrel
2019-08-01 20:39:01.949  Status: dzVents: > Access-Control-Allow-Origin: *
2019-08-01 20:39:01.949  Status: dzVents: > Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept-Encoding
2019-08-01 20:39:01.949  Status: dzVents: Info:  Buienradar: ---- headers end ----------
2019-08-01 20:39:01.949  Status: dzVents: Info:  Buienradar: HTTP reponse was ok
2019-08-01 20:39:01.949  Status: dzVents: Info:  Buienradar: ITEM is OK
2019-08-01 20:39:02.026  Status: dzVents: !Info: Buienradar: temperature: 20.9
2019-08-01 20:39:02.027  Status: dzVents: Info:  Buienradar: ------ Finished (dzVents) isJon
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Buienradar json problem  [Solved]

Post by Maxx »

Not sure I understand (see /etc/init.d/domoticz.sh)

Hmm, looks a bit different:

Code: Select all

 2019-08-01 20:47:01.473 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
2019-08-01 20:48:00.742 Status: dzVents: Debug: - OpenURL = {["URL"]="https://data.buienradar.nl/2.0/feed/json", ["method"]="GET", ["_trigger"]="Weather_Callback"}
2019-08-01 20:48:01.224 Status: dzVents: Info: Buienradar: ------ Start internal script: Buienradar: HTTPResponse: "Weather_Callback"
2019-08-01 20:48:01.224 Status: dzVents: Info: Buienradar: ---- headers begin ----------
2019-08-01 20:48:01.224 Status: dzVents: > set-cookie: bm_sv=B3E7EA29961E11EF15EF2A4F0270D344~duE2Q/DuHctEDFzibmcEf3Td95pt+xH1eQvKCWr7lT4bGq0pNEnlt11NO8X+EwUF54Svtj5GehZ9Tg4TlNGCVXtuWd2TKlKFIopYrIjU5ZtmeouiGMSbKGOZDTK4AV3Vv7ccV/0AEOzRfjXLNzyeHXFw0fng8wbYyLfrV66v2Og=; Domain=.buienradar.nl; Path=/; Max-Age=5100; HttpOnly
2019-08-01 20:48:01.225 Status: dzVents: Info: Buienradar: ---- headers end ----------
2019-08-01 20:48:01.225 Status: dzVents: Info: Buienradar: HTTP reponse was ok
2019-08-01 20:48:01.225 Status: dzVents: Info: Buienradar: ITEM is OK
2019-08-01 20:48:01.302 Status: dzVents: !Info: Buienradar: temperature: 20.7
2019-08-01 20:48:01.303 Status: dzVents: Info: Buienradar: ------ Finished Buienradar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Maxx wrote: Thursday 01 August 2019 20:59 Not sure I understand (see /etc/init.d/domoticz.sh)
In my situation the loglevel is defined in the /etc/init.d/domoticz.sh file.


Looking at the the content of your item.headers it looks like relate to the way cookies are handled on your system. I don't have enough knowledge on that subject to add something sensible. It has no direct relation to dzVents or the event system.

on a side note. You use the number 10 to identify a weather station (Gilze Rijen). Please be aware that this number is not fixed. If one of the other stations is set offline or a new one is added the number will change and you will receive data from another station without a warning.

Your script should check for this and alert you !
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Buienradar json problem

Post by Maxx »

Thanks for your help and suggestion about the weather station number.

Until the next ....
User avatar
Antoinne
Posts: 14
Joined: Monday 30 March 2020 17:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Buienradar json problem

Post by Antoinne »

Hi,

Can you perhaps explain to me how to get information on weather stations by number or name through the Buienradar API nowadays? I have managed to create my first dzvents script based on what I learned from this thread :D

And I have looked everywhere, but I can not figure out how to get data from a specific station. Now I do get the weatherreports that I wanted. But I really would like to extend this API-call with some weatherstation specific information, such as for instance local windgusts, windspeed, precipitation, etc in "De Bilt" ..

Code: Select all

return 
{
    on = 
    {
        httpResponses = { 'Weather_Callback' },
        timer = {'every 4 hours'},
    },

    logging = { level = domoticz.LOG_DEBUG,
                marker = "Buienradar" },    

    execute = function(domoticz, item)

        if (item.isTimer) then
            domoticz.log('Timer event')
            domoticz.openURL({
                url = 'https://data.buienradar.nl/2.0/feed/json',
                method = 'GET',
                callback = 'Weather_Callback', 
            })
        end

        if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
            if item.ok then
                domoticz.log('ITEM is OK')
                local rawJSON = domoticz.utils.fromJSON(item.data)
                if rawJSON == nil then 
                    domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                    domoticz.log('Badly formatted data: ' .. string.sub(item.data,1,240) .. ', etc.....', domoticz.LOG_ERROR)
                    return
                end    

                domoticz.log('weatherreport_published: ' .. rawJSON.forecast.weatherreport.published,domoticz.LOG_FORCE)

                domoticz.devices('BR API Published').updateText(rawJSON.forecast.weatherreport.published)
                domoticz.devices('BR API Title').updateText(rawJSON.forecast.weatherreport.title)
                domoticz.devices('BR API Summary').updateText(rawJSON.forecast.weatherreport.summary)
                domoticz.devices('BR API Text').updateText(rawJSON.forecast.weatherreport.text)
                domoticz.devices('BR API Shortterm').updateText(rawJSON.forecast.shortterm.forecast)
                domoticz.devices('BR API Longterm').updateText(rawJSON.forecast.longterm.forecast)

            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
                domoticz.log(item.data, domoticz.LOG_ERROR)
            end
        end
    end
}
And perhaps the code above contains some silly things (it works for me ;) ), but if so, any reference to better coding standards are welcome ;-)


TIA,

Antoinne
User avatar
Antoinne
Posts: 14
Joined: Monday 30 March 2020 17:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Buienradar json problem

Post by Antoinne »

Forgot to mention: proud to be a noob posting to this solved thread ;-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Antoinne wrote: Saturday 25 April 2020 0:07 Can you perhaps explain to me how to get information on weather stations by number or name through the Buienradar API nowadays?
Hope this will get you going with an individual station.
Please feel free to ask if anything is unclear.

Code: Select all

return 
{
    on = 
    {
        httpResponses = { 'Weather_Callback' },
        devices = {'BuienradarTrigger'}, -- just for development and testing
        timer = {'every 4 hours'},
    },

    logging = { level = domoticz.LOG_DEBUG,
                marker = "Buienradar" },    

    execute = function(domoticz, item)
        
        local myStation = 'Meetstation De Bilt'
        
        if item.isTimer or item.isDevice then
            domoticz.log('Timer event')
            domoticz.openURL({
                url = 'https://data.buienradar.nl/2.0/feed/json',
                method = 'GET',
                callback = 'Weather_Callback', 
            })
        end

        if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
            if item.ok then
                domoticz.log('ITEM is OK')
                local rawJSON = domoticz.utils.fromJSON(item.data)
                if rawJSON == nil then 
                    domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                    domoticz.log('Badly formatted data: ' .. string.sub(item.data,1,240) .. ', etc.....', domoticz.LOG_ERROR)
                    return
                end    
                
                local stations = rawJSON.actual.stationmeasurements
                local stationReport
                
                for index, station in ipairs(stations) do
                    if station.stationname == myStation then
                        stationReport = station
                        break -- search is over we found it !!
                   end
                end    
                
                if stationReport then
                    domoticz.utils.dumpTable( stationReport ) -- this will show what is in the JSON for this station
                else
                    domoticz.log('There was no station with the name ' .. myStation .. ' in the Buienradar result JSON' , domoticz.LOG_ERROR)
                end
                
                local forecast = rawJSON.forecast
                local report = forecast.weatherreport
                
                domoticz.log('weatherreport_published: ' .. report.published,domoticz.LOG_FORCE)

                domoticz.devices('BR API Published').updateText(report.published)
                domoticz.devices('BR API Title').updateText(report.title)
                domoticz.devices('BR API Summary').updateText(report.summary)
                domoticz.devices('BR API Text').updateText(report.text)
                domoticz.devices('BR API Shortterm').updateText(forecast.shortterm.forecast)
                domoticz.devices('BR API Longterm').updateText(forecast.longterm.forecast)
                

                
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
                domoticz.log(item.data, domoticz.LOG_ERROR)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Antoinne
Posts: 14
Joined: Monday 30 March 2020 17:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Buienradar json problem

Post by Antoinne »

Hi Waaren,

Very helpful indeed. With this script I now do see the JSON data for the specific station in the log. But I still can not figure out how to write a measurements from this station to a dummy switch. For instance, if I would like to the name of the region in a dummy switch called "BR API Regio", I would expect to use something like this:

Code: Select all

domoticz.devices('BR API Regio').updateText(rawJSON.actual.stationmeasurements.regio)
Looking at the JSON output of Buienradar the above syntax does work for the weather report and forecasts which I already got working. But I can not figure out how to do this for the stationmeasurements. Everything I tried results in a "nil" value in Domoticz.

What am I missing and how do I do this? If i can only get one example of the station measurements working, I'm sure the rest will be simple :lol:

Antoinne
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Buienradar json problem

Post by waaren »

Antoinne wrote: Saturday 25 April 2020 15:30 What am I missing and how do I do this? If i can only get one example of the station measurements working, I'm sure the rest will be simple :lol:
With the example

Code: Select all

return 
{
    on = 
    {
        httpResponses = { 'Weather_Callback' },
        devices = {'BuienradarTrigger'}, -- just for development and testing
        timer = {'every 4 hours'},
    },

    logging = { level = domoticz.LOG_DEBUG,
                marker = "Buienradar" },    

    execute = function(domoticz, item)
        
        local myStation = 'Meetstation De Bilt'
        
        if item.isTimer or item.isDevice then
            domoticz.log('Timer event')
            domoticz.openURL({
                url = 'https://data.buienradar.nl/2.0/feed/json',
                method = 'GET',
                callback = 'Weather_Callback', 
            })
        end

        if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
            if item.ok then
                domoticz.log('ITEM is OK')
                local rawJSON = domoticz.utils.fromJSON(item.data)
                if rawJSON == nil then 
                    domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                    domoticz.log('Badly formatted data: ' .. string.sub(item.data,1,240) .. ', etc.....', domoticz.LOG_ERROR)
                    return
                end    
                
                local stations = rawJSON.actual.stationmeasurements
                local stationReport
                
                for index, station in ipairs(stations) do
                    if station.stationname == myStation then
                        stationReport = station
                        break -- search is over we found it !!
                   end
                end    
                
                if stationReport then
                    domoticz.utils.dumpTable( stationReport ) -- this will show what is in the JSON for this station
                else
                    domoticz.log('There was no station with the name ' .. myStation .. ' in the Buienradar result JSON' , domoticz.LOG_ERROR)
                end
                
                domoticz.devices('BR API Regio').updateText(stationReport.regio)
                
                local forecast = rawJSON.forecast
                local report = forecast.weatherreport
                
                domoticz.log('weatherreport_published: ' .. report.published,domoticz.LOG_FORCE)

                domoticz.devices('BR API Published').updateText(report.published)
                domoticz.devices('BR API Title').updateText(report.title)
                domoticz.devices('BR API Summary').updateText(report.summary)
                domoticz.devices('BR API Text').updateText(report.text)
                domoticz.devices('BR API Shortterm').updateText(forecast.shortterm.forecast)
                domoticz.devices('BR API Longterm').updateText(forecast.longterm.forecast)
                
                
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log('statusCode: ' .. item.statusCode,domoticz.LOG_ERROR)
                domoticz.log('isJSON: ' .. tostring(item.isJSON),domoticz.LOG_ERROR)
                domoticz.log(item.data, domoticz.LOG_ERROR)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Antoinne
Posts: 14
Joined: Monday 30 March 2020 17:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Buienradar json problem

Post by Antoinne »

Jippie! Thanks. I thought I tried that syntax earlier today, but I probably made a mistake somewhere down the line...
Now it's working and I can continu with the other variables. I still have a lot to learn ;-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest