does this still work, or need to be changed?
Still works for me.
boom just found this and works a treat!
Sorry a newbie here.
Please tell me which step I am doing wrong and what I should do
- create a helper called âsensor.wind_dirâ
- Paste the code listed above in the state template field and it objects
Which field am I supposed to paste the code into please?
Tony
If you havenât already, create a sensors.yaml file in your /config directory and add the following 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] }}
If sensors.yaml is new to your configuration, add this line to configuration.yaml:
sensor: !include sensors.yaml
You could add the sensor directly to your configuration.yaml file if youâf prefer, but the syntax would change slightly. I donât use the helpers yet. This is working for me on HA 2024.12.0.
When setting this up as a Helper:
The âState Templateâ field only accepts Jinja templates, use
{% 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] }}
Make sure to use the entity ID of your sensor if it isnât sensor.wind_direction.
Leave all the other fields empty:
Thanks @GaryK, this was the step I was missing. Works like a bought one!
Hi All!
I am trying to do the same thing, convert compass degrees to compass headings, but in an automation. I am using the Flightradar24 add on. In the automation trigger.event.data.heading returns the compass degrees heading of the aircraft for a flight entry notification.
I have added the following to a variable section in the Then do section of the automation:
variables:
direction: "{{ ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] }}"
degree: "{{ states(trigger.event.data.heading) | float }}"
compass_heading: "{{ direction[((degree+11.25)/22.5) | int] }}"
enabled: true
Using the âdirectionâ variable in my automationâs notification returns the error:
Error: Error rendering data template: AttributeError: âintâ object has no attribute âlowerâ
I think I am close but I do not understand the syntax for the direction variable to know what it is doing.
Thanks for any advice you can give!
The variable compass_heading is what outputs the final value⌠direction is an list of values ordered in such a way that the math in the template for compass_heading can select the correct value via list index.
But, you also have an error. If âtrigger.event.data.heading returns the compass degreesâ then you need to use that on its own when defining the degree variable⌠the states() function expects an entity ID as itâs primary argument:
variables:
direction: "{{ ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] }}"
degree: "{{ trigger.event.data.heading | float }}"
compass_heading: "{{ direction[((degree+11.25)/22.5) | int] }}"
enabled: true
Ah, yes of course I kept removing the variables and use the trigger data so my automatons would still work. Looks like I put the wrong one in, as compass_heading is the correct one.
Thanks or pointing out my mistake with the states(). I took that over from the original code in this post without thinking too much about it.
Thanks again! After making the changes you suggested my automation are now working with compass headings.
Thanks for this idea, i did bump into an issue. When the wind degrees is 360. It returns 17 as int. But this is not in the list unless we add another âNâ. But thats awkward since there already 2 now
I first used this method, but it was always kind of by one direction
for (var count = 0; count < windDir.length; count++) {
j += 22.5;
// console.log("Count: %s - Deg: %s - Windir: %s - i: %s - j: %s",count,_Deg,windDir[count],i,j)
// console.log("count %s",count)
// console.log("_deg >= %s _deg < %s",i, j)
if (_Deg >= i && _Deg < j) {
// console.log("==== YES ====")
// console.log("count %s",count)
// console.log(windDir[count])
return windDir[count];
} else if (_Deg >= i && _Deg == j) {
return windDir[count];
}
i += 22.5;
}
I use it for this simple wind check thing i made using data from OpenWeather
https://www.promotiespullen.com/windcheck

