Change direction of wind - template sensor

Hi,

Maybe i am missing the obvious?

# Weather Meter
  - platform: template
    sensors:
     ext_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.external_wind_direction')|float %}
            {{ direction[((degree+11.25)/22.5)|int] }}
  

I am using this sensor to determine the direction of wind. The weather meter has been installed backwards (face palm) so south is north, and north is south. The above template gets a value between 0 and 360 (degree of wind).

How can i reverse the current setup, to correct the direction?

Thanks

You have two options… Rearrange your array of directions or apply offsets.

- platform: template
    sensors:
     ext_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.external_wind_direction')|float %}
          {% if degree >= 180 %}
            {% set degree = degree - 180  %}
          {% else %}
            {% set degree = degree + 180  %}
          {% endif %}
          {{ direction[((degree+11.25)/22.5)|int] }}
1 Like

This doesn’t work anymore for me.
My code (in template.yaml) is:

  - name: "tmpl_weather_wind_dir"
    unique_id: "tmpl_weather_wind_dir"
    state_class: measurement
    icon: mdi:sign-direction
    state: >
      {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
      {% set degree = states('sensor.weather_wind_bearing')|float %}
      {{ degree }}
      {{ direction[((degree+11.25)/22.5)|int] }}
    availability: "{{ states('weather.home') not in [ 'unknown', 'unavailable'] }}"

Using value_template instead of state gave me an error, preventing restart.

With this code the system restarts, but the value of the sensor remains ‘unknown’.
And the logs show these errors:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:173
Integration: Template (documentation, issues)
First occurred: 13:11:40 (1 occurrences)
Last logged: 13:11:40

TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified') while processing template 'Template("{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}")' for attribute '_attr_native_value' in entity 'sensor.tmpl_weather_wind_dir'



Logger: homeassistant.helpers.event
Source: helpers/template.py:411
First occurred: 13:11:40 (1 occurrences)
Last logged: 13:11:40

Error while processing template: Template("{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1615, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1842, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1618, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1331, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 525, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified

value_template is used in the legacy template sensor method, it is not a valid configuration variable for the current method.

Also, it would probably be better if your state and availability templates are based on the same source:

  - name: "tmpl_weather_wind_dir"
    unique_id: "tmpl_weather_wind_dir"
    state_class: measurement
    icon: mdi:sign-direction
    state: >
      {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
      {% set degree = states('sensor.weather_wind_bearing')|float(0) %}
      {{ degree }}
      {{ direction[ (( degree + 11.25) / 22.5) | int] }}
    availability: >
      {{ states('sensor.weather_wind_bearing') not in [ 'unknown', 'unavailable', '', none] }}