I would like to ask for your advice regarding the setup of automations and scripts within Home Assistant.
Currently, I have several automations running in Node-RED. I chose this option at the time because I found it to be clear and the management seemed easier. However, with the recent developments in automations in Home Assistant, I am considering gradually transitioning to this functionality and phasing out Node-RED.
Before I take this step, I have a few questions:
How many automations do you typically use?
How and when do you implement a script, especially with management in mind?
Does the number of automations or scripts significantly impact the performance of Home Assistant?
To make my question a bit more concrete, I want to share an example: My children each have their own button next to their bed to turn on their nightlight and the central bedroom lamp. Additionally, they can also turn on the hallway light. How would you translate something like this into an automation or script?
Should I set each button as a separate automation?
Can I migrate to one automation with multiple conditions
Or is it better to combine automations with scripts?
I have included a screenshot of Node-RED below for clarification.
You could use trigger variables.
So each event is a separate trigger in the automation, and accompanied there are variables with the % and entity name.
The action could probably be just a light turn on with the variables being the entity and percentage.
I would not use scripts for this. This is far to simple, adding scripts will just make it more complex and harder to follow
Always the first question -
First, Do I NEED an automation or script. (In most cases probably not or thereâs another construct that already does the thing. Letâs assume I do.)
If I do, I then ask myself, whatâs the simplest way to do it using HA built-in constructs. (For instance, if Iâm trying to glue together a synthetic media console I use a Universal Media Player - not just a bunch of scripts because the endgame presents a media player so the rest of the system (and my LLM) can use it.
Then when I need to build automations / scripts, remember automations are just scripts with a trigger. Scripts can accept variables while automations on the other hand (yes, Iâm ignoring the entire trigger variables discussion for now). Therefore, in my setup Automations are mostly just triggers âwe need to do a thing nowâ. I then build a script accepting vars to âdo the thingâ when necessary and then use automations to trigger those scripts.
As many as needed to get the job done. That said most often I only need 1 automation.
I try to avoid using any script because they tend to make debugging the action next year more complicated. What was I thinking when I made that script. An automation is like a script and will cover 99% of everything you might need.
Not really. HA can handle hunderds of automations without any sweat.
I 100% disagree with this one sorry ashai.but I understand why youâd say it. For my setup it actually makes things easier. I see a trigger. Fired script A with these vars. Hereâs the trace from A. Otherwise there is not really a difference troubleshooting IMHO now INTENT_SCRIPTS on the other handâŚ
Hehehe, not offended. But to be honest. Your setup is not a normal setup. It is more like a hypersonic spaceship with a still to invent hyperdrive in the back. In your case i surely would also use scripts. As a software developer that will give me far more control. But i still believe that for everyday use automations will do nicely.
The thing is that when a solution works it just works for a long time. No need to look at it anymore till is isnât. Having a lot of scripts makes debugging less easy if you havenât looked at them for a year or so.
That is so in my case. I create a solution and when it is working it can forget about it and work at other things.
The thing with scripts is that most use them as substitutes for the exact same thing.
As in this case (as far as we can see from the image) we need entity and percentage and a light turn on.
So we are going to replace the light turn on in the automation with a script call which then has a light turn on.
So what did we gain on that?
Same thing with a notification, people set up scripts where you feed it with message and title.
I reserve scripts to something I would use multiple times and the script should contain at least 3 steps otherwise I donât see the point.
You just made debugging harder as Ashai says in my opinion.
Sure I would know the issue is in either the automation or the script, or scripts.
But still, that leaves me with at least two places to look for a cause, in most cases that is not my preferred way.
Same. Iâm not using them for trivial crap. I agree with you in what most use them for and why I harp on âdo I need itâ
But having the ability to break stuff up when it gets REALLY complicated is helpful and I canât imagine trying to manage it any other way. Itâll also fit in automation construction as they start using the new targeting paradigm.
Unless you have very inefficient automations (meaning the automations run more than they need to), you probably wonât notice performance issues, directly related to the number of automations you have.
The time you might notice is when a major event happens (for example first person to arrive home), since HA has to do a bunch of stuff at once **.
In my case I notice a slight âwaveâ of actions as the actions fire in sequence - TBF its still under a second and if you are not explicitly looking for it you wouldnât notice - kinda like a flickering light doesnât bother you until you notice it.
** - I donât think this is actually a HA issue, I think its âbacking upâ sending all the commands over Zigbee.
I generally find that most automations follow common patterns:
Motion detected â turn on light.
Button pressed â toggle something on/off
I typically use 1 automation per pattern, regardless of the number of devices that it controls, so for example I have a single automation that handles all of my presence/motion detectors.
I find that having a lot of branching in automations or scripts is what makes them difficult to read, therefore I used âtricksâ to try to eliminate most (and occasionally all) branching.
For example here is my motion detected automation:
All presence sensors are in groups (some groups only have one sensor).
This is an âinputâ automation, so I take inputs and update âhelperâ input selects - it may do a little more than it needs to, but at the end of the day its only updating UI (helper) elements.
I use a map (scene_map) instead of doing a condition in the actions section, hence my house modes (Off, Day, Night & Away) are all defined in the scene_map.
The scenes to activate are defined as lists, therefore I can have any number of them without needing an if condition. Additionally if I donât want any actions I simply use an empty list (as in the case of the Off sensor mode).
There is also an Off scene for every room, so I donât need a special case I just activate the Off scene.
Note: There is an accompanying âoutputâ automation that reads the helpers and applies scenes (to change the lights). The output automation only fires if the select actually changes, so it doesnât matter if input automations fire a little more than needed, as nothing actually happens.
I donât always split âinputâ and âoutputâ automations, but I often find there are multiple input automations and only a single output, so it saves duplicating the output logic multiple times (my scene switches update the same UI helper selects, hence re-use the same output automation as the motion detectors).
In answer to your question (about script usage) I have refactored my scripts/automations so that I donât have to have any scripts, however I have 3 scripts which are invoked by my âButton Pressedâ automation, so that I can have 3 clear sections/functions (Scenes, AC, House Modes).