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…)
Not sure what not logical can be in C++.
But every language has to be deterministic thus logical
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
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.
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…
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 )
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….
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.
I tried this “Node Red”… no, thanks, I remember when i was small i assembled cubes, Lego etc… 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…