Wind direction

dear people I recently added an anemometer to my hassio sensors. Now it indicates the degrees of the direction the wind is coming from. I actually that he also indicates the direction. north, east etc.
does anyone have an idea how I can get this done

wind

2 Likes

Create a template sensor to translate degrees to direction which will become the state of this sensor. See here https://www.home-assistant.io/integrations/template/

Here’s mine, just exchange my wind bearing sensor for your and it’ll get you close.

      weather_wind_dir:
        value_template: >
          {% set dir = states('sensor.darksky_weather_wind_bearing')|float %}
          {% if 11.25 < dir <= 33.75 %} NNE
          {% elif 33.75 < dir <= 56.25 %} NE
          {% elif 56.25 < dir <= 78.75 %} ENE
          {% elif 78.75 < dir <= 101.25 %} E
          {% elif 101.25 < dir <= 123.75 %} ESE
          {% elif 123.75 < dir <= 146.25 %} SE
          {% elif 146.25 < dir <= 168.75 %} SSE
          {% elif 168.75 < dir <= 191.25 %} S
          {% elif 191.25 < dir <= 213.75 %} SSW
          {% elif 213.75 < dir <= 236.25 %} SW
          {% elif 236.25 < dir <= 258.75 %} WSW
          {% elif 258.75 < dir <= 281.25 %} W
          {% elif 281.25 < dir <= 303.75 %} WNW
          {% elif 303.75 < dir <= 326.25 %} NW
          {% elif 326.25 < dir <= 348.75 %} NNW
          {% else %} N
          {% endif %}
8 Likes

Here’s another solution

{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
{% set degree = states('sensor.wind_bearing')|float %}
{{ direction[((degree+11.25)/22.5)|int] }}
14 Likes

can you tell me where to put this and how in my configuration.

Goes in the configuration.yaml. “your_wind_sensor” can be any name you choose and it will then appear as sensor.your_wind_sensor in the Tools/States UI. Sensor.wind_bearing is of course the name of the sensor from your weather station etc.

sensor:
  - platform: template
    sensors:
      your_wind_sensor:
        value_template: >
          {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
          {% set degree = states('sensor.wind_bearing')|float %}
          {{ direction[((degree+11.25)/22.5)|int] }}
14 Likes

yes, it’s that easy, if you know what you’re doing :slight_smile:
Could you also help me change a measure. I measure the speed of the wind in mtr / s. I actually want to convert this to km / h. Do you have any idea how I can do it.

Wind meter kmh

Same process. This would go under the same platform: template as the previous wind direction.

sensor:
  - platform: template
    sensors:
      your_wind_sensor:
        value_template: >
          blah, blah blah, from previous sensor
      your_speed_sensor:
        value_template: "{{ states('sensor.wind_speed')|float * 3.6 }}"
        unit_of_measurement: km/h
2 Likes

An innovative solution, though I think micque has the edge.
Can I ask though why are you bothering testing “33.75 < dir” ?

Not sure I understand your question.

NNE is 11.25 thru 33.75
NNW is 326.25 thru 348.75
therefore, anything ‘else’ (348.75 thru 11.25) is N

Also, I think I copied this off of someone else’s example in these forums weeks ago (sadly I forget who or I’d give them props).

Also, for me, the shortest solution isn’t always the best. From all I’ve learned in HA, most of the time find the ‘best’ / ‘shortest’ / ‘least amount of code’ solution isn’t worth the time it actually saves… :joy:

It would be shorter to test : -

  1. 326.25 < dir or dir < 11.25 for N (the only double test)
  2. elif dir < 33.75 for NNE
  3. elif dir < 56.25 for NE
  4. etc

You have already tested the lower values and failed, so just take the next that passes (only one shall pass) :rofl:

1 Like

Definitely a situation in HA where I’ve learned that it’s better not to care. I have bigger :fish: to fry in life than worrying about minutia. Plus, my NUC’s not really running anything else, so it needs the workout.

EDIT. That said, @micque’s solution is much cleaner, so I’m updating mine… :joy:

2 Likes

BTW it only took me 10 minutes to put together so it still falls into your low effort category of goodness. :wink: :grin:

1 Like

You seem to have a great understanding of the use of template in HA. Is there better document than the doc link? Not much detail there. Thanks

Just the Jinja online document which I think is what you are referring to. In general, Jinja uses python-like syntax. So if you know python you have a tremendous leg up. And well, HA is also python. If you keep that in mind then a lot of the rules and behaviors of the whole system start to make a lot more sense (for example why entity states represent numbers as text). So if there is anything you should do it’s learn python if you don’t already know it.

1 Like

I want to use SMHI:s wind bearing but i can’t get it to work . I only get N and today the wind is from S.

Is it possible to use: states.weather.smhi_klagshamn.attributes.wind_bearing?
The state write out 216 in develop tools today.

Can you please help me?

If you haven’t fixed this yet you could always submit your sensor config so we can have a look at it.

Have a look here for another simple solution:
https://community.home-assistant.io/t/wind-direction-template-sensor/297511

This worked great for me, and my Acurite weather station.
I did need to change how the sensor is called, as shown below:

{% set dir = states.sensor.pws_report.attributes.observations[0].winddir|float %}

Hi, I have tried to use this config in my config YAML …and it breaks my main weather integration for some reason that I cant work out … The code works in the template test in Dev Tools.

'sensor:

  • platform: wundergroundpws
    api_key: secret key here
    pws_id: my station here
    numeric_precision: none
    monitored_conditions:
    • temp
    • dewpt
    • heatIndex
    • winddir
    • humidity
    • weather_1d
    • weather_1n
    • weather_2d
    • weather_2n
    • weather_3d
    • weather_3n
    • weather_4d
    • weather_4n

camera:

####################################################

Sensor - Templates

####################################################

sensor:

  • platform: template
    sensors:
    your_wind_sensor:
    value_template: >
    {% set direction = [‘N’,‘NNE’,‘NE’,‘ENE’,‘E’,‘ESE’,‘SE’,‘SSE’,‘S’,‘SSW’,‘SW’,‘WSW’,‘W’,‘WNW’,‘NW’,‘NNW’,‘N’] %}
    {% set degree = states(‘sensor.wupws_winddir’)|float %}
    {{ direction[((degree+11.25)/22.5)|int] }} ’