Choose, Sequence Contains A Condition, Shouldn’t A False Condition Terminate The Whole Automation?

From my experiences with the action CHOOSE what happens in CHOOSE stays in CHOOSE just like in VEGAS, not what I expected. Example: When a condition in a sequence of a choose action/sequence is false only the choose action/sequence terminates, the remainder of the automation/script continues to run. How can I terminate the automation/script from within a choose action/sequence?

Similarly, variables defined outside of a choose action/sequence can be used in the choose action/sequence, but redefining variables inside the choose action/sequence are not carried forward to the remainder of the automation/script. Maybe this will be resolved as variables are fully implemented in HA in the meantime how do I get variables defined/redefined in a choose action/sequence into the remaining actions/sequences of the automation/script?

The following automation was used to verify the above statements.

- alias: Housekeeper ON4
  description: 'Housekeeper On TEST'
  initial_state: true
  mode: single

  trigger:
  - platform: state
    entity_id: input_boolean.hk_manual_override  
    to: 'on'
  - platform: state
    entity_id: input_boolean.hk_manual_override  
    to: 'off'

  variables:
    stop: 'false'

  condition: []

  action:
  - choose:
      - conditions: "{{ ( is_state('input_boolean.hk_manual_override', 'on') ) }}"
        sequence: 
          - condition: template
            value_template: "{{ 1 == 2 }}"
          - service: switch.turn_on
            entity_id: switch.b_xmas_lights_32
      - conditions: "{{ ( is_state('input_boolean.hk_manual_override', 'off') ) }}"
        sequence: 
          - variables:
              stop: 'true'
          - service_template: >
              {% if stop == 'true' %}
              switch.turn_on
              {% else %}
              switch.turn_off
              {% endif %}
            entity_id: switch.b_xmas_lights_32
    default: []

  - service_template: >
      {% if stop == 'false' %}
      switch.turn_on
      {% else %}
      switch.turn_off
      {% endif %}
    entity_id: switch.f_xmas_lights_31

I think this is wrong.


- service_template: >
      {% if stop == 'false' %}
      switch.turn_on
      {% else %}
      switch/turn_off  ##  This should be switch.turn_off
      {% endif %}
  entity_id: switch.f_xmas_lights_31

Thanks for pointing that out Cameron, I corrected the original post. I copied the wrong version of the automation to the post.

RESULTS:
Change input_boolean.hk_manual_override to ON triggers automation
Switch	               Expected Result	      Actual Result
b_xmas_lights_32	     off	                      off
f_xmas_lights_31	     off	                      on

Change input_boolean.hk_manual_override to OFF triggers automation
Switch	               Expected Result	      Actual Result
b_xmas_lights_32	       on	                    on
f_xmas_lights_31	       off	                    on

The last service template isn’t in the chooser, so it will always run (unless my eyes are playing tricks on me)

1 Like

Thank you, I missed that it will always run in the documentation and your eyes aren’t playing tricks the last service template isn’t in the chooser.

Based on your statement my error is considering the sequence of the choose as an extension of the actions in the automation and the choose as a path director. Similar to traveling down highway 1 and turning off on a town’s business loop for a meal then continue to travel down the business loop until it merges back onto highway 1. But if I have a fatal accident on the business loop all travel stops there and I never merge back onto highway 1.

I will change my perception of choose to a single action that selects a group of actions from a list. The group of actions functions the same as a discrete script where conditions, variables and other actors only apply to the discrete script within that choose. Likewise, variables defined outside the choose are available within the choose, but variables defined within the choose are not available outside the choose. This is also true for a nested choose. Similar to traveling down highway 1 and turning off on a town’s business loop for a meal then continue to travel down the business loop until it merges back onto highway 1. But if I purchase a souvenir while on the business loop the souvenir will disappear when I merge back onto highway 1. If I have a fatal accident on the business loop, I no longer travel down the remainder of the business loop instead I magically reappear back on highway 1.

Choose is not what I expected when I first saw it in 0.113.

1 Like

Your poetry is truly magical :wink: