I’m looking for some feedback or suggested improvements on my first foray into Jinja macros and templating, that I hope some feedback may help others as well as myself.
I’ve made multiple esp32 temperature sensors (Xiaomi) over BLE using an Atom M5Stack (see esphome project) as a Bluetooth proxy to WiFi. I use these for various automations such as making a very cheap thermostat to automate a fan heater to prevent freezing temperatures in an outdoor room.
A very common code “chunk” I use though, is colouring various icons on cards according to temperature. I started with a set of helpers, defining the temperature for a colour, and then a second helper for the RGB colour I wanted the icon. This resulted in lots of code fragments like this:
- type: custom:mushroom-template-card
entity: sensor.atom_alpha_ble_sen_1_temperature
primary: Kitchen
icon: mdi:thermometer
secondary: "{{ states('sensor.atom_alpha_ble_sen_1_temperature') }} °C"
icon_color: >-
{% if states('sensor.atom_alpha_ble_sen_1_temperature') >
states('input_number.room_temp_hot') %}
{{ states('input_text.colour_hot')}}
{% elif states('sensor.atom_alpha_ble_sen_1_temperature') >
states('input_number.room_temp_warm') %}
{{ states('input_text.colour_warm') }}
{% elif states('sensor.atom_alpha_ble_sen_1_temperature') >
states('input_number.room_temp_medium') %}
{{ states('input_text.colour_medium') }}
{% elif states('sensor.atom_alpha_ble_sen_1_temperature') >
states('input_number.room_temp_cool') %}
{{ states('input_text.colour_cool') }}
{% else %}
{{ states('input_text.colour_cold') }}
{% endif %}
Its pretty ugly and repetitive, so I created a folder called custom_templates (chmod 755) and then created a macro in a file called tools.jinja (chmod 755 again) that contained a list of temperatures / colours that I could iterate over for a given sensor:
{% macro get_colour_for_temp(entity_id) %}
{% set myTemps = {25: '#9d0208', 22: '#e85d04', 19: '#f48c06', 15: '#799db5', 5: '#00b4d8', -200: '#0000cc'} %}
{% for temp, colour in myTemps.items() %}
{% if (states(entity_id)) | float > temp | float %}
{{ colour }}
{% break %}
{% endif %}
{% endfor %}
{% endmacro %}
I now simplify the card to just being:
- type: custom:mushroom-template-card
entity: sensor.atom_alpha_ble_sen_1_temperature
primary: Kitchen
icon: mdi:thermometer
secondary: "{{ states('sensor.atom_alpha_ble_sen_1_temperature') }} °C"
icon_color: >-
{% from 'tools.jinja' import get_colour_for_temp %}
{{ get_colour_for_temp('sensor.atom_alpha_ble_sen_1_temperature') }}
So…I think that this is pretty concise and reduces a chunk of code being repeated across my configuration, however, I did like the helpers within the HA UI that allowed me to change the colours and the temperature levels without jumping to code, but hey.
So, is this an “appropriate” use of macros, or have I just wasted a day of learning? I know I’ve not defensively coded the hell out of it, but at least it works…today. I know I should probably template the sensor (in case of name changes in the future), check that the input sensor actually exists in Jinja - all recommendations welcome.
P.S. I went through learning about template sensors as well this past week, after upgrading an integration that resulted in all my sensors changing name - but that’s a whole other story.
So, for the final result, some nicely coloured chips at the top of my card and a simple thermostat for garden room: