Allow for simple branching in scripts

I know that we can sort of branch in scripts now using service_template, but if the two services require different data parameters, then we’re kind of out of luck. I would propose a slight extension to the condition clause of a script – the addition of an “on_false” attribute that can contain a sequence of service calls. Take the following example:

script:
  the_script:
    - service: some.service
      data:
        param: "This is always executed."
    - condition: first_condition
      on_false:
        - service: another.service
          data:
            a: "Only executed if first_condition evaluates to false"
            b: "Execution stops here."
    - service: third.service
        blah: "Only executed if first_condition evaluates to true"
    - condition: another_condition
      on_false:
        - service: whatevs
          data:
            bar: "Only executed if another_condition evaluates to false"
        - condition: nested_condition
        - service: more.service
          data:
            a: "Only executed if (another_condition == false) and (nested_condition == true)"
            b: "Execution stops here."

If the condition evaluates to false, the sequence under on_false executed, so the default behaviour of a condition doesn’t change. For a condition without on_false, the execution stops (as it does currently) because on_false is an empty sequence.

Thoughts?

good read, and thoughts, and we’ve discussed it here more than once, especially the service templates that need different data in either case of the template…

Still, out of luck isn’t 100% true is it. What we can do, and I do so most frequently, is create simple (…) subscript(s) for this, which is/are then called in the main script.

example:

check_turn_down_the_house:
  alias: Check turn down the house
  sequence:
    - service_template: >
        script.{{'turn_down_the_house' if is_state('input_boolean.house_turned_down','off') else 'dummy' }}

with 2 subscripts:

script.dummy simply does nothing and returns to the main script with which ever service follows. If any.

script.turn_down_the_house is somewhat more complex:

turn_down_the_house:
  alias: Turn down the house
  sequence:
    - delay:
        seconds: 2
    - service: script.check_ok_audio_turn_off #package_audio_system
    - delay:
        seconds: 2
    - service: switch.turn_off
      entity_id:
        - switch.sw_espresso_keuken_cl
        - switch.sw_multi_purpose_cl
        - switch.sw_tv_library_cl
        - switch.sw_netwerk_library_cl
        - switch.sw_audio_gym_cl

    - service: script.turn_off_home_mode_party
    - service: script.netwerk_dorm_off_check
    - service: script.dorm_off_check
    - delay:
        seconds: 5
    - service: switch.turn_off
      entity_id: switch.sw_netwerk_auditorium_cl
    - service: input_boolean.turn_on
      entity_id: input_boolean.house_turned_down

and as you can see, has yet another set of subscripts :wink:

so, it might be a bit complex, but, we’re not out of luck. Secondly, because one has to think about the structure of all this, I feel it is more manageable, and easier to follow, than putting all of the logic in 1 main script. One can add or change individual services in the respective script quite easily depending on changing circumstances.

the top script.check_turn_down_the_house is in itself called as only service in my main home scenarios automation.

You can do simple branching in script sequences with the choose action.
See HA Docs on Script Syntax for choose.

Yeah, I suggested this before “choose” existed or perhaps was in initial development.

I have adapted to using “choose” :slight_smile:

Sorry, I know the thread was old, but my OCD wouldn’t allow me to leave the question hanging, now that it has been addressed in later HA versions.
(I’ve never yet had to ask a question in HA forums - the repository of answers to existing questions, together with HA docs, has always been enough to find what I need.)