Template sensor - unit_of_measurement dynamically

I am templating the Yr-weather wind sensor using a arrow for displaying wind direction.

YR

In the unit_of_measurement attribute, I want to set the abbreviation value N/NNE/NE/ENE/E etc. This atribute does not have a “_template”.

Any way of making the unit_of_measurement work?

homeassistant:
      customize:
        sensor.ha_update_available:
          state_card_mode: badges
          templates:
            unit_of_measurement: ${entities['sensor.ha_latest_version'].state}
            theme: >
              if (state = 'Yes') return 'green_badge'; else return 'blue_badge';

wind

Closing in, but not there. Is this Java or Python code? (Does not really matter since I can’t both).
What am I missing?

sensor.weather_wind_direction2:
templates:
unit_of_measurement: ${entities[‘sensor.weather_wind_direction2’].state}
theme: >
if (337.51 < state < 22.50)
return ‘N’
elif (22.51 < state < 67.50)
return ‘NE’
elif (67.51 < state < 112.50)
return ‘E’
elif (112.51 < statet < 157.50)
return ‘SE’
elif (157.51 < state < 202.50)
return ‘S’
elif (202.51 < state < 247.50)
return ‘SW’
elif (247.51 < state < 292.50)
return ‘W’
elif (292.51 < state < 337.50)
return ‘NW’
endif;

You are mixing the 2 up…

Theming is for the badge style, the UoM template is for the value of the UoM.

You’d have to create another template there to show in the badge-label

Aha, I see.

So, there is no way of making a logic to populate the unit_of_measurement. I have to get the state value from a sensor.

Is there a way to put the state value from the logic of sensor1 into sensor2?

  • platform: template
    sensors:
    weather_wind_direction2:
    friendly_name: “Weather Wind direction”
    value_template: ‘{{ states.sensor.weather_wind_direction.state }}’
    entity_picture_template: >-
    {% if 337.51 < states(‘sensor.weather_wind_direction’)|float < 22.50 %}
    /local/WindDirection/N.png
    SET states.sensor2.state = S

i guess you can play around with that value in a template… there’s no end to it, just try the templates in the template dev-tools and see what happens.

suppose you could create an intermediate señor with a value_template of the wind directions, and use the state of that sensor in the UoM.

Keep it modular and maintainable .

doesnt the sensor.weather_wind_direction2’ give you that already? if so it should just work like that , no?

wind

Got it. Works fine. Thanks so much!

I´ll publish the code after refining it a bit.

2 Likes

Would you mind sharing how you did it in the end? I’m struggling with precisely the same issue right now.

Thanks in advance!

This isn’t working for me. Do I need something special, like custom UI?

I haven’t gotten around to make the 32 pictures for the wind compass jet. – At least decided not to make 360 pixs. Ill post it when done,

The code so far, based on the YR-sensor:

In the file custom.yaml (you need to install customUI).

 sensor.wind_compass:
   templates:
     unit_of_measurement: ${entities['sensor.wind_compass_abbreviation'].state}

In the file sensor.yaml:

  - platform: template
    sensors: 
      wind_compass_abbreviation:
        value_template: >-
          {% if 354.38 <= states('sensor.weather_wind_direction')|float <= 5.62 %}
             N
          {% elif 5.63 <= states('sensor.weather_wind_direction')|float <= 16.87 %}
             NbE
          {% elif 16.88 <= states('sensor.weather_wind_direction')|float <= 28.12 %}
             NNE
          {% elif 28.13 <= states('sensor.weather_wind_direction')|float <= 39.37 %}
             NEbN
          {% elif 39.38 <= states('sensor.weather_wind_direction')|float <= 50.62 %}
             NE
          {% elif 50.63 <= states('sensor.weather_wind_direction')|float <= 61.87 %}
             NEbE
          {% elif 61.88 <= states('sensor.weather_wind_direction')|float <= 73.12 %}
             ENE
          {% elif 73.13 <= states('sensor.weather_wind_direction')|float <= 84.37 %}
             EbN
          {% elif 84.38 <= states('sensor.weather_wind_direction')|float <= 95.62 %}
             E
          {% elif 95.63 <= states('sensor.weather_wind_direction')|float <= 106.87 %}
             EbS
          {% elif 106.88 <= states('sensor.weather_wind_direction')|float <= 118.12 %}
             ESE
          {% elif 118.13 <= states('sensor.weather_wind_direction')|float <= 129.37 %}
             SEbE
          {% elif 129.38 <= states('sensor.weather_wind_direction')|float <= 140.62 %}
             SE
          {% elif 140.63 <= states('sensor.weather_wind_direction')|float <= 151.87 %}
             SEbS
          {% elif 151.88 <= states('sensor.weather_wind_direction')|float <= 163.12 %}
             SSE
          {% elif 163.13 <= states('sensor.weather_wind_direction')|float <= 174.37 %}
             SbE
          {% elif 174.38 <= states('sensor.weather_wind_direction')|float <= 185.62 %}
             S
          {% elif 185.63 <= states('sensor.weather_wind_direction')|float <= 196.87 %}
             SbW
          {% elif 196.88 <= states('sensor.weather_wind_direction')|float <= 208.12 %}
             SSW
          {% elif 208.13 <= states('sensor.weather_wind_direction')|float <= 219.37 %}
             SWbS
          {% elif 219.38 <= states('sensor.weather_wind_direction')|float <= 230.62 %}
             SW
          {% elif 230.63 <= states('sensor.weather_wind_direction')|float <= 241.87 %}
             SWbW
          {% elif 241.88 <= states('sensor.weather_wind_direction')|float <= 253.12 %}
             WSW
          {% elif 253.13 <= states('sensor.weather_wind_direction')|float <= 264.37 %}
             WbS
          {% elif 264.38 <= states('sensor.weather_wind_direction')|float <= 275.62 %}
             W
          {% elif 275.63 <= states('sensor.weather_wind_direction')|float <= 286.87 %}
             WbN
          {% elif 286.88 <= states('sensor.weather_wind_direction')|float <= 298.12 %}
             WNW
          {% elif 298.13 <= states('sensor.weather_wind_direction')|float <= 309.37 %}
             NWbW
          {% elif 309.38 <= states('sensor.weather_wind_direction')|float <= 320.62 %}
             NW
          {% elif 320.63 <= states('sensor.weather_wind_direction')|float <= 331.87 %}
             NWbN
          {% elif 331.88 <= states('sensor.weather_wind_direction')|float <= 343.12 %}
             NNW
          {% elif 343.13 <= states('sensor.weather_wind_direction')|float <= 354.37 %}
             NbW
          {% endif %}

      wind_compass:
        friendly_name: "Weather Wind direction"
        value_template: '{{ states.sensor.weather_wind_direction.state }}'
        #unit_of_measurement: 'degrees'
        entity_picture_template: >-
          {% if 337.51 < states('sensor.weather_wind_direction')|float < 22.50 %}
             /local/WindDirection/N.png
          {% elif 22.51 < states('sensor.weather_wind_direction')|float < 67.50 %}
             /local/WindDirection/NE.png
          {% elif 67.51 < states('sensor.weather_wind_direction')|float < 112.50 %}
             /local/WindDirection/E.png
          {% elif 112.51 < states('sensor.weather_wind_direction')|float < 157.50 %}
             /local/WindDirection/SE.png
          {% elif 157.51 < states('sensor.weather_wind_direction')|float < 202.50 %}
             /local/WindDirection/S.png
          {% elif 202.51 < states('sensor.weather_wind_direction')|float < 247.50 %}
             /local/WindDirection/SW.png
          {% elif 247.51 < states('sensor.weather_wind_direction')|float < 292.50 %}
            /local/WindDirection/W.png
          {% elif 292.51 < states('sensor.weather_wind_direction')|float < 337.50 %}
             /local/WindDirection/NW.png
          {% endif %}
2 Likes

Looks great! I’d love to see this integrated into the Custom Weather Card.

I’ll give this some thought

Installing customUI finally did the trick for me, thanks for your detailed answer!

you shouldn’t post the same issue twice, I was helping you in the other thread…

Did you post these Badge png’s anywhere?
I would really like to use them…
Right now my Badge is Blank :frowning:

image

Try Google?

https://visualpharm.com/free-icons/south%20south%20west-595b40b85ba036ed117dbb1d

Here you go:

1 Like

Thank you !
a few more icons than your code above uses !
I edited the path and file name for your original N.png to 0.png etc…
but for some reason I still get a blank badge.
I’ll keep playing.