Convert wind direction degrees to text

My wind direction sensor sends the direction in degrees: 0 is North, 45 is North-East, 90 is East and so on untill 315 for North-West. This works fine with below code in configuration.yaml

sensor Windrichting:
  - platform: mqtt
    state_topic: "Windrichting"
    name: "Windrichting"
    icon: mdi:compass-rose
    unit_of_measurement: "º"
    value_template: "{{ value_json.graden | round(0) }}"

Now I am struggling to convert the degrees to directions because I would replace the degrees in my card with text like North, North-West, East and so on. Can someone tell me how to do this?

This is what 'm using:

- platform: template
  sensors:
    weather_wind_dir:  
      friendly_name: 'Wind Direction'
      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_dir')|float %}
        {{ direction[((degree+11.25)/22.5)|int] }}
13 Likes

Thanks Gary. Unfortunately my experience is too limited to translate this to my situation, so any other help is welcome.

I’m not using mqtt, so not sure how to adapt this for your application.

When using C++ I would try something like filling an array with the 8 directions like [N,NE,E,SE,S,SW,W,NW] and select the right text with

textDirection = array[direction/45]

But I have no idea how to do this in Home Assisant :frowning:

Hi Hans,
this is exactly what Gary does in his answer. So what’s the problem with applying this to your situation? You have to create a template sensor with the code he posted and just replace the entity id “sensor.wind_dir” with the name of your sensor, probably sth like sensor.windrichtung.

Unfortunately, I have too little experience with Home Assistant to set this up properly, so I did not succeed. Because I am more experienced with NodeRED I solved it with a node: https://github.com/bartbutenaers/node-red-contrib-compass The node translates compass degrees to wind directions (text). While I use MQTT this is a perfect solution for my setup.

Thanks anyway!

This works perfectly thanks.

This worked perfectly for me. Thank you for posting this!

does it work anymore? With an Update it doesn’t work anymore for me.

I’m on the latest and it’s still working for me.

I used your example and updated for the new templates/sensor format, works great:

configuration.yaml:

template:
  - sensor: !include_dir_merge_list templates/sensors.d

templates/sensors.d/weather_derived.yaml:

- name: "GW2000C Wind Direction Name"
  unique_id: gw2000c_wind_direction_name
  state: >
    {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
    {% set degree = states('sensor.gw2000c_wind_direction')|float %}
    {{ direction[((degree+11.25)/22.5)|int] }}

Is the old format being deprecated?

Can anyone explain to me,
It worked perfectly, until I reinstalled HA, trying to embed the script again, it no longer works! (I am a beginner (I am a beginner)

This is working for me with all the latest code:

- platform: template
  sensors:
    weather_wind_dir:  
      friendly_name: 'Wind Direction'
      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_direction')|float %}
        {{ direction[((degree+11.25)/22.5)|int] }}

I use a sensors.yaml file where I place the above code.

1 Like

I create Helpers with it !
It work too !

Thank you so much !

You’re welcome. Haven’t tried helpers yet.

It’s more simple, create “Template sensor”

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

A Template Sensor Helper currently supports some but not all of what a Template Sensor can do.

In its current form, the options it offers are meant to handle the majority of basic needs. However if you want to select or template the sensor’s icon, or handle availability, or create attributes, or etc, it doesn’t support those options.

If you are already familiar with creating Template entities in YAML, there’s little advantage for you to switch to using a Template Helper.