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

Define automation platform. HA is home automation platform, while platform is package consisting multiple pieces of software to provide complete solution.
IMO strongest part of HA is ability to integrate uncountable number of devices from different brands. Scripting/automation is one of weakest areas IMO.

If you see HA as a single app (ha core), wanting it to be a monolith, then you are limiting its functionality (not speaking about bad architectural choice for such scale of operation)

NR was my reaction on question for alternative to current automation integration, since NR is more mature, powerful and user friendly. It wasnā€™t meant as replacement to automation GUI
thatā€™s all.

To make it Clear, I donā€™t want thatt HASS moves to someting like node Red :wink:
It just was an example how people solve their problems without using Jinja.

I know many people who are using Homeassistant, nobody of them likes to build things with jinja.
Are they all to lazy? Or to stupid?

Maybe the problem is also a lack in documentation? Maybe we need more tutorials?
I can confirm, the documentation was getting much better over the last years.
Also the GUI is MUCH better now, especially for automations itā€™s mostly not needed to use YAML/Jiinja.

I think itā€™s hard to understand the correlations between a template and a template-sensor (and so on) and how you bring these together.

Donā€™t forget: Not only IT-Nerds are using Homeassistant !

In that list, which of those try to shoehorn YAML into defining a programming language (automations)? The whole list seems to be examples of companies/projects which are using YAML to define configuratations. A purpose which it seems perfectly suited for.

From what I have seen and experienced, most of the complaints stem from having to use YAML structures for something the markup language was definitely not designed for.

This was in a way band-aided by allowing Jinja. Which in some ways makes it even more difficult to get a grasp on.

1 Like

It was not bandaided, this was designed this way from the beginning. Jinja and yaml go hand in hand, itā€™s common for automations in IT. See Ansible.

I think thatā€™s an euphemism for ā€œthis is reality, sonā€.
(Been there a few times myself).

No in a separate post there was there was 2 conflicting sides that kept rehashing the same arguments, so I asked people to stop.

The word you want is ā€œtooā€, not ā€œtoā€.

They sound the same but donā€™t mean the same thing.

As posted before, Google use yaml also in their automations.

Then all I can say is that Iā€™m glad Iā€™ve never touched Ansible in my career, and now know to avoid it. :wink:

Itā€™s really not hard. Iā€™m self taught without experience using it before HA. Took maybe a month to pick up at most knowing a prior language like Python or C#.

Yaml is also easy if you know json.

Well, from my experience NR is far from being ā€œuser friendlyā€. When i tried it as a total noob i quickly hit the wall ā€œwhat the heck to do nowā€ā€¦ (iā€™m talking about somewhat bigger projects, not only one line of codeā€¦)

Hmā€¦ thatā€™s pretty brave thing to say. I strongly doubt that you know so many HA users (say a few (tens of) thousands) to claim this to be a general opinionā€¦

True, but using HA for a while forces you to become one. Not becoming one sooner or later results in problems. Letā€™s face it: HA is alive with all frequent updates, itā€™s developing, extending and as such requires constant maintaining. My sister often says ā€œyou work with HA so muchā€¦i bet someone without knowledge canā€™t have HA automationā€; and sheā€™s totally correct.

For ā€œset-and-forgetā€ system itā€™s better to use a ready-made systems (like Siemens, Boschā€¦god know what elseā€¦). Of course they all come with the priceā€¦

Iā€™m tempted to send my brother-in-law (an actual IT Nerd) a link to this thread and have him chime in on how much, if any, Jinja2 heā€™s felt the need to use in his home automations when using Home Assistant.


All languages are hard to learn if you donā€™t already know something about them. They all have rules and you have to follow them to be understood.

The Jinja docs says this about it:

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document.

Jinja, like most programming languages use non-alphanumeric characters and punctuation to indicate structure and doesnā€™t care about blank space as much.

YAML says this about itself

What It Is:
      YAML is a human-friendly data serialization
      language for all programming languages.

YAML uses blank space to structure things. This makes the look of the text follow the structure, which also means you have to get it exactly right. This is hard to do, unless you are using an editor that helps you with that.

Years ago when I was looking at what to use for settings/configuration for a project, I discarded YAML beacuse blank space is SO important and also SO hard to do without proper editor support. I ended up using JSON, which I thought did a good job of balancing the many requirements with ease of use. Years later after spending much time with Python, I have come to appreciate the value of using blank space to indicate structure. It forces you to write configs that show the structure, but letā€™s you do it in fewer vertical lines than the equivalent JSON.

Like most things, you have to learn the tools to get the results you want. Some tools are easy to use (probably because you already learned how to use them someplace/time else). Some tools are easy to remember how to use, once you learn them (like riding a bike). I donā€™t use Jinja enough to remember the syntax and therefore have to look at examples to figure out how to express what I want. If there is not example that is close, this can be hard, but I donā€™t believe that it is Jinja that is making it hard.

YAML is great for simple things. It is much less great for very complicated things and understanding the rules can be challenging when you need to use YAML to hold something that needs to use the special characters that YAML uses.

Here is an example:

I have a presence sensor in my bathroom and I want to use it to make my bathroom floor heater turn off when I leave. This sounds simple and it should be, but it really isnā€™t. It takes the floor a long time to heat up, so while I can turn it off when I leave, turning it on when I arrive wonā€™t have much impact (since it takes tens of minutes to change the temperature much). But, letā€™s just use the simple thing of changing the thermostat set point when I leave.

The UI has a decent WHEN and it gives me a friendly GUI way of selecting the sensor I want and that it has stopped detecting motion. That was easy. I didnā€™t need any And if for this one. That just leaves Then do. For this I had to figure out Jinja templates and I got this:

service: climate.set_temperature
metadata: {}
data:
  temperature: "{{ states('input_number.floor_current_vacant_temp') | int }}"
target:
  device_id: e61a8edb3abd68cacbfd9de928199ff2

Jinja uses delimiters to indicate which things Jinja should process and which things it should leave alone. Since it is a templating engine, this makes sense (assuming you know what the purpose of a templating engine is). It is also a very powerful templating engine and closely aligned with Python, which is likely why it is used in HA (which is also closely aligned with Python). Note that it appears that Jinja was not designed to be used by someone who knows nothing about programming concepts. I also personally find the documentation on the Jinja site to be much less than easily understandable. The documentation in HA for Jinja2 and templates is equally dense and hard to understand (unless you already know it).

So, for me, I think the real problem is not so much Jinja2 but rather there is very little good documentation on using Jinja2 to achive actual results. Tutorials that go beyond the ā€œhello worldā€ and into more real life examples are needed. I searched for and found examples on the internet (some in the forums here) that helped me get a basic enough understanding, but I also recall it being hard and requiring a large investment in time.

I started using HA because it does allow some more complicated automations and supports a lot of different systems, but it does require some effort to get it all working.

I guess all programming languages are hard when you first start. Iā€™m by no means a real coder but Iā€™ve come across and used many computer languages in my carreer as an experimental physicist. Pascal, Labview, Matlab, Python, HTML, and a few more Iā€™ve had to learn by trying stuff out, trawling through help files and studying examples. I find Jinja probably the most unintuitive of those, and YAML the most lenghty one, in the sense that you have to use so many words to write a simple statement like an IF, that it makes it hard for me to keep a decent overview.
Thereā€™s apparently a pyscript HACS addon that lets you write automations in Python. Iā€™m tempted to try it out.

1 Like

Try telling that to my almost 400 automations and 125 scripts.

just because you are weak at creating HA automations doesnā€™t mean HA is inherently weak.

Doesnā€™t literally everything?

of course. no one is born knowing how Python or C++ or Basic or etc work. you have to learn them. yaml and jinja are no different.

so

if:
  condition:...
then:
  action:...

is too many words?

can you give an example of a smaller if-then syntax used elsewhere that doesnā€™t use their own special characters as shorthand?

and again I think we are all still waiting for concrete suggestions to fix the problem (aside from forcing us to use NR which I really dislike but if you like it then thatā€™s awesome for you so use it).

every time we get a ā€œyaml or jinja is too hardā€ no one ever suggests an easier way of doing the same complex things.

EFP

and in this case Iā€™ll even leave out the F.

Please, please provide an example of something that is easy with no learning curve at all and simply perfectly intuitive that can do all of the stuff we can use yaml and/or jinja to do in HA?

You want an example?
Matlab if statement:

if x.state>1, send("message"); end

More complex statements can be put on multiple lines if you prefer that visibly.
YAML if statement

if:
 - condition:numeric state
   entity_id: x
   above: 1
then:
 - action: send
   data: "message"

I donā€™t have a suggestion for a better language to use because I do not know that many computer languages and donā€™t have deep enough understanding how the backend works (I know it is in Python, thatā€™s about it) and what power the frontend language needs to have to communicate with it. I do know I prefer programming in Python to programming in YAML/Jinja, and Iā€™ve spent about the same amount of time learning them (very different use cases though). Iā€™'l try this Pyscript thing for a couple of months and see if I find it a good alternative.

I think youā€™re way overthinking things here. As far as I can tell, the reason theyā€™re using YAML and Jinja2 is because they donā€™t need a lot of actual programming done; they need to tell the back-end automation engine what to do with the devices each integration provides.

1 Like