I want to use a single automation with the single trigger (button press/MQTT message), then do one of two different actions depending on a condition.
This seems like it should be much easier…
I just want a simple if/else then two separate actions, but can’t figure out how.
The closest I got was with service_template but when the if is false, there’s no service so that’s invalid (non-canonical, complete example):
I think you’re on the right lines with service_template. There may be a better solution, but you could write a “do nothing” script and call its script.turn_on service to get around your problem.
Creating and saving a script in the UI with just a name — no actions — appears to create a valid “no-op” script, and running script.turn_on effectively does nothing.
Thanks for the reply. I thought about that, a “pass” kind of script… still seems like a hack.
I just realised since I’m duplicating anyway, I could make a stop/off script such that the service is the same and the entity is the only difference:
Again, still feels like it breaks the “DRY” principle to have a script that just does a simple turn_off, but better.
I’m still open to suggestions (learning), thanks.
Because I don’t know if ‘off’ & ‘on’ (or in the example above “not ‘off’”) are the only states for the media player entity.
It’s not required if the only possible states are ‘off’ or ‘not off’.
But even without the ‘else’ and in the event that those aren’t the only states then it will just write an error message into the log but still work so not a big deal to the non-ocd among us. If it does become an issue then just put an else clause and add a dummy service and/or a dummy entity in.
Thanks for the thoughts/replies so far.
The trigger is just a simple MQTT message from a button - same every time.
The condition is just off or not off (not ‘on’, but ‘idle’, ‘playing’, etc.).
@finity - that’s a cool idea of using one service_template and data_template, but it duplicates the if/else. @jwelter - that would only work if either the service or entity_id/data were the same; mine are different actions.
There appears to be no method that doesn’t violate DRY…
That’s probably true (but I’m not sure that having more than one of the same if/else in different sections violates it or there may even be other methods not previously thought of yet) but why is that such a problem?
If it works and gives you the result you want and you understand it enough to maintain it then I’d say use it. Or not and stay with your first iteration that worked. It’s not like you’re selling the code as a service to others and going thru a review process for it.
There are likely other methods of accomplishing this like creating binary sensors or even using other programming languages (python or node-red) but if you want to stay within HA and use yaml/jinja you are kind of stuck just making do and making it work even if it’s not “dry”.
You’re correct; it should be. And, coincidentally, based on some recent architecture discussion, and leveraging off work that I’ve recently completed on improving HA’s native scripting language (which should be in the 0.113 release), I’m just about to start implementing a new script action option, namely choose. This will make it possible to implement if-elif-else type statements. See the link for what it will look like. (I actually just sat down to start work on it, but thought I’d check recent posts, first. )
You’re right; the bottom line is getting it to work satisfactorily.
To answer your question… I teach computer programming with a focus on best practice.
Duplicating the basic service by using a script is just a bit of extra work for the programmer;
Duplicating the if-else is duplicating both work for the programmer AND work (and hence, execution time) for the computer.
@pnbruckner - awesome to hear that this is being addressed!
I read through most of that discussion you linked. I’m happy with the more generic approach of using multiple if/conditions compared with the template and switch-style labels.
I look forward to it! If there’s any chance you can reply here when it’s out, that would be super!
Well I think I just finished implementing it. Next comes some ad hoc testing, adding formal tests & updating the docs. It’s possible I’ll have PRs ready in time for the 0.113 beta cutoff (next Wednesday.) But, sure, I’ll keep you posted.
All script/automation enhancements, including the choose action, are available in the 0.113 beta. Assuming all goes well, which it looks like it is, 0.113 should be released tomorrow.