[SOLVED] JSON Path weather warnings DWD (Deutscher Wetterdienst)


(Stefan) #1

Hello,
I’m currently using Weather Underground and the API to get weather warnings in OH.
As the service gets worse and worse and for new User the WU Service no longer provide an API, I looked around for alternatives.

In Germany, the DWD has the following file:
https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json

This will be every 10min. updated.

Unfortunately I am not very good in JSON Path, so here is an example of what information would be interesting for me:

,"108326000":[{"regionName":"Schwarzwald-Baar-Kreis","start":1528722000000,"end":1528723800000,"type":0,"state":"Baden-Württemberg","level":3,"stateShort":"BW","description":"Es treten Gewitter auf. Dabei gibt es Sturmböen mit Geschwindigkeiten um 80km/h (22m/s, 44kn, Bft 9) sowie Starkregen mit Niederschlagsmengen bis 25 l/m² pro Stunde und Hagel.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor STARKEM GEWITTER","instruction":"ACHTUNG! Hinweis auf mögliche Gefahren: Örtlich kann es Blitzschlag geben. Bei Blitzschlag besteht Lebensgefahr! Vereinzelt können beispielsweise Bäume entwurzelt und Dächer beschädigt werden. Achten Sie besonders auf herabstürzende Äste, Dachziegel oder Gegenstände. Während des Platzregens sind kurzzeitig Verkehrsbehinderungen möglich.","event":"STARKES GEWITTER"}]

this information for the items:

"start":1528722000000
"end":1528723800000
"level":3
"description":"Es treten Gewitter auf. Dabei gibt es Sturmböen mit Geschwindigkeiten um 80km/h (22m/s, 44kn, Bft 9) sowie Starkregen mit Niederschlagsmengen bis 25 l/m² pro Stunde und Hagel."
"headline":"Amtliche WARNUNG vor STARKEM GEWITTER"
"instruction":"ACHTUNG! Hinweis auf mögliche Gefahren: Örtlich kann es Blitzschlag geben. Bei Blitzschlag besteht Lebensgefahr! Vereinzelt können beispielsweise Bäume entwurzelt und Dächer beschädigt werden. Achten Sie besonders auf herabstürzende Äste, Dachziegel oder Gegenstände. Während des Platzregens sind kurzzeitig Verkehrsbehinderungen möglich."
"event":"STARKES GEWITTER"

or:

,"106435000":[{"regionName":"Main-Kinzig-Kreis","start":1528711200000,"end":1528754400000,"type":0,"state":"Hessen","level":1,"stateShort":"HE","description":"Ab dem Mittag bilden sich einzelne schwere Gewitter mit heftigem Starkregen bis 40 l/qm in kurzer Zeit, Hagel um 4 cm sowie Sturmböen bis 85 km/h (Bft 9). Vereinzelt können auch extreme Regenmengen um 70 l/qm in wenigen Stunden und schwere Sturmböen bis 100 km/h (Bft 10) nicht ausgeschlossen werden. In der Nacht zum Dienstag lässt die Unwettergefahr deutlich nach. ","altitudeStart":null,"altitudeEnd":null,"headline":"VORABINFORMATION UNWETTER vor SCHWEREM GEWITTER","instruction":"Dies ist ein Hinweis auf erwartete Unwetter. Er soll die rechtzeitige Vorbereitung von Schutzmaßnahmen ermöglichen. Bitte verfolgen Sie die weiteren Wettervorhersagen mit besonderer Aufmerksamkeit.","event":"VORABINFORMATION SCHWERES GEWITTER"}]
"start":1528711200000
"end":1528754400000
"level":1
"description":"Ab dem Mittag bilden sich einzelne schwere Gewitter mit heftigem Starkregen bis 40 l/qm in kurzer Zeit, Hagel um 4 cm sowie Sturmböen bis 85 km/h (Bft 9). Vereinzelt können auch extreme Regenmengen um 70 l/qm in wenigen Stunden und schwere Sturmböen bis 100 km/h (Bft 10) nicht ausgeschlossen werden. In der Nacht zum Dienstag lässt die Unwettergefahr deutlich nach. "
"headline":"VORABINFORMATION UNWETTER vor SCHWEREM GEWITTER"
"instruction":"Dies ist ein Hinweis auf erwartete Unwetter. Er soll die rechtzeitige Vorbereitung von Schutzmaßnahmen ermöglichen. Bitte verfolgen Sie die weiteren Wettervorhersagen mit besonderer Aufmerksamkeit."
"event":"VORABINFORMATION SCHWERES GEWITTER"

Here is also the actual warnings.json as XML for upload:
warnings.json.xml (289.8 KB)

It would be important that only this data is output. Of course, there may be no message for the IDs, when their is no warnig for the region. It may also be the case that multiple messages are submitted for the same ID.

Maybe someone can help me?


(Vincent Regaud) #2

I have helped about this a few weeks ago. Search the forum
This is solved


(Udo Hartmann) #3

This json isn’t valid. So I doubt that openHAB will be able to use it directly. Instead you will have to strip the beginning and the end warnWetter.loadWarnings(valid json here); to get the valid json: valid json here
I’m pretty sure this could be done by a rule or even a javascript.

After further analysis I’m pretty sure that the node number has an encoded part of the region, take a look at the 3rd to the 6th digit. For example every node with …6435… will be “Main-Kinzig-Kreis”.

But not to know how many messages are in, would be the main thing. How to deal with this?


(Vincent Regaud) #4

I’ll have a go in the morning with Javascript
I think I’ll do a js transform to return a valid json
And do JSONPATH from there…
We’ll see


(Vincent Regaud) #5

@The-Elk

Hello, I think I have a solution for you.
Not straightforward but workable
Are you only interested in these 2 regions above?


(Stefan) #6

Thanks for your support.

I was also opend the Topic [SOLVED] JSON Path "(DWD) Deutscher Wetterdienst Pollenflug " pollen flying but it was another case and with your help it was solved.

But in this case I would like to integrate the weather alerts (warnings).
I also try to transfer the JSONPATH from the other topic, because it looks very similar.

But I have two differnet challenges:
First, the warning for the IDs is only in warnings.json include in fact of real warning.
Second, in some cases, their a two or more warings for the same ID.

to your question:

It was only a sample for an ID. I need the information from another ID.


(Vincent Regaud) #7

Which one?

Do you want all the warnings or just the first one?


(Stefan) #8

Here is a list of all IDs and regions:
http://www.dwd.de/DE/leistungen/gds/help/warnungen/cap_warncellids_csv.csv?__blob=publicationFile&v=4

I need the information for ID: 109564000

If its possible, all :wink:

But what I see, the first in the file is the newest one.


(Vincent Regaud) #9

This should get you the JSON for the choosen region:

var String jsonString = sendHttpGetRequest("https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json")
jsonString = transform("JS", "getDWDjson.js", jsonString)

Create a file called getDWDjson.js in your transform folder with the following content:

(function(jsonString) {
var newString = jsonString.replace('warnWetter.loadWarnings(','');
newString = newString.replace(');','');
var newJSON = JSON.parse(newString);
var jsonResult = "";
if (newJSON.warnings.hasOwnProperty('109564000')) {
    jsonResult =JSON.stringify(newJSON.warnings.['109564000'][0]);
} else jsonResult = 'NULL'
return jsonResult;
})(input)

The function will return only the json of your region if present or NULL if not

Your can then use the JSONPATH transform to get your data:

WeatherWarningDWD_description.postUpdate(transform("JSONPATH","$.description",jsonString)

Hope that helps.

PS. There are no regions with multiple warnings at the moment so I don’t know the data structure for that.


(Stefan) #10

@vzorglub

thanks for your support.

Today I am very busy, but I will try it as soon as possible.

See in my linked file, delete the .xml and you got a warnings.json from the day with many warnings.

I found multiple warings with the IDs 110042000, 107336000, 109173000 …
All the IDs had warning “level”:1 (advance warning, hours before) and “level”:4

perhaps it help you for analysing


(Vincent Regaud) #11

Mmmmh I had a look,
It won’t be easy for two reasons.

  • We don’t know how many warning there can be
  • The returned json will be an array (No big deal)

How do you plan to store the results in OH?
Do you want to create another for items for each warning?


(Udo Hartmann) #12

Is there a way to get the size of the array (i.e. the list)? one could define a pointer, an item to store the whole array and some items to display one warning. A rule would ensure that the pointer will fit to the list size, then you could switch between the messages via setpoint.


(Vincent Regaud) #13

There is always a way in javascript…

But I don’t want to end up with several javascript transforms… (Maybe that’s the way forward)

If I return the array from the js transform it will be a string
We can’t easily split it because the separator is a comma, used everywhere in json…

The Json transform doesn’t accept arrays as a result… :frowning_face:

Not straightforward…


(Udo Hartmann) #14

Oh, I wasn’t aware of that. What a pity…


(Stefan) #15

I think we need a separate item for each value and items for each event.
So I think it makes the most sense in terms of concept to have only the most recent (newest) warning displayed?
If I interpret the JSON file correctly, are at the top always the current (newest) warning and if another warning is present, then it is further down in the file?!

as additional information:

the values:

“start”:1528722000000
“end”:1528723800000

are displays the date time in UNIX.

maybe we can combine this?


(Stefan) #16

Today I had a little bit time and try it: I try it with ID 113003000 in cause of warnings now!

items:

DateTime	DWD_Unwetter_last_update			"letzte Aktualisierung [%1$td.%1$tm.%1$tY %1$tH:%1$tM Uhr]"		<time>			

String		DWD_Unwetter_start					"Start: [%s]"				
String		DWD_Unwetter_end					"Ende: [%s]"
String		DWD_Unwetter_level					"Level: [%s]"
String		DWD_Unwetter_description			"Beschreibung: [%s]"
String		DWD_Unwetter_headline				"Betreff: [%s]"
String		DWD_Unwetter_instruction			"Hinweis: [%s]"
String		DWD_Unwetter_event					"Event: [%s]"

getDWDjson.js

(function(jsonString) {
var newString = jsonString.replace('warnWetter.loadWarnings(','');
newString = newString.replace(');','');
var newJSON = JSON.parse(newString);
var jsonResult = "";
if (newJSON.warnings.hasOwnProperty('113003000')) {
    jsonResult =JSON.stringify(newJSON.warnings['113003000'][0]);
} else jsonResult = 'NULL'
return jsonResult;
})(input)

rule:


rule "Unwetterwarnung DWD"
when
   Time cron "0 * * ? * *"     
then
	var String jsonString = sendHttpGetRequest("https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json")
	jsonString = transform("JS", "getDWDjson.js", jsonstring)
	DWD_Unwetter_start.postUpdate(transform("JSONPATH","$start",jsonString))
	DWD_Unwetter_end.postUpdate(transform("JSONPATH","$end",jsonString))
	DWD_Unwetter_level.postUpdate(transform("JSONPATH","$level",jsonString))
	DWD_Unwetter_description.postUpdate(transform("JSONPATH","$description",jsonString))
	DWD_Unwetter_headline.postUpdate(transform("JSONPATH","$headline",jsonString))
	DWD_Unwetter_instruction.postUpdate(transform("JSONPATH","$instruction",jsonString))
	DWD_Unwetter_event.postUpdate(transform("JSONPATH","$event",jsonString))
	DWD_Unwetter_last_update.postUpdate(new DateTimeType())
end

but I got this log:

2018-06-19 11:11:01.739 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Unwetterwarnung DWD': The name 'jsonstring' cannot be resolved to an item or type; line 18, column 48, length 10

@vzorglub Do you have any ideas?


(Vincent Regaud) #17

jsonString = transform("JS", "getDWDjson.js", jsonstring)

jsonString with a capital S at String


(Stefan) #18

now no failure log, but I got for all Items:

2018-06-19 13:02:00.359 [vent.ItemStateChangedEvent] - DWD_Unwetter_event changed from warnWetter.loadWarnings({"time":1529405406000,"warnings":{"501000008":[{"regionName":"Östlich Rügen","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"113003000":[{"regionName":"Hansestadt Rostock","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"913073002":[{"regionName":"Kreis Vorpommern-Rügen - Küste","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"913073003":[{"regionName":"Kreis Vorpommern-Rügen - Insel Rügen","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"501000006":[{"regionName":"Flensburg bis Fehmarn","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"501000007":[{"regionName":"Östlich Fehmarn bis Rügen","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"913072002":[{"regionName":"Kreis Rostock - Küste","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"901055002":[{"regionName":"Kreis Ostholstein - Küste","start":1529370000000,"end":1529424000000,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Es treten Windböen mit Geschwindigkeiten um 55 km/h (15m/s, 30kn, Bft 7) anfangs aus südwestlicher, später aus westlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}]},"vorabInformation":{},"copyright":"Copyright Deutscher Wetterdienst"}); to warnWetter.loadWarnings({"time":1529406071000,"warnings":{"501000008":[{"regionName":"Östlich Rügen","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"113003000":[{"regionName":"Hansestadt Rostock","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"913073002":[{"regionName":"Kreis Vorpommern-Rügen - Küste","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"913073003":[{"regionName":"Kreis Vorpommern-Rügen - Insel Rügen","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"501000006":[{"regionName":"Flensburg bis Fehmarn","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"501000007":[{"regionName":"Östlich Fehmarn bis Rügen","start":1529350320000,"end":null,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Südwest bis West 5 bis 6, dabei Böen von 7 Beaufort.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche Warnung des Seewetterdienstes Hamburg vor STARKWIND","instruction":"","event":"STARKWIND"}],"913072002":[{"regionName":"Kreis Rostock - Küste","start":1529388000000,"end":1529431200000,"type":1,"state":"Mecklenburg-Vorpommern","level":2,"stateShort":"MV","description":"Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) anfangs aus südwestlicher, später aus nordwestlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}],"901055002":[{"regionName":"Kreis Ostholstein - Küste","start":1529370000000,"end":1529424000000,"type":1,"state":"Schleswig-Holstein","level":2,"stateShort":"SH","description":"Es treten Windböen mit Geschwindigkeiten um 55 km/h (15m/s, 30kn, Bft 7) anfangs aus südwestlicher, später aus westlicher Richtung auf.","altitudeStart":null,"altitudeEnd":null,"headline":"Amtliche WARNUNG vor WINDBÖEN","instruction":"","event":"WINDBÖEN"}]},"vorabInformation":{},"copyright":"Copyright Deutscher Wetterdienst"});

It look like thef ull warnings.json


(Vincent Regaud) #19
rule "Unwetterwarnung DWD"
when
   Time cron "0 * * ? * *"     
then
	var String jsonString = sendHttpGetRequest("https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json")
	jsonString = transform("JS", "getDWDjson.js", jsonstring)
	DWD_Unwetter_start.postUpdate(transform("JSONPATH","$.start",jsonString))
	DWD_Unwetter_end.postUpdate(transform("JSONPATH","$.end",jsonString))
	DWD_Unwetter_level.postUpdate(transform("JSONPATH","$.level",jsonString))
	DWD_Unwetter_description.postUpdate(transform("JSONPATH","$.description",jsonString))
	DWD_Unwetter_headline.postUpdate(transform("JSONPATH","$.headline",jsonString))
	DWD_Unwetter_instruction.postUpdate(transform("JSONPATH","$.instruction",jsonString))
	DWD_Unwetter_event.postUpdate(transform("JSONPATH","$.event",jsonString))
	DWD_Unwetter_last_update.postUpdate(new DateTimeType())
end

(Stefan) #20

same result; not work