Help templating wind from radian value to degree

Hi, need to go from apparent wind in radian to apparent wind in degree.
For this there is a formula multiply per 180 and divide per PI. That’s easy.

My problem is that the sensor can be positive or negative, hence the formula is slightly different upon + or - value

when the sensor is positive the value should be between 0 and 180 degrees (North -East - South)


{{ ( states('sensor.magnetic_wind_direction_radian')|float(0) * 180 / pi )|round(1) }}

when the sensor is negative the value should be between 180 to 360 (adding 360 does it)


{{ ( states('sensor.magnetic_wind_direction_radian')|float(0) * 180 / pi + 360)|round(1) }}
{% set val = states('sensor.magnetic_wind_direction_radian')|float(0) %}
{% set offset = 360 if val < 0 else 0 %}
{{ (val * 180 / pi + offset) | round(1) }}