Template Sensor Wind Direction


I am trying to use the below code however the sensor only outputs “N” and doesn’t seem to update despite the fact the angle sensor it references updates every second:

- platform: template
      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] }}

Any help would be appreciated.


Well, the code itself works for me (tried it in “Developer tools”).
What happens, if you add a “-” to your value_template line, like this:

value_template: >-

Thanks, the code does work however as the degree changes from the wind sensor, the value remains on “N” even with the “-” added as you have suggested.

Unsure as to what it could be…

OK, I tried it as a whole now (added sensors and all) and it works for me.
However, I used a little bit different indentation to match my other sensors definitions in “sensor.yaml”, as just copying your code didn’t pass the configuration check.
So I did it like this:

- platform: template
    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] }}

I’m not sure whether this is the reason, but as I said, it works in my HA.

1 Like

What sort of sensor did you use to create the values to change the text?

I still can’t get it to work. It shows “N” but doesn’t adjust despite the fact the sensor value is changing from 0-359

I used two different sensors to test, one helper (input_number.windtest) to simulate different values in short time response and one actual wind sensor from Weatherunderground API (I have a weatherstation there) and it worked in both cases.
I would say, that either your sensor name is incorrect (did you check what’s the actual sensor’s value by copying the exact sensor name to Developer tools, like {{states(‘sensor.wind_dir’)|float}} ?) or there’s an error with indentation, so the returned value of your sensor is “null” or “unknown” or something similar, but the “float” makes whatever value become “0.0”, hence the code always returning “N”.

If degree is 0 then the final line effectively becomes:

{{ direction[0] }}

That will generate an N.

The variable degree can be 0 if sensor.wind_dir doesn’t exist (unavailable) or has no value (unknown) because the float filter will convert unavailable/unknown to 0.

For example, I don’t have a sensor.wind_dir so degree is 0 and the template reports N.

Screenshot from 2021-04-09 11-37-11

You are right, it works however I think the issue is the speed in which the sensor is updating (every second as the meter sends out a command).

The weather meter updates via RS232 into node red via an IP to RS232 gateway. The data gets into HA and updates on the lovelace interface with no issue, however taking that value (which changes every second) and applying that to the template, seems to be the issue…

Here is my nodered flow - the “external wind direction” is updated every second - if i take this sensor and add it to lovelace, the value changes from 0-359 every second, pending where the wind is.

[{"id":"6bff04c3.1d096c","type":"csv","z":"720ab126.608a6","name":"","sep":",","hdrin":false,"hdrout":"none","multi":"one","ret":"\\n","temp":"DIR,SPEED,PRESS,TEMP,RH,DEWPOINT,VOLT","skip":"0","strings":false,"include_empty_strings":false,"include_null_values":false,"x":210,"y":300,"wires":[["68100633.dfa9c8"]]},{"id":"68100633.dfa9c8","type":"split","z":"720ab126.608a6","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":340,"y":300,"wires":[["d9c14757.617bd8"]]},{"id":"d9c14757.617bd8","type":"switch","z":"720ab126.608a6","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"DIR","vt":"str"},{"t":"eq","v":"SPEED","vt":"str"},{"t":"eq","v":"PRESS","vt":"str"},{"t":"eq","v":"TEMP","vt":"str"},{"t":"eq","v":"RH","vt":"str"},{"t":"eq","v":"DEWPOINT","vt":"str"},{"t":"eq","v":"VOLT","vt":"str"}],"checkall":"true","repair":false,"outputs":7,"x":490,"y":300,"wires":[["18ef2748.baef39"],["27d9d361.a0263c"],["51b22e6d.0ae55"],["7b7526c1.4a9d68"],["5364900b.3b29e"],["25b0ae57.beb3d2"],["68bf2d8c.1ea8d4"]]},{"id":"4c2d8b30.068334","type":"udp in","z":"720ab126.608a6","name":"","iface":"","port":"2559","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":60,"y":300,"wires":[["6bff04c3.1d096c"]]},{"id":"27d9d361.a0263c","type":"ha-entity","z":"720ab126.608a6","name":"External Wind Speed","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external wind speed"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Mph"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":740,"y":240,"wires":[[]]},{"id":"51b22e6d.0ae55","type":"ha-entity","z":"720ab126.608a6","name":"External Air Pressure","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external air pressure"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"mbar"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":740,"y":280,"wires":[[]]},{"id":"7b7526c1.4a9d68","type":"ha-entity","z":"720ab126.608a6","name":"External Temperature","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external temperature"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"°C"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":740,"y":320,"wires":[[]]},{"id":"5364900b.3b29e","type":"ha-entity","z":"720ab126.608a6","name":"External Humidity","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external humidity"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":730,"y":360,"wires":[[]]},{"id":"25b0ae57.beb3d2","type":"ha-entity","z":"720ab126.608a6","name":"External Dew Point","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external dew point"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"°C"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":730,"y":400,"wires":[[]]},{"id":"68bf2d8c.1ea8d4","type":"ha-entity","z":"720ab126.608a6","name":"External Station Voltage","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external station voltage"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"Vdc"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":750,"y":440,"wires":[[]]},{"id":"18ef2748.baef39","type":"ha-entity","z":"720ab126.608a6","name":"External Wind Direction","server":"eabe397b.2e7ab8","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"external wind direction"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":750,"y":200,"wires":[[]]},{"id":"eabe397b.2e7ab8","type":"server","name":"Home Assistant","addon":true}]

But as soon as its applied to the template, it doesn’t seem to work

What does this report in the Template Editor?

{{ states('sensor.wind_dir') }}

When you refresh the Template Editor, does the reported value always correspond to what you expect it should?

If it works in the Template Editor, but not in the Template Sensor, then we will focus our attention on the Template Sensor.

Thanks, this seems to work but there is a little “?” before the number - unsure if this means anything - it is however updating every second as expected.

That is significant. It is a non-ASCII character and explains why the float filter doesn’t recognize the value as being numeric and simply converts it to zero.

Given that your flow is responsible for parsing the received data, ideally it should be enhanced to trim away any non-ASCII characters (now that we know they can appear in the data).

Ok, can i remove this in node red or within home assistant? I think it is the start and end of the ASCII string that is causing this. Here is the raw data as it comes in from the sensor:


The “08” on the end isn’t set to output from the sensor…

It would be best if your flow removed it, to avoid passing along invalid characters to Home Assistant.

A brute force way would be to simply remove the first and/or last character(s) but only if you were certain they are always non-ASCII characters. I think that won’t work in this case because it appears to be inconsistent.

The superior, foolproof technique would be to filter the value so that all of its non-ASCII characters are removed. There are many examples on the web for doing this in JavaScript, here’s one:


Here’s a simpler way that only passes letter, numbers, and periods.

msg.payload = msg.payload.replace(/[^A-Za-z 0-9 \.]*/g, '');

Feel free to reduce the pattern to just numbers and periods (or just numbers).

1 Like

Thanks, i actually moved the ASCII along a line and placed the node ID first and ignored this in node red - now it works as expected!

Thanks very much.

1 Like
  • moved the ASCII along a line
    Moved what where?

  • placed the node ID first
    What is it and where was it?

  • ignored this in Node-Red
    Ignored what exactly?

The weather meter/sensor is configurable, and you can select how data is sent to the bus and in which order:

Initially wind direction was the first level sent, as a result of the STX as shown in the image above, this somehow ended up being sent as the “?” into home assistant before the numerical value of the wind direction. I changed the configuration of the station to send its node ID first (which now has the ? before the address (A)). I am now reading the wind direction correctly because it is the second value sent in the ASCII string (shown below as the number 314).



Understood. The root cause of the problem was, as I had mentioned, due to the presence of the non-ASCII character (STX) in sensor.wind_dir’s state value. That’s what caused float to always convert the state value to 0.

I had suggested to filter out non-ASCII characters in your flow, not knowing you had direct control over the data’s reporting format. You effectively “filtered it out” by rearranging the data’s order (to separate wind direction from STX).

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps helps other users who may have a similar problem with a state value that refuses to be converted to anything but 0.

1 Like