Automation with real code (not YAML)

Is there a way, to write complex automations with real code? Like JavaScrip, Python, Jave, etc? Or only solution will be to write custom integration for that?

I think all programmers will agree that abilities what can be done with YAML are pretty limited.

2 Likes

Firstly, this is not a feature request - itā€™s a question.

And to answer your question, thereā€™s plenty!

3 Likes

So weā€™ve had this before and yaml (with templating) is super flexible.
I have heating that modulates according to time of day, if doors are open or closed (ditto windows) if we are outside a certain radius from home etc.
We have music that comes on via a schedule, switches off if we are out or if the TV is on
We have lights that follow a brightness profile but can be overridden at the (simple) switch to maximum or minimum brightness from the current profile.
Assembler is pretty basic but you can write anything in it.
Yaml is native to HA, it doesnā€™t need extra layers of software, so actually runs a lot faster than node red or even appdaemon AND never causes as many upgrade problems any ā€˜addonā€™ inevitably runs into at some point

Give me an example of what you are attempting and if you say a ā€œspreadsheetā€ Iā€™ll just walk away.

And No they donā€™t
High Level languages are good at High level things (manipulating arrays, setting visual objects and the like) We are talking about real time control systems (well as close as you can get on a raspberry Pi) and converting code as close to the objects as possible into appropriate listeners and response sets
(hence why yaml is often quicker)

Besides what is REAL code ?

I can certainly understand the reluctance to lean Yet Another Programming Language. I can even understand wanting to leverage some familiarity with existing languages for writing automations.
On the other hand, YAML is very good for what it does and itā€™s simplicity allows people to go from canned solutions, to a little bit of recoding, to writing your own automations fairly quickly.
I suspect that if you have something specific that you want to do that you think requires a higher level language, one of the wonks here could probably show you how to do it with YAML alone.

1 Like

Do you have examples of automations you tried to create but could not because of limitations?

Iā€™m not a programmer, but fairly experienced with HA. When I started with HA, I used AppDaemon (python) for all my automations, because at that time YAML automations were really limited (3 years ago), but a few months ago when choose and other things were added to native HA automations, I slowly moved over all my automations from AppDaemon to HA, as it is just another unnecessary layer. I still havenā€™t found an automation that is not possible with native HA automations, but is possible with AppDaemon.
From my experience here in the forum, I also noticed that a lot of people make their automations way more complex then needed and most of the time there was a way simpler automation logic that could easily be implmented with native HA automations.
Same as Taras, I challenge you to provide an automation that is not possible with native HA automations.

1 Like

Indeed.

I think one has to remember that creating automations arenā€™t quite the same as ā€˜programmingā€™ too.

Youā€™re not ā€˜programmingā€™ homeassistant, if you were youā€™d be submitting pull requests to the core github repo.

Rather you are feeding it a set of rules that use its programming react to something (trigger), check something (condition) and do something (action).

Feeding rules does not require any programming knowledge at all, and therefore does not require a programming language.

The reason people get confused is because they donā€™t take the time to understand how to structure a yaml document, and confuse ā€˜homeassistant logicā€™ with ā€˜programmingā€™ but itā€™s absolutely two very different things.

1 Like

Sort of agree, but anything that allows you to make a test and either do this or that as a result of that test, is programming to me.
:man_shrugging:

1 Like

I have to agree with Mutt here. Iā€™ve ā€œprogrammedā€ everything from PLAs and firmware to Applications to macros and scripts. The underlying syntax changes as do the ways you can represent logic, but the process from going from an idea to a functional unit is essentially the same.
To me ā€œprogrammingā€ is more a way of thinking than it is a specific set of syntax.
That said, YAML most resembles the parts of other languages that create data structures. It might help to think of it as data specification (which, as mf_social points out) is then acted upon by the HA core programming.

4 Likes

Before writing this post, I was trying to write two automations.

Simple one:
I have 8 lightbulbs in living room. And switch with 3 rockers. I wanted to change amount of turned on bulbs with one rocker, and brightness with other one.
Most probably it can be done with YAML, but it will be a lot of messy code. When with normal programming it will be just couple lines of code: Array with bulbs, variable for amount of turned on bulbs, and for-loop, which iterate from zero till amount of turned on bulbs.

Complex one:
Ability to play game with those lights. I have lot of ides, like two players have two buttons and lights change color over time. When there is exactly one red bulb players should press their button. Who pressed his button first will get a point. Amount of points displayed between rounds by blinking lights (every player have their own light-bulb for this). You have 4 points ā€” your light-buld will blink 4 times. Your opponent have 3 points ā€” his light-bulb will blink 3 times in the same as yours.

This is an interesting requirement. How do you envision using one rocker to ā€œchange amountā€?

I hope you donā€™t mean this: tap once to turn on one light, double-tap for two lights, triple-tap for three lights, etc?

If I have 10 bulbs and the variableā€™s value is 6, which 6 of the 10 bulbs should be turned on?

Does it simply use the first 6 in the list?

For me, I have found coding in Python to be familiar and help me get to my end result a little faster as I learn how to utilize YAML in the same way. I can see that YAML allows fairly complicated logic but because it looks messy to me (with the double brackets etc) I started things out with some of my more complicated automation using Python via pyscripts in HASS (I tried AppDaemon and itā€™s pretty cool but more overhead than I needed for my scripting needs).

So, again, for me, Iā€™m using Python until I have an opportunity to really understand how the YAML scripting works for more complex things. Iā€™m sure Iā€™ll move the Python stuff Iā€™m doing today into YAML in the future, but itā€™s a glove that fits me well at the moment and gets me to my end result. Iā€™m sure the HASS experts will criticize this decision, but Iā€™m getting the result I want so to me Python has been great to have handy.

1 Like

My own 2 cents as a relative newcomer. I wrote all my initial automations in YAML and found the learning curve and verbosity frustrating. I discovered pyscript and fell in love. Iā€™m familiar with python so I was able to make automations much more quickly and debug them more effectively. The scripts are shorter and much more readable to my eyes. I donā€™t know that it matters much whether one method or the other is more capable or more computationally efficient. Ultimately I think what matters most is how much time does it take you to get the result you want. If youā€™re comfortable with python, you have options which is great. If you like YAML, stick with the native HA method. Whatever floats your boat. Just grateful to the community for giving us options.

4 Likes

^^^^
This.

Same goes for the users who prefer to use Node-Red. Use what makes you productive.

+1 for pyscript. My own interest in it is mostly academic (I have no automation logic implemented in pyscript) but I know itā€™s there if I need it (and its syntax is tidier than python_script).

2 Likes

I can inderstand your motivation. I also hate yaml for various reasons.

I think nobody mentioned NodeRed. While itā€™s designed as visual programming tool it allows to program using JavaScript.
But most things you can do by using so called nodes which process messages flowing from one to another.
Besides native built-in nodes, there is a lot more which are available to add.

BTW this thread turned into attempts of persuade that yaml is not bad and there is no reason to replace it. How common on this forum.

2 Likes

Possibly I didnā€™t articulates myself as well as Iā€™d hoped. Yes it is ā€˜programmingā€™ by definition, but you donā€™t ā€˜programā€™ homeassistant in yaml, thatā€™s already done in python. You just provide data to it in yaml (or pyscript, appdaemon et al).

I think people get lost in the distinction and feel like they need to handle a lot more of the process than they actually do.

1 Like

Because YAML is not bad an there is no reason to replace it other than personal preference.

1 Like

Obviously OP has personal preference and his not asking to change it.

Yes, I can completely back that view
:+1:

And Marc happy new year. Letā€™s hope 2021 gets us some equilibrium

1 Like

Back atcha :+1:

1 Like