Convert wind direction to compass direction

There’s still a few typos in your last version. Some of them probably from fixing the formatting (eg. the ` at the start and end of the second line). And the E in your version still has the curly quotes. :slight_smile:

You can see the quote issue I mentioned in your screenshot:

That’s what I get for rushing.

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 ? "E" : degrees > 67 ? "ENE" : degrees > 44 ? "NE" : degrees > 22 ? "NNE" : "N"

BINGO!!!

This was tougher than the Sunday crossword puzzle…

Thank you both!

2 Likes

I’m sorry I led you astray in a few places.

Thanks to @josh as always for keeping us straight.

1 Like

Please don’t be sorry. I like a good challenge. It keeps the brain going. I always appreciate someone who sticks with it…

Want another???

I have another variable… can I use “title” in the expression to change to Upper and Lower case? Or is that CSS?

1 Like

I do it in a few Rules:

This converts the variable which is all lower case to title case.

2 Likes

This worked…


That’s enough brain work for tonight!!! :smiley:

Thanks again… I really appreciate it.

1 Like

Here’s a mathematical approach which should give you the same results as the chained conditions approach:

degree = $context.response.data.hourly.0.wind_deg
directions = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]
index = round((degree % 360) / 22.5)
directions[index % 16]

If you’re curious about the details on how this works or what the logic is…

How it works (tap to expand)

The way it works, line-by-line, is:

  1. Alias wind_deg from the response to a degree local variable
    • Just makes it easier to reference the value without typing the long name
  2. Define all the cardinal directions we are interested in (16 of them)
  3. Get an index based on which of the 16 positions it falls into
    • degree % 360 is a safety measure to make sure the value is always 0-360.
      • For example, if it was 361, this would ‘wrap’ it back to 1
    • 22.5 represents the range for each direction (360 / 16 positions = 22.5)
    • So we divide our coerced degrees by 22.5 to find the index
      (eg. which of the ‘directions’ values to grab)
  4. Get the cardinal direction based on the index
    • Again, using the modulus operator to coerce to a valid 0 based index

 

Simple Direction

Using that same logic, you could simplify things to just the cardinal and intercardinal directions if you wanted (eg. drop the NNE, ENE, etc):

degree = $context.response.data.hourly.0.wind_deg
directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
index = round((degree % 360) / 45)
directions[index % 8]

As you can see, it’s a simple update trimming down the list of directions and adjusting the numbers accordingly. (eg. we only have 8 directions now, so its 45 degrees each)

3 Likes

Nice… Overwhelming, yes… but nice.

1 Like

I like it!!! I didn’t need 16 of them… LOL

1 Like

LOL. I just wanted to provide a different option - especially if someone else comes across this thread later. :smiley:

Isn’t it interesting how each of our brains work differently! For me, the chained ternary conditions makes my brain start to scramble - I guess I start worrying about formatting one of the conditions wrong or using a ? where I should have used a : with all that nesting.

Seeing the directions as a simple list in the math version calms those nerves for me. Though I can certainly understand that the modulo operator % could trigger the same feelings for a non-programmer! Once you get comfortable with it and realize we’re basically just constraining things, it’s pretty straightforward, but I certainly understand the feeling!

But that’s the beauty of having choice! Lots of different ways to accomplish the same thing and you can pick what’s best for you!

3 Likes

Hi, I also want to convert my degree values (from an Ecowitt weather station - I already have the tile set up) into the 16 compass points, but I’ve fallen at the first hurdle: what do I have to do to create a ‘$wind’ or whatever and then how do I get to the Expression menu to enter the parameters? Sorry, but I know very little about this sort of stuff and would be grateful for an ‘idiot’s’ guide’ please.