WTH: The heavy usage of JINJA2 in HASS is pain in the ass / HASS need a real script engine

Why does Home Assistant still use JINJA2 templates so often?

Jinja2 templates are difficult to understand, difficult to read and difficult to maintain, especially for people without a developer background.

It would make much more sense to use real languages like Python instead of templates, especially for template sensors, which is a real pain in the ass.

Jinja is great when I mix dynamic content and static content, for example on Lovelace !
But not for Scripts, not for filling own sensors dynamicaly and so on…

Hm…i’m not an experienced programmer, but i think that jinja is very easy to understand and very logical. It seems that you never tried, say, C++ - that one is harder and not so logical. I always hated it… (but i’m an old basic/bascom fan…)

2 Likes

Not sure what not logical can be in C++.
But every language has to be deterministic thus logical :wink:

In my life I touched several programming and scripting languages. From assembler, through C/C++, PHP, JS to Java and C# and Golang recently. Not speaking about sql/plpgsql which is my daily job.

It’s personal opinion but no other language is so hard to read and prone to make mistake comparing to yaml/jinja. The only one which comes to mind is bash scripting.

Also note the yaml is not a programming language. Thus might look simple and adequate for very simple tasks. But it suffers more with complexity

For OP: there is a HA platform allowing scripting in Python afaik (have no any experience with it). You can also chose NodeRed which provides visual event-driven automation as well as JavaScript.

I agree that yaml/jinja is not the best choice for scripting. The are many options the devs could have had chose from. But they did not. And now you will experience lot of pushback trying to suggest an alternative for the core

1 Like

yes it’s logical, but hard to read and hard to maintain.
You have so many characters {{ and % and “”

For example: I currently try to build a template sensor which should hold attributes which I get from a for loop.
I tried so many hours to get that work… the loop is working but writing the informations to statte attributes is very complex (I didn’t get that working).

I am not a programmer, but I already wrote small scripts with Python, Powershell and JavaScript.
with all of these langues it would be easy to realize that.

1 Like

There are three. One of which is hardly used (comments). See: What's with all the curly brackets? Jinja template delimiters and whitespace control

That’s what all C++ programmers say…you know: it’s pretty easy to fly a space shuttle once you know how to… but before that… :cold_face:
comparing to, say, Bascom (for AVR’s) it’s highly UNlogical. But this is not this topic’s theme… (BTW…bascommers and C++ guys “hate” each other anyway :rofl: )

here we go again…

:roll_eyes:

4 Likes

If you know Python then Jinja should be a walk in the park for you.

2 Likes

you seem surprised?
Maybe it’s time to start noticing that problem.

btw do we now any other system which uses yaml the same way HA does?

The problem that people try nothing and are all out of ideas is already well noted.

5 Likes

I cannot say this attitude is any helpful, Tom.

we asking community for pinpointing things they struggle with, and in turn you blame them for not trying hard enough? really?

maybe they tried, maybe even succeed. but signalizing it’s too complicated to justify initial need

5 Likes

From my experience going something “easy way” quickly turns into “hard way”. One example: HA’s UI for cards: while i love having UI i tend to use code editor more and more, since it quickly becomes easier when you want something more than just basic stuff.

The point is when you have UI for something then you do what dev’s decided it’s good for you, while programming with code you do what YOU want.

There’s a learning curve for everything. I remember my start with esp’s - i used tasmota, since it’s way more easy then esphome. But, i quickly “hit the wall” and switched to esphome. I had to learn, and i did. With that i also learned pure bascis of C… (also had to…).

I spent several hours to write realy simple scripts in jinja….
I didn‘t found any good solution to fill multiple attributes from a template sensor from inside a loop.

In orher languages this problem sounds very simple (and I am not a developer!)

Jinja is good for UI and Config files but not for task like creating a new sensor from existing data….

Did you ask for help here in the forum?

We are all happy to help if you are putting the effort in. Not just with example answers but explanations too.

Well, two things: first, there is UI for creating (really simple) scripts, so you don’t have to struggle with Jinja.
Second: my first script took hours, too. Then came second, third…. Now it takes minutes.

For simple tasks yes, jinja is OK.
But for everything more then a few lines code Jinja is crap.

  • You will break your fingers because of thousands and thousands of { and [ and %…
  • together with the correct indentation of the YAML config file it will get unreadable and you often have syntax errors (its hard to find them)
  • Jinja has many limitations … no good variable / object handling ( or it’s not documented well ??? or they won’t work with HASS) everything what is a little bit more dynamic won’t work…

I am not the only one who hates jinja and thats the reason why many people use Node Red and so on… But there should no need to do that.

Asked AI and got this:
Here are some specific examples of entities and professionals who use YAML:

  1. Companies:
  • Google: Uses YAML in Kubernetes configuration files.
  • GitHub: Uses YAML for GitHub Actions workflows.
  • Microsoft: Uses YAML in Azure DevOps for pipeline definitions.
  • Netflix: Uses YAML for various configuration management tasks.
  1. Open-Source Projects:
  • Ansible: An IT automation tool that uses YAML for writing playbooks.
  • Jekyll: A static site generator that uses YAML front matter for page metadata.
  • Home Assistant: A home automation platform that uses YAML for configuration files.
  1. Individual Professionals:
  • DevOps Engineers: Use YAML to define CI/CD pipelines and manage infrastructure.
  • System Administrators: Use YAML for configuration management with tools like Ansible.
  • Developers: Use YAML for application configurations and data serialization.
  • Data Scientists: Use YAML for experiment configurations and data sharing.

So, Home Assistant is in good company.

2 Likes

All you Need Is a good editor
Notepad++, vscode, any other of your choice
And you need patience and interest too

1 Like

I tried this “Node Red”… no, thanks, I remember when i was small i assembled cubes, Lego etc… :grinning: it’s just…. It looks like that, for small children… and even more complicated than jinja. At least for me it was. And, when it’s huge it’s even less readable than jinja with tons of boxes, lines etc…

1 Like

I didn’t ask for that. I know that yaml is widely used as configuration tool.

I was asking for pretending programing/scripting language with use of it.

BTW yaml in HA is mentioned on your list as source of configuration. no word about automations.