Reusing globally defined Jinja2 templates

Hello Community,
It’s my first post here, welcome to everyone :slight_smile:
First of all thank for your brilliant work, HA is awesome I really like it.

I’m playing with my own custom sensors which are utilizing value_template and icon_template. I’ve noticed that a lot of template code repeats breaking DRY principle.
I’ve found possibility of using Jinja2 macros to make the code more modular, however I don’t know how to define macros globally on top level and the to reuse it in whole HA configuration.
Till now I just found that macros can be defined in particular template section, but it doesn’t solve my problem.

Is it possible somehow to setup macros on the top of the HA and reuse it on lower level?

You can use the same code in multiple places using includes, so long as you’re happy that the code needs to be identical in each spot.

Thanks for your reply. This is not exactly what I’m looking for. I would like to define a macro which utilise few variables and us it later in different context.

I actually have a similar need. I want to change all my icon’s on the front end to change color based on its last_changed attribute. I don’t want to write each one out.

It might also be worth looking at app daemon or a python script that will take an input. For example, you can create a shell command and send it a entity id. Then that shell command can look up that entity using HA api and return some translated value back to HA directly or by mqtt.

https://community.home-assistant.io/t/imap-email-content-message-body-seems-encrypted/