Number to String

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. :slight_smile:
thanks
Andrea

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

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

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

Yes we have :slight_smile:

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

Hi Andrea,

the WU-JSON is much better for automation than the XML. Please have a look on the thread:

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! :wink: it’s in the URL you provided… :wink:

another Thing unrelated. If you’d like your OH2 to speak spanish on weather, have a look here:

you can add spanish language support for the alerts and weather description.

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/…?

maybe, using a brute-force approach, I can

  1. create 12 items for all 12 alarms we can have via meteoalarm

  2. find a way to say "populate item1 if wtype_meteoalarm is “1”, item2 if is “2”, … and so on.

Does it make any sense?

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

You don’t need to, the wtype_meteoalarm_name is provided by the api!!

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?

Why do you want 12 items? Do you get 12 weather alarms in one location at any one time?

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

shall I also associate the right level of alert for each item?

maybe same approach, then I concat Alarm1 + Level1 and so on?

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

AHHAHAHAAHAHA … right :slight_smile: :slight_smile: :smile:

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.

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.

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