Nested code blocks with their own conditions in scripts & automation actions

Something I feel that is missing in HA is the ability to have a script or automation action continue with additional code even after a condition = false. If we could have nested blocks of code where the condition only applies to that block and the script / automation could continue afterwards, that would be a massive game changer for HA. Currently we can only have one condition in an action = false and the script / automation stops. This is rather limiting.

logic example:

service_1:
service_2:
    condition_1:
    service_3: # this service to be based on condition_1
service_4:     # this service to occur irrelevant of condition_1
    condition_2:
    service_5: # this service to be based on condition_2
    service_6: # this service to be based on condition_2
service_7:     # this service to occur irrelevant of condition_1 or condition_2

The idea being that we could use indentation or some other means to signify which services are controlled by which conditions and any code after that with no indentation would continue.

I couldn’t agree more.
From day one of using HA I have missed something like this.

Have you thought about sending the possibly blocking condition into its own script?

This way the automation can run through all the services but the conditions are evaluated in their own independent scripts and don’t block the other services.

Not as clean as your request, but its a way to make it work with the current version.

Another option would be to use a service template. If the condition is true one service if its false run a different service (one that wont error out but doesn’t actually do anything meaningful)

I think the service_template is a good work-around for now as I would rather not have to turn 1 script into many, still not as neat as the nested concept. Hopefully this can gain enough votes for dev attention…

Yes, I use service_template a LOT to get around this but it relies on everything that follows being valid for all outcomes of the service_template.

Or more service_templates.

Or the service_template branching off into separate scripts; which does…

Or a condition where only one outcome continues. But now we are back to the original feature request :wink:

It looks like my request may have been answered! (finally)

1 Like

Yep, you now owe Phil, your first born :rofl: