Import for macro not working?

I am creating a number of sensors with templates. Since much of the templating code is using similiar same code over and over, I was thinking of putting most of it in a macros and then refering them. However adding “import ‘util.jinja’ as util” for example in a template results in this error in the log:

homeassistant.exceptions.TemplateError: TypeError: no loader for this environment specified

which seems to indicate to me that importing is not implemented? But this can’t be I must be missing something obvious here, surely people are not just massively duplicating Jinja code over and over in their configuration, so what exactly is the correct way to import macros?

1 Like

Maybe you can study this a bit and see it if suits your needs.

Check the include/template.yaml, custom_button_card.yaml and ui-lovelace.yaml files. He managed, so if you handle it the same way, it should work for you too.

Not sure what I should be looking in this for fixing macro import, but looking at the code it suffers from exactly what I was talking about. Massive duplication of identical code over and over again. I mean I can of course just copy/paste code the same code all over the place whenever I make any changes but surely there has to be something missing here?

You can’t import into the template environment. Use scripts or yaml anchors to reduce redundancy.

I don’t think you can use yaml anchor inside Jinja templates and I don’t want to rewrite everyting as Python scripts and start mixing and matching. So there is absolutely no code reuse for Jinja templates? Well that is very dissapointing.

No reusing outside your current scope. I.e the single template

Ayear later no change. sigh We got macros for automatons, but not for sensors or anything else that uses jinja templates. Just making the jinja import function work would solve this.

I’ve got a complicated sensor that uses a 40 line template, and the only solution is to cut-n-past it 7 times. This makes maintenance a real pain.

Making sensor macros would be a huge boon for simplifying the sharing and distribution of various sensors among users. You could even have a library just like there is for automatons now.

Make a #feature-requests

I already did more than a year ago:

And unfortunately in my experience nobody looks at any of these feature request posts at all.

Feature Requests do get implemented. Which one gets implemented depends on whether it interests someone, who has the requisite time and expertise, to volunteer to do it. Many Feature Requests (some with a large number of votes) languish unfulfilled for years simply because no one has felt the urge to implement them.

Well, you got my vote.

My vote too.

I only wish I had found this post a couple of days ago. Spent a day trying to figure out what was the problem with my sensor jinja code. The macros worked fine when tested in dev tools, I did not see a run time error (maybe it is not self evident and I do not know were to look), and in the dev tools states tab sensor attributes just disappeared / reappeared. Too many ducks to line up. :unamused:

Uh, you joking? This was added this release…despite @kristjanbjarni saying feature requests never work.

My bad :slight_smile: (kinda).

After update it works, thank you. I notice you updated the feature request to closed after my post. I checked the feature request status before posting to see if it had been fixed. It suggests that features get implemented but just maybe still the request was ignored … will never know. I put this down to documentation by community posts.

Thanks