Sensor Template and statements

I have a sensor template i’m trying to make. It takes the sensor reading from sensor.weather_wind_bearing which is in degrees 0-359 I want the sensor to out put 0 = north 90 = east ect. That much works. When I get to in between parts. I need it to show if it’s between 0 and 90 it’s north east and ect. Below is my setup. I’ve look around and can’t seem to find anything on this. If there is a better thread to look at I would love to read that. Thanks

sensor:
  platform: template
   sensors:
    weather_wind_bearing:
       friendly_name: Wind direction
       value_template: >-
             {%- if is_state("sensor.weather_wind_bearing", "0") %}
                 North
             {%  elif is_state("sensor.weather_wind_bearing", "90") %}
                 East
             {%  elif is_state("sensor.weather_wind_bearing", "180") %}
                 South
             {%  elif is_state("sensor.weather_wind_bearing", "270") %}
                 West
             {%  elif states.sensor.weather_wind_bearing.state > 0 and states.sensor.weather_wind_bearing.state < 90 %}
                 North East
             {%  elif states.sensor.weather_wind_bearing.state > 90 and states.sensor.weather_wind_bearing.state < 180  %}
                 South East
             {%  elif states.sensor.weather_wind_bearing.state > 180 and states.sensor.weather_wind_bearing.state < 270  %}
                 South West
             {%  elif states.sensor.weather_wind_bearing.state > 270 and states.sensor.weather_wind_bearing.state < 360  %}
                 North West
             {% else %}
                 unknown
             {%- endif %} 

If this doesn’t work right I’m sorry Thanks for any and all help

You could include the Weather Underground sensor, which includes two methods of showing the wind direction (wind direction and wind string, which shows direction and speed). It’s not an answer to your question but it is a solution.

1 Like

It is a different route to go, if I can’t get the and statements working then I’ll have to look into weather underground, thanks for the suggestion. I still want to figure the template and statements out

I know I saw someone doing something similar when I was researching this a while back - I was thinking of doing the same thing. I’m not sure where it was though; it wasn’t in this forum. I’ll look through my history and see if I can find it and if it is of any help.

1 Like

Here’s a link to that post I was talking about; it’s on stack overflow. Not sure if it will be of any help, but thought I’d post it here anyway.

You rock thanks

So I noticed this post was referenced in another post about templating examples. So I figured I better revisit my original and see if my studying has come up fruitful. So far I got it working like a charm. My working code is below for anyone struggling like I was with this or wanting this. Thanks

# Template Sensor Wind Direction
- platform: template
  sensors:
    wind_dir:
      friendly_name: 'Wind Direction'
      value_template: >-
        {%if states.sensor.weather_wind_bearing.state | float<=11 %}North
        {% elif states.sensor.weather_wind_bearing.state | float>348 %}North
        {% elif states.sensor.weather_wind_bearing.state | float<=34 | float>11 %}North North East
        {% elif states.sensor.weather_wind_bearing.state | float<=56 | float>34 %}North East
        {% elif states.sensor.weather_wind_bearing.state | float<=79 | float>56 %}East North East
        {% elif states.sensor.weather_wind_bearing.state | float<=101 | float>79 %}East
        {% elif states.sensor.weather_wind_bearing.state | float<=124 | float>101 %}East South East
        {% elif states.sensor.weather_wind_bearing.state | float<=146 | float>124 %}South East
        {% elif states.sensor.weather_wind_bearing.state | float<=169 | float>146 %}South South East
        {% elif states.sensor.weather_wind_bearing.state | float<=191 | float>169 %}South
        {% elif states.sensor.weather_wind_bearing.state | float<=214 | float>191 %}South South West
        {% elif states.sensor.weather_wind_bearing.state | float<=236 | float>214 %}South West
        {% elif states.sensor.weather_wind_bearing.state | float<=259 | float>236 %}West South West
        {% elif states.sensor.weather_wind_bearing.state | float<=281 | float>259 %}West
        {% elif states.sensor.weather_wind_bearing.state | float<=304 | float>281 %}West North West
        {% elif states.sensor.weather_wind_bearing.state | float<=326 | float>304 %}West North West
        {% elif states.sensor.weather_wind_bearing.state | float<=348 | float>326 %}North North West
        {%- endif %}

This code will tell you in Easy to understand wind direction, numbers were taken from http://climate.umn.edu/snow_fence/components/winddirectionanddegreeswithouttable3.htm.

Thanks to all who commented and helped out,

6 Likes

I was using this and got some help in the Discord as the way the float was working was wrong. Here is a working and cleaner way of doing it.

Make sure you change “states.sensor.weather_wind_direction.state” to match your entity.

# Template Sensor Wind Direction
 - platform: template
   sensors:
     wind_dir:
       friendly_name: 'Wind Direction'
       value_template: >-
         {% set wind_dir = states.sensor.weather_wind_direction.state %}
         {% if wind_dir | float<=11 %}North
         {% elif wind_dir | float>348 %}North
         {% elif 11 < (wind_dir|float) <=34 %}North North East
         {% elif 34 < (wind_dir|float) <=56 %}North East
         {% elif 56 < (wind_dir|float) <=79 %}}East North East
         {% elif 79 < (wind_dir|float) <=101 %}East
         {% elif 101 < (wind_dir|float) <=124 %}East South East
         {% elif 124 < (wind_dir|float) <=146 %}South East
         {% elif 146 < (wind_dir|float) <=169 %}South South East
         {% elif 169 < (wind_dir|float) <=191 %}South
         {% elif 191 < (wind_dir|float) <=214 %}South South West
         {% elif 214 < (wind_dir|float) <=236 %}South West
         {% elif 236 < (wind_dir|float) <=259 %}West South West
         {% elif 259 < (wind_dir|float) <=281 %}West
         {% elif 281 < (wind_dir|float) <=304 %}West North West
         {% elif 304 < (wind_dir|float) <=326 %}West North West
         {% elif 326 < (wind_dir|float) <=348 %}North North West
         {%- endif %}
2 Likes

I think there is a duplication in the list (the second West North West should be North West).Here is my modification to correct that as well as abbreviate the longer direction headings:

  - platform: template
    sensors:
      wind_dir:
        friendly_name: 'Wind Direction'
        value_template: >-
         {% set wind_dir = states.sensor.dark_sky_wind_bearing.state %}
         {% if wind_dir | float<=11 %}North
         {% elif wind_dir | float>348 %}North
         {% elif 11 < (wind_dir|float) <=34 %}NNE
         {% elif 34 < (wind_dir|float) <=56 %}NE
         {% elif 56 < (wind_dir|float) <=79 %}}ENE
         {% elif 79 < (wind_dir|float) <=101 %}East
         {% elif 101 < (wind_dir|float) <=124 %}ESE
         {% elif 124 < (wind_dir|float) <=146 %}SE
         {% elif 146 < (wind_dir|float) <=169 %}SSE
         {% elif 169 < (wind_dir|float) <=191 %}South
         {% elif 191 < (wind_dir|float) <=214 %}SSW
         {% elif 214 < (wind_dir|float) <=236 %}SW
         {% elif 236 < (wind_dir|float) <=259 %}WSW
         {% elif 259 < (wind_dir|float) <=281 %}West
         {% elif 281 < (wind_dir|float) <=304 %}WWN
         {% elif 304 < (wind_dir|float) <=326 %}NW
         {% elif 326 < (wind_dir|float) <=348 %}NNW
         {%- endif %}
4 Likes

Ahh nice. I had a issue when I was changing it from the original template I used and must have copied a line and not fixed the result. I have been thinking about simplifying it even more. cutting it back to 4-8 directions instead of 16.

Good idea @TehRobot. Here is an updated compass template limited to only eight principal wind directions:

  - platform: template
    sensors:
      wind_dir:
        friendly_name: 'Wind Direction'
        value_template: >-
         {% set wind_dir = states.sensor.dark_sky_wind_bearing.state %}
         {% if wind_dir | float<=23 %}North
         {% elif wind_dir | float>338 %}North
         {% elif 23 < (wind_dir|float) <=68 %}NE
         {% elif 68 < (wind_dir|float) <=113 %}East
         {% elif 113 < (wind_dir|float) <=158 %}SE
         {% elif 158 < (wind_dir|float) <=203 %}South
         {% elif 203 < (wind_dir|float) <=248 %}SW
         {% elif 248 < (wind_dir|float) <=293 %}West
         {% elif 293 < (wind_dir|float) <=338 %}NW
         {%- endif %}
2 Likes

hello

how can i add the template sensor to a group?

thx, i know this. but what is the correct entities?

You can look at http://<server>:8123/dev-state but from the above my guess is

- sensor.wind_dir
2 Likes

Maybe something in here can help