Wind direction template sensor

Hi,

I have a wind sensor which outputs its wind direction in degrees. This part works fine. What I want to do is create a template that translates the “degrees” into the various letters that represent the true direction of the wind.

Any ideas?

Thanks.

Quite easy. Simply replace [sensor.tidewater_wind_dir_avg_10m] (my weather stations wind direction sensor) with your wind direction sensor in the code below:

{% set winbearing = (states('sensor.tidewater_wind_dir_avg_10m')|float/45)|int|round +1 %}
{% set winddir = ['North', 'North East','East','South East','South','South West','West','North West']%}
{{ winddir[winbearing]}}

In your code: {{ winddir[winbearing] }} outputs the wind direction as plain text. Test it in the Template Editor and then incorporate into your code. Enjoy.

Disclaimer: I am not the author of this code. All credit goes to the unknown original coder.

1 Like

Thanks but where does this code go? YAML?

Does it form part of a template sensor?

Thanks.

- platform: template
  sensors:
    wind_direction:
      friendly_name: "Wind Direction"
      value_template: >-
        {% set winbearing = (states('sensor.tidewater_wind_dir_avg_10m')|float/45)|int|round +1 %}
        {% set winddir = ['North', 'North East','East','South East','South','South West','West','North West']%}
        {{ winddir[winbearing]}}

When you asked for the code to convert a number of degree to wind direction text, I assumed you were able to create the template sensor. Since the conversion was the complicated part, that’s what I posted for you. Here is the complete code. Cut and Paste into wherever your other template sensors reside. Reload template entities. Done.

1 Like

This is what I use.

5 Likes

Hello,
I am a novice here in the forum, so sorry for simple questions.

I have adopted the template here to convert degree information into text form (N/NNE…):

  - platform: template
    sensors:
      wind_direction_txt:
      friendly_name: 'Windrichtung'
      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.wettersensoroc3_1_jeq0140841_wind_direction')|float %}
              {{ direction[((degree+11.25)/22.5)|int] }}

A test in the template editor does the trick: everything is output correctly.

But in sensors.yaml the test of the configuration fails: It checks forever and does not come to a result.

I have the following questions:

  • If I want to delete everything except digits from the input value: How do I do that?
  • Can control characters in the input be the problem at all? In the template editor everything works?

Greetings, Eckart

Looks like you might have an indentation problem.