Jinja Custom Template
Referring page: Dynamic Weather Dashboard
This is the Jinja code called from the UI to make icons, labels, and other page elements more dynamic. It is saved as a custom template file in Home Assistant to reduce code repetition and to improve maintainability. The file needs to be save to the config/custom_templates folder with a .jinja extension. More info on using custom templates is available here.
{% macro icon_weather_type(entity_id) %}
{% set sky = states(entity_id) %}
{% if sky == 'clear-night' %}
mdi:weather-night
{% elif sky == 'cloudy' %}
mdi:weather-cloudy
{% elif sky == 'fog' %}
mdi:weather-fog
{% elif sky == 'hail' %}
mdi:weather-hail
{% elif sky == 'lightning' %}
mdi:weather-lightning
{% elif sky == 'lightning-rainy' %}
mdi:weather-lightning-rainy
{% elif sky == 'partlycloudy' %}
mdi:weather-partly-cloudy
{% elif sky == 'pouring' %}
mdi:weather-pouring
{% elif sky == 'rainy' %}
mdi:weather-rainy
{% elif sky == 'snowy' %}
mdi:weather-snowy
{% elif sky == 'snowy-rainy' %}
mdi:weather-snowy-rainy
{% elif sky == 'sunny' %}
mdi:weather-sunny
{% elif sky == 'windy' %}
mdi:weather-windy
{% elif sky == 'windy-variant' %}
mdi:weather-windy-variant
{% else %}
mdi:weather-cloudy-alert
{% endif %}
{% endmacro %}
{% macro icon_color_temp(entity_id, attrib=None) %}
{% if attrib is not none %}
{% set temp = state_attr(entity_id, attrib) | float() %}
{% else %}
{% set temp = states(entity_id) | float() %}
{% endif %}
{% if temp < 28 %}
#3C0ADA
{% elif temp < 30 %}
#2E0ADA
{% elif temp < 32 %}
#200ADA
{% elif temp < 34 %}
#120ADA
{% elif temp < 36 %}
#0A0FDA
{% elif temp < 38 %}
#0A1DDA
{% elif temp < 40 %}
#092BDB
{% elif temp < 42 %}
#0938DB
{% elif temp < 44 %}
#0946DB
{% elif temp < 46 %}
#0954DB
{% elif temp < 48 %}
#0962DB
{% elif temp < 50 %}
#0970DB
{% elif temp < 52 %}
#097EDB
{% elif temp < 54 %}
#098CDB
{% elif temp < 56 %}
#099ADB
{% elif temp < 58 %}
#08A8DC
{% elif temp < 60 %}
#03D1FA
{% elif temp < 62 %}
#05FAEB
{% elif temp < 64 %}
#07F9B3
{% elif temp < 66 %}
#09F87C
{% elif temp < 68 %}
#0BF747
{% elif temp < 70 %}
#0DF713
{% elif temp < 72 %}
#3EF60F
{% elif temp < 74 %}
#73F511
{% elif temp < 76 %}
#A7F413
{% elif temp < 78 %}
#DBF415
{% elif temp < 80 %}
#ECF80C
{% elif temp < 82 %}
#F5ED0C
{% elif temp < 84 %}
#F3D80B
{% elif temp < 86 %}
#F0C40A
{% elif temp < 88 %}
#EEAF09
{% elif temp < 90 %}
#EC9B08
{% elif temp < 92 %}
#E98808
{% elif temp < 94 %}
#E77407
{% elif temp < 96 %}
#E46106
{% elif temp < 98 %}
#E24E06
{% elif temp < 100 %}
#E03B05
{% elif temp < 102 %}
#DD2804
{% elif temp < 104 %}
#DB1603
{% elif temp < 106 %}
#D80403
{% elif temp < 108 %}
#D60211
{% else %}
#D40122
{% endif %}
{% endmacro %}
{#
{% macro icon_temperature_type(entity_id) %}
{% set temp=states(entity_id) | float() %}
{% if temp >= 100 %}
mdi:sun-thermometer
{% elif temp >= 90 %}
mdi:thermometer-high
{% elif temp >= 80 %}
mdi:thermometer-plus
{% elif temp >= 65 %}
mdi:thermometer
{% elif temp <= 32 %}
mdi:snowflake-thermometer
{% else %}
mdi:thermometer-low
{% endif %}
{% endmacro %}
#}
{% macro icon_temperature_type(entity_id, attrib=None) %}
{% if attrib is not none %}
{% set temp = state_attr(entity_id, attrib) | float() %}
{% else %}
{% set temp = states(entity_id) | float() %}
{% endif %}
{% if temp >= 100 %}
mdi:sun-thermometer
{% elif temp >= 90 %}
mdi:thermometer-high
{% elif temp >= 80 %}
mdi:thermometer-plus
{% elif temp >= 65 %}
mdi:thermometer
{% elif temp <= 32 %}
mdi:snowflake-thermometer
{% else %}
mdi:thermometer-low
{% endif %}
{% endmacro %}
{% macro icon_pollen_colors(entity_id) %}
{% set temp=states(entity_id) | float() %}
{% if temp >= 9.7 %}
#FF0000
{% elif temp >= 7.3 %}
#FFA500
{% elif temp >= 4.9 %}
#FFFF00
{% elif temp >= 2.5 %}
#00CD00
{% else %}
#008000
{% endif %}
{% endmacro %}
{% macro icon_color_bft_wind(entity_id) %}
{% set wind = states(entity_id)|float(0) %}
{% if wind <= 1 %}
grey
{% elif wind <= 3 %}
#add8e6
{% elif wind <= 7 %}
#87CEFA
{% elif wind <= 12 %}
#87ceeb
{% elif wind <= 18 %}
#0d98ba
{% elif wind <= 24 %}
#00ff00
{% elif wind <= 31 %}
#ffa500
{% elif wind <= 38 %}
#cb4154
{% elif wind <= 46 %}
#ff0000
{% elif wind <= 54 %}
#800080
{% elif wind <= 63 %}
#ee82ee
{% elif wind <= 72 %}
purple
{% else %}
purple
{% endif %}
{% endmacro %}
{% macro icon_color_bft_wave(entity_id) %}
{% set wave_height = states(entity_id)|float(0) %}
{% if wave_height == 0 %} grey
{% elif wave_height <= 0.5 %} #3366FF
{% elif wave_height <= 1 %} #33CCFF
{% elif wave_height <= 3 %} #00FFFF
{% elif wave_height <= 5 %} #33FFCC
{% elif wave_height <= 8 %} #00CC00
{% elif wave_height <= 12 %} #00FF00
{% elif wave_height <= 19 %} #FFFFCC
{% elif wave_height <= 25 %} #FFFF00
{% elif wave_height <= 32 %} #FFCC66
{% elif wave_height <= 41 %} #FF9900
{% elif wave_height <= 53 %} #FF6666
{% elif wave_height <= 72 %} #FF66FF
{% else %} purple
{% endif %}
{% endmacro %}
{% macro label_bft_wave(entity_id) %}
{% set wave_height = states(entity_id)|float(0) %}
{% if wave_height == 0 %}Bft 0, Mirror Like
{% elif wave_height <= 0.5 %}Bft 1, Water ripples
{% elif wave_height <= 1 %}Bft 2, Small wavelets
{% elif wave_height <= 3 %}Bft 3, Large wavelets
{% elif wave_height <= 5 %}Bft 4, Small Waves
{% elif wave_height <= 8 %}Bft 5, Moderate Waves
{% elif wave_height <= 12 %}Bft 6, Large Waves
{% elif wave_height <= 19 %}Bft 7, Breaking Waves Begin
{% elif wave_height <= 25 %}Bft 8, Moderate Breaking Waves
{% elif wave_height <= 32 %}Bft 9, High Breaking Waves
{% elif wave_height <= 41 %}Bft 10, Angry Sea
{% elif wave_height <= 52 %}Bft 11, Very Angry Sea
{% else %}Bft 12, Avoid the Sea
{% endif %}
{% endmacro %}
{# If/then condition not indented here, seems to indent the multiline "sky" at the end if it is#}
{% macro secondary_label_hi_lo_order(curr, high, low, fcst, hour, sol_noon_hour, coverage) %}
{% if curr == low %}
@Low, fHigh: {{ fcst }}°F
{% elif curr == high and curr >= fcst %}
@High, Low: {{ low }}°F
{% elif hour < 12 %}
Low: {{ low }}°F, fHigh: {{ fcst }}°F
{% elif hour < (sol_noon_hour + 1) %}
fHigh: {{ fcst }}°F, Low: {{ low }}°F
{% else %}
High: {{ high }}°F, Low: {{ low }}°F
{% endif %}{{ coverage }}
{% endmacro %}
{% macro sky_coverage(rain_rate, sky_cover, night) %}
{% if rain_rate > 0.01 %}Raining
{% elif sky_cover <= 0.125 and night == 0 %}Sunny
{% elif sky_cover <= 0.125 and night == 1 %}Clear
{% elif sky_cover <= 0.375 and night == 0 %}Mostly Sunny
{% elif sky_cover <= 0.375 and night == 1 %}Mostly Clear
{% elif sky_cover <= 0.75 %}Partly Cloudy
{% elif sky_cover <= 0.875 %}Mostly Cloudy
{% else %}Cloudy
{% endif %}
{% endmacro %}
{% macro wind_description(wind, gust) %}
{% if wind <= 1 %} Calm, no wind.
{% elif wind <= 3 %} Light air, gusts up to {{gust}} mph.
{% elif wind <= 7 %} Light breeze, gusts up to {{gust}} mph.
{% elif wind <= 12 %} Gentle breeze, gusts up to {{gust}} mph.
{% elif wind <= 18 %} Moderate breeze, gusts up to {{gust}} mph.
{% elif wind <= 24 %} Fresh breeze, gusts up to {{gust}} mph.
{% elif wind <= 31 %} Strong breeze, gusts up to {{gust}} mph.
{% elif wind <= 38 %} Near gale, gusts up to {{gust}} mph.
{% elif wind <= 46 %} Gale, gusts up to {{gust}} mph.
{% elif wind <= 54 %} Severe gale, gusts up to {{gust}} mph.
{% elif wind <= 63 %} Storm, gusts up to {{gust}} mph.
{% elif wind <= 72 %} Violent storm, gusts up to {{gust}} mph.
{% elif wind <= 83 %} Really violent storm, gusts up to {{gust}} mph.
{% else %} Hurricane, gusts up to {{gust}} mph.
{% endif %}
{% endmacro %}
{% macro find_nws_warning_type(search_term) %}
{% set key_words = ["Frost", "Freeze", "Cold", "Rip", "Fog", "Wind", "Flood", "Rain", "Winter", "Test"] %}
{% for word in key_words %}
{% set pattern = word | regex_replace(word, '', true) %}
{% set pattern = word + " " %}
{% if search_term == word or search_term.startswith(word + " ") or search_term.endswith(" " + word) or (pattern in search_term) %}
{{ word | trim }}
{% break %}
{% endif %}
{% endfor %}
{% endmacro %}