Convert wind direction to compass direction

I can get wind direction from Open Weather, but it comes in degrees. Is there a way to convert that to compass direction?

Example: 180 = South

Is this something that can be done in SharpTools math?

@josh

How about using a ternary expression:

$degrees = $openWeatherDegrees
$degrees > 337 ? "NNW" : $degrees > 314 ? "NW" : $degrees > 292 "WNW" ......

Completed per this chart:

Thank you Nezmo!

Do you have an example of how this expression would set a variable?

Currently mine looks like this: $context.response.data.hourly.0.wind_deg

Something like this:

$windFormatted would be your variable that would hold the cardinal/compass direction.

That 315 should be 314.

Thanks @Nezmo - I picked up the greater than 314, but I’m getting an error upon updating the rule.

Error Parsing: False part of conditional expression expected (char 724)

$degrees = $context.response.data.hourly.0.wind_deg
$degrees > 337 ? “NNW” : $degrees > 314 ? “NW” : $degrees > 292 ? “WNW” : $degrees > 269 ? “W” : $degrees > 247 ? “WSW” : $degrees > 224 ? “SW” : $degrees > 202 ? “SSW” : $degrees > 179 ? “S” : $degrees > 157 ? “SSE” : $degrees > 134 ? “SE” : $degrees > 112 ? “ESE” : $degrees > 89 ? “S” : $degrees > 67 ? “ENE” : $degrees > 44 ? “NE” : $degrees > 22 ? “NNE” : $degrees > 0 ? “N”

Try ending the expression with:

: "N"

Instead of:

: $degrees > 0 ? "N"

Can you post the entire expression.

$degrees = $context.response.data.hourly.0.wind_deg
$degrees > 337 ? “NNW” : $degrees > 314 ? “NW” : $degrees > 292 ? “WNW” : $degrees > 269 ? “W” : $degrees > 247 ? “WSW” : $degrees > 224 ? “SW” : $degrees > 202 ? “SSW” : $degrees > 179 ? “S” : $degrees > 157 ? “SSE” : $degrees > 134 ? “SE” : $degrees > 112 ? “ESE” : $degrees > 89 ? “S” : $degrees > 67 ? “ENE” : $degrees > 44 ? “NE” : $degrees > 22 ? “NNE” : “N”

I’m confused how you are getting that evaluation error. The rule builder UI cannot evaluate an expression in line when a context variable is in play:

I’m getting the error when clicking “UPDATE” to save the rule

Got it. I’m looking.

In a JSON format editor I found, I got this: There seems to be a problem with your JSON.
Error message: Unexpected token ‘$’, “$degrees =”… is not valid JSON

Try this:

$degrees = $context.response.data.hourly.0.wind_deg

$degrees > 337 ? "NNW" : $degrees > 314 ? "NW" : $degrees > 292 ? "WNW" : $degrees > 269 ? "W" : $degrees > 247 ? "WSW" : $degrees > 224 ? "SW" : $degrees > 202 ? "SSW" : $degrees > 179 ? "S" : $degrees > 157 ? "SSE" : $degrees > 134 ? "SE" : $degrees > 112 ? "ESE" : $degrees > 89 ? "S" : $degrees > 67 ? "ENE" : $degrees > 44 ? "NE" : $degrees > 22 ? "NNE" : "N"

You had angled quotes. They have to be straight quotes.

I copied and pasted yours… Same error. Where do you get straight quotes? I only have one set on the keyboard.

Yeah, sorry, I think they get changed with the copy and paste. I can see that in my post now.

I’m on a Mac and the straight quotes are the default.

I did some Googling and:

I found an easier way on Google… (Windows) I turned them OFF permanent.

Changing them now…

Okay…

No error with straight quotes, BUT… my variable went from 259 to this:

The $windDirection variable should be defined as a string variable.

1 Like

I don’t see that as an option when creating the variable. Is it done in the rule somehow?