ariela
(Andrea Riela)
March 26, 2018, 9:18am
5
I have another question, I don’t know if you can help me.
The solution above is perfect when I have a Number and I need to convert to a String
example: “1” -> Wind
But what about when I receive in the same array 2 values?
I receve “21” and it means “2” and “1”
At the moment I’ve created the entry 21 in the map file, and I say “Fog/Wind” for example, if 2 means “Fog” and 1 “Wind”.
But I’m wondering to know if there is a better way for doing that.
thanks
Andrea
vzorglub
(Vincent Regaud)
March 26, 2018, 9:26am
6
The problem here is that “21” is not really an array. OH will interpret that as a string containing a number.
You may want to have a look at a javascript transform
ariela
(Andrea Riela)
March 26, 2018, 11:14am
7
going more specific.
attached you can find an output from Wunderground Alarms about the current situation in Huesca, Spain. We have 2 alerts, so good example.
zmw_00000.210.08094.xml (3.3 KB)
items:
/*Wunderground Meteoalarm */
Number WuAlarmsCode “meteoalarm [MAP(wu_alarms_code.map):%s]” { http="<[WuAlertsCache:10000:XSLT(wu_weather_alerts_type.xsl)]" }
Number WuAlarmsLev “level [%d] { http=”<[WuAlertsCache:10000:XSLT(wu_weather_alerts_level.xsl)]" }
in http.cfg:
configuration of the first cache item: Meteoalarm every 5 mins
WuAlertsCache.url=http://api.wunderground.com/api/xxxxxxx/alerts/q/zmw:00000.210.08094.xml
WuAlertsCache.updateInterval=300000
wu_weather_alerts_type.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform ”>
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:for-each select="//response/alerts/alert">
<xsl:value-of select="wtype_meteoalarm"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
wu_weather_alerts_level.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform ”>
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:for-each select="//response/alerts/alert">
<xsl:value-of select="level_meteoalarm"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
It works, but not as expected. When I have 2 alerts in facts I see:
WuAlarmsCode= “12” because first alert value is “1”, and second is “2”
WuAlarmsLev= “22”, because both alerts are in this case Level 2
Any clue how to solve this issue?
thanks
Andrea
vzorglub
(Vincent Regaud)
March 26, 2018, 11:17am
8
Does the weather underground provide a json result instead of xml?
The you could access each alarm separately on two items with a slightly different JSONPATH transform for each
ariela
(Andrea Riela)
March 26, 2018, 11:33am
9
Yes we have
http://api.wunderground.com/api/354d93b043833b5e/alerts/q/zmw:00000.210.08094.json
How can I access each alarm separately?
Then I need to create multiple items? A sort of
Alarm1/Level1
Alarm2/Level2
…
and they will be populated only when I’ll have multiple alarms?
Andrea
binderth
(Thomas Binder)
March 26, 2018, 11:49am
10
Hi Andrea,
the WU-JSON is much better for automation than the XML. Please have a look on the thread:
This tutorial will illustrate how I currently use the HTTP binding to get weather information from Wunderground, bypassing the various weather bindings which, if the postings on the forum are any indication, are a bit flakey and provide incomplete information.
You could apply this to other weather services, though the JSONPATH or XPATH transforms will likely be different.
Note that the snazzy weather webview that one can configure to use with the Weather Binding is not supported with this appr…
From your JSON in Huesca, you can see:
"alerts": [
{
"type": "WND",
"wtype_meteoalarm": "1",
"wtype_meteoalarm_name": "Wind",
"level_meteoalarm": "2",
"level_meteoalarm_name": "Yellow",
...
{
"type": "WIN",
"wtype_meteoalarm": "2",
"wtype_meteoalarm_name": "Snow/Ice",
"level_meteoalarm": "2",
"level_meteoalarm_name": "Yellow",
you get the type and the level in distinct attributes for both alerts WND and WIN.
PS: After settling all that, you should Change your WU API Key! it’s in the URL you provided…
binderth
(Thomas Binder)
March 26, 2018, 11:51am
11
another Thing unrelated. If you’d like your OH2 to speak spanish on weather, have a look here:
some finetuning from my side (forgive me, if that already was mentioned someplace else…)
If using Wunderground with HTTP binding, you can also get some more tuned Information for your Special Location.
Localized Content
just put /lang:XX in the API-Call, e.g.
weatherForecast.url=http://api.wunderground.com/api/7cffc35ff04afd7c/forecast/lang:FR/q/France/Paris.json
in that case, the returning texts all come in french (no need to transform them into your language).
:exclamation:note: Wundergro…
you can add spanish language support for the alerts and weather description.
ariela
(Andrea Riela)
March 26, 2018, 12:22pm
12
Thanks all for this clarification.
But how can, for example, populate item1 with alert1 and item2 with alert2, and so on?
I’ts always alerts.wtype_meteoalarm for both alerts, how to understand what is first and what is, in case, second/third/…?
ariela
(Andrea Riela)
March 26, 2018, 12:35pm
13
maybe, using a brute-force approach, I can
create 12 items for all 12 alarms we can have via meteoalarm
find a way to say "populate item1 if wtype_meteoalarm is “1”, item2 if is “2”, … and so on.
Does it make any sense?
vzorglub
(Vincent Regaud)
March 26, 2018, 12:35pm
14
In your http.cfg
weatherConditions.url=the_url_for_wu_underground_json_with_your_api_key
weatherConditions.updateInterval=360000
items file:
String Alarm1 "Alarm1 [%s]" { http="[weatherConditions:360000:JSONPATH($.alerts[0].wtype_meteoalarm_name)]" }
String Alarm2 "Alarm2 [%s]" { http="[weatherConditions:360000:JSONPATH($.alerts[1].wtype_meteoalarm_name)]" }
If there is only one alarm, the Alarm2 value will be an empty string
vzorglub
(Vincent Regaud)
March 26, 2018, 12:36pm
15
You don’t need to, the wtype_meteoalarm_name is provided by the api!!
ariela
(Andrea Riela)
March 26, 2018, 12:43pm
16
Yes, idea is
if there is 1 alarm, 1 Alarm will have a value
if there are 2 alarms, 2 Alarms will have a value
Then I will play with visibility in my sitemap, to see only the items with an alarm.
But I need to populate all 12 items, each with the right alarm.
Does it make any sense? any other clue?
vzorglub
(Vincent Regaud)
March 26, 2018, 12:45pm
17
Why do you want 12 items? Do you get 12 weather alarms in one location at any one time?
ariela
(Andrea Riela)
March 26, 2018, 12:47pm
18
No, correct @vzorglub
with your approach $.alerts[0] (the number in brackets is the first of the array, right?) we don’t need to have more than 3-4 alerts for sure
ariela
(Andrea Riela)
March 26, 2018, 12:48pm
19
shall I also associate the right level of alert for each item?
maybe same approach, then I concat Alarm1 + Level1 and so on?
vzorglub
(Vincent Regaud)
March 26, 2018, 12:50pm
20
Correct, unless you live in a place with a hell of a weather you will never have more than two or three weather alerts at any one time. [0] is the first alert, [1] is the second…
If you paste your json in jsonpath.com , you can play with the jsonpath expression to get whatever value you want and assign it with an item.
2 Likes
binderth
(Thomas Binder)
March 26, 2018, 12:52pm
22
https://www.wunderground.com/weather/api/d/docs?d=data/alerts
in theory there could be 19 alerts at once. I would go with three to be on the safe side (on the other hand: I hope, WU will put the alerts in the API sorted with priorities. so the most severe will come first.
vzorglub
(Vincent Regaud)
March 26, 2018, 12:55pm
23
The severity of the alert is also on the json, so if wu don’t put them in priority you can always create priority items and sort them that way.
ariela
(Andrea Riela)
March 26, 2018, 1:10pm
24
Mmmm the path seems not correct.
I’ve tried also { http="[WuAlertsCache:10000:JSONPATH($.response.alerts[0].wtype_meteoalarm_name)]" }
but the value of my items is always NULL
the tree between xml and json versions is quite different