Automations if else if else

it would be great to add the statments if , else, else if to the automations graphic set up, also to have a delete option of the automations present with confirmation

Even though I see some value in that, I think the way it is now is actually better. We currently have automations, which, optionally depending on conditions (which is sort of an if), perform some action. Adding further if's (which I assume you would place along with the services to be called), would in some way make automations less predictable. Having separate automations for each case would indeed result in more maintenance. But it would also mean that there’s a 1:1 relation between the conditions you have chosen and the action that has been performed in the end.
So essentially: having multiple variants of one automation (with different conditions) is already possible, and is finer grained when looking at the history.

An example:
If the laundry is done and you’re home, make Alexa say “the laundry is done”. If the laundry is done and you’re away, send a notification via telegram.

I can see that it would be nice to use just one automation that fires when the laundry is done, and depending on where I am does either the one thing or the other. But in some way both cases are actually separate automations. If you could do the if/else thing, then your history would say it executed that one automation. But looking at that automation alone you couldn’t tell if you were at home or not. If instead you had separate automations, one for at home and one for not, you would instantly know which conditions were met.

An analogy may be overloading of functions in programming. If you have one function that’s overloaded and does different things depending on the input, then it’s a pain to debug that if you don’t know what data the user has entered and you didn’t care to log which variant has been executed and seems to be buggy.

That being said, I think to some degree it’s also possible to do some if/else with templates already. But I don’t know much about that.

1 Like

If you want to write automations in a programming language, try appdaemon.
https://home-assistant.io/docs/ecosystem/appdaemon/

1 Like

For if else/if else if, just call two scripts from your automation and check conditions in each script.

Or just use two automations.

Else is just a negated if condition, so yeah, you have more to write, but it is easy enough to do.

Also, besides appdaemon, node-red is another alternative for programming that might make it easier.

Taking a little time to learn appdaemon is well worth the investment if you are comfortable programming in python or similar languages.

I’d quite like this, it would be nice if an automation could do one thing if a state is this, while doing a completely different thing if state is that.

Sure you can make multiple scripts and launch them form an automation but this seem more like a hack than a solution IMO.

I agree with @Codec303, would really like to have an If else capability to clean up my automations and scripts.

Especially having an if condition in a script that only executes a part of the script if the condition is true and otherwise continues on with the rest of the script. This is very similar to the “condition” in a script that is already there, but that always makes the whole rest of the script quite if the condition is false. I would like to have it only not execute a few lines of the script and continue with the rest.

Example when this is useful:

I have color scenes defined throughout the house such as “savannah” and a input_select to select to which part of the house that scene needs to be applied. Based on the input_select the colors of different parts of the house will change (such as ‘living_room’, ‘hallway’, ‘kitchen’ etc). Now I realized this with 1 script called savannah that fires off about 8 other scripts to apply the color to each different region of the house and each of those 8 scripts have conditions in them to check if the color should be applied based on the ‘input_select’. Because I did this for 9 different color scenes this makes for 8x9 = 72 scripts cluttering my HomeAssisant. While I would have been able to do this with only 9 scripts instead of 72 if I had the “if” option in a script…

I think the solution would be to join conditions and actions.

So you have triggers as they are now.

You then have “if this condition do this action”, etc, etc, etc.

I think this would be a more intuitive way versus multiple automations with the same trigger, if even behind the scenes it’s the same thing.

Your example would become:

trigger: if laundry machine is done

condtion/action:

  1. If home then Alexa say it’s done
  2. If not home then send a notification via telegram
1 Like

I get why this seems a good solution.

However, consider more than one person living in the household, both being talked to via Alexa when at home, and via mobile notification if away.
Not lets imagine one of the two persons goes on a business trip / important appointment, the other one stays at home. The person at home does the laundry, so Alexa announces that. On top of that the person away gets the notification. But being in a meeting it might actually be bad getting that notification, or just irrelevant. With separate automations it would be possible to just disable automations based on some presence-state / calendar / whatever. The ones not required. If this would depend on additional conditions, it would make the automation itself more complex, and this complexity would have to be added to each automation.

To me it seems a lot simpler to be able to disable specific automations when required instead of possibly creating complex conditions that might break the automation as a whole under unforseen circumstances.

And in fact I already do it this way. If someone in this household goes on holiday, the automations telling them about the loundry simply get disabled. After all they don’t want to know about stuff like that whily laying on the beach. And for me everything keeps working without using complex conditions.

Yes, but this is already an issue and requires multiple conditions or condition templates to solve.

I have an automation very much like you propose, but to take out the garbage.

If anyone is at home, it uses TTS at 5:30 to announce to take out the garbage.

If nobody is home it sends a message to my wife and I to take the garbage out. But, if I am over 50 miles from home I am omitted from the text. Also if the house is in vacation mode (which means it’s been empty for 36+ hours) no text.

So all this complexity exists today but we add complexity because we don’t have a proper if/elseif/else

I guess it really just boils down to a personal automation-paradigm. Earlier you’ve said I think the solution would be to join conditions and actions. Which makes sense if you want to organize your automations like “this is my garbage-automation”, “this is my laundry-automation” etc… That’s totally reasonable, as long as it doesn’t impact the current behaviour. Because, at least for me, I think of automations as “this one tells me the laundry is done”, “this one tells person x the laundry is done”.

So essentially I guess we could say we can think of automations either based on the triggers (with different tasks based on (multiple) conditions), or based on what task should be executed under one set of conditions.
This gives us either one automation with multiple possible outcomes (what should happen if actually more than one outcome is desired? Duplicate the action per condition?), or multiple automations with each just having a single outcome if the condition is met.

I’m not voting against the feature of conditional actions. They could be a useful enhancement. I’m just trying to point out a different perspective on how to think about automations, which minimizes the need for conditional actions. :+1:

4,5 years later…

Today we’ve merged an if/else construct, expected to land in Home Assistant 2022.5

3 Likes