# Convert Wind Direction (degrees) to Compass Points

Tags: #<Tag:0x00007f432df12030>

Sorry, but the old navigator has to step in.
It might be that I do not understand the used code but IMHO the used steps are wrong!

N for North would be assigned to courses from above 348,75 over 360 (which is 0 ) to 11,25
NNE from above 11,25 to 33,75 …
The steps are all 22,5 ° wide.

Jep, but you missed the tricky part I added 11.25 so north is now 0° to 22.5° (val j)

1 Like

So I was NOT understanding the code.

@Udo_Hartmann and @zobelhelas you’re indeed right, I do like the look of this solution best though. Thanks guys.

EDIT: it doesn’t work as expected though.

My amended version using the actual compass points even though my readings are integers, this still holds true. Yes I know if we get down to the final condition it could simply be an ‘else’ statement, and that all of the conditions could be one liners, but to me it’s more readable in this form.

(function(i) {
if(i >= 348.75 || i < 11.25){
return +i + ". -=- N";
} else if (i >= 11.25 && i < 33.75) {
return +i + ". -=- NNE";
} else if (i >= 33.75 && i < 56.25) {
return +i + ". -=- NE";
} else if (i >= 56.25 && i < 78.75) {
return +i + ". -=- ENE";
} else if (i >= 78.25 && i < 101.25) {
return +i + ". -=- E";
} else if (i >= 101.25 && i < 123.75) {
return +i + ". -=- ESE";
} else if (i >= 123.75 && i < 146.25) {
return +i + ". -=- SE";
} else if (i >= 146.25 && i < 168.75) {
return +i + ". -=- SSE";
} else if (i >= 168.75 && i < 191.25) {
return +i + ". -=- S";
} else if (i >= 191.25 && i < 213.75) {
return +i + ". -=- SSW";
} else if (i >= 213.75 && i < 236.25) {
return +i + ". -=- SW";
} else if (i >= 236.25 && i < 258.75) {
return +i + ". -=- WSW";
} else if (i >= 258.75 && i < 281.25) {
return +i + ". -=- W";
} else if (i >= 281.25 && i < 303.75) {
return +i + ". -=- WNW";
} else if (i >= 303.75 && i < 326.25) {
return +i + ". -=- NW";
} else if (i >= 326.25 && i < 348.75) {
return +i + ". -=- NNW";
}
})(input)

Hi all,

Just a slight pointer to the Scale Transformation which does the math for you.

Have fun.

2 Likes

Brilliant, thanks for that @cweitkamp. I was looking for a way to do that with a normal transform, but as far as I understood it, it only allows a single value either side of the =. Didn’t know about the Scale Transformation, thanks for bringing it to my attention. I’ll stick to my script for now though so I can return both the angular direction and the compass point together. But I’ll definitely use the scale transform once I’m happy with the values my weather station provides.

I think the second line should be

var j = (…

val j = (…

at least that way it works for me

My guess is you do not know the difference between a val (value) and a var (variable).

1 Like

For shure I don’t know the to much about the JS Implementation within OH rules, but my installation complains about val but not about var.
var: object; val: method

Perhaps you could show us the error message.

Happy to find this. However, having a problem with the item definitions…

I am not sure how this code works with item definitions, but it breaks my sitemap (where only frame names are shown, no items). If I remove it, the value is shown as null.

I’ve tried with and without the padding spaces, confirmed javascript transformation, confirmed string item type… what am I missing?

Oh, maybe I need to wait for an item update? It should have come through by now…

Take the <----> and <> parts out and replace with spaces, they’re tabs, that was me copying from my editor which was set to show tabs which then became part of that copy.

EDIT: I’ve now cleaned up that copy problem in the original post.

cool - will try, thanks!

Strange, it doesn’t seem to be returning a valid result:

Mqtt_WeatherStation_WindDir changed from NULL to 1969-12-31T19:04:22.000-0500

Will have to look at later, but if you have any ideas…

got it. My item didn’t like being changed to a number to string.

Make sure you have the correct transformation services enabled, I can’t remember which one is needed for this, but here’s my transform setup, it’s in the ‘OPENHAB_CFG"/openhab2/services/addons.cfg’ file.

transformation = map,jsonpath,exec,javascript,regex,scale,xpath,xslt

It is much easier to just use the Scale transformation…

1 Like

That’s what I use since April last year now.

windDir.scale

[0..11.25] = N
]11.25..33.75] = NNE
]33.75..56.25] = NE
]56.25..78.75] = ENE
]78.75..101.25] = E
]101.25..123.75] = ESE
]123.75..146.25] = SE
]146.25..168.75] = SSE
]168.75..191.25] = S
]191.25..213.75] = SSW
]213.75..236.25] = SW
]236.25..258.75] = WSW
]258.75..281.25] = W
]281.25..303.75] = WNW
]303.75..326.25] = NW
]326.25..348.75] = NNW
]348.75..360] = N
[..] = Unknown Value
NaN="N/A"

Items file

String windDir          "Wind Direction [SCALE(windDir.scale):%s]"      <wind>          (Home,Weather)  ["Data"]                {channel="mqtt:topic:mosquitto2:weather:windDir"}
String windGustDir      "Wind Gust Direction [SCALE(windDir.scale):%s]" <wind>          (Home,Weather)  ["Data"]                {channel="mqtt:topic:mosquitto2:weather:windGustDir"