Action with choose

Hi All,

I love the new options for automation and script. I wondering if this will work also:

Within the action a service call and then a choose option. Will this work or not?

###########################################################################################
# CONTROL LIGHT KITCHEN WHEN GOODNIGHT SCENE IS NOT ACTIVE
###########################################################################################

- alias: "Motion Detection - Kitchen On Off"
  trigger:

    platform: state
    entity_id: binary_sensor.node_16 # Motion Kitchen

  condition:
    condition: state
    entity_id:
      - switch.scene_daytime
      - switch.scene_goodnight
      - switch.scene_cooking
    state: "off"

  action:
    - service: mqtt.publish
      data_template:
        topic: 'homeassistant/motion/kitchen'
        payload: "{{ 1 if is_state('binary_sensor.node_16', 'on') else 0 }}"
        
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Christmas"
          sequence:
            - service: script.kitchen_christmas_turn_on
            
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Halloween"
          sequence:
            - service: script.kitchen_halloween_turn_on

        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Kingsday"
          sequence:
            - service: script.kitchen_kingsday_turn_on

        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Birthday"
          sequence:
            - service: script.kitchen_birthday_turn_on
            
        - conditions:
            - condition: state
              entity_id: binary_sensor.node_16
              state: "off"
          sequence:
            - service: script.kitchen_seeling_turn_off
                    
        default:
          - service: script.kitchen_normal_turn_on

Yes, that should work, but that’s the hard way to do something that can be done in a single service_template.

- service_template: >
    {% set holiday = states('sensor.holiday')|lower %}
    {% if holiday in ('christmas', 'halloween', 'kingsday', 'birthday') %}
      script.kitchen_{{ holiday }}_turn_on
    {% elif is_state('binary_sensor.node_16', 'off') %}
      script.kitchen_seeling_turn_off
    {% else %}
      script.kitchen_normal_turn_on
    {% endif %}

I already have it this way. What i want to try is some conditions and within the conditions later on create some if else statements. Every color scene have some specific rules and conditions also. Otherwise it will become a big if else lalalala statement :slight_smile:

Yep there are pluses and minuses to both solutions. Use what you’re most comfortable with and works best for you. I was just pointing out an alternative solution.

Even those the choose option has only been available for a couple of days, I can already see people are gravitating to it when other, potentially simpler, solutions are, and have been, available. I guess choice is good! :stuck_out_tongue_winking_eye:

haha. Its fun to create a good if else etc… its more complex and you have to do lot yourself.
More satisfying to build yourself. But with the choose it will be faster readable ;D
As you say… plus and minus are friendsor enimy haha

Finally this is the automation. Inside the choose part there are if statements. Will be a challenge to build this all into a if statement without choose new feature. Now I can remove the script part. Less files and everything together in 1 file… makes life easier haha

###########################################################################################
# CONTROL LIGHT KITCHEN WHEN GOODNIGHT SCENE IS NOT ACTIVE
###########################################################################################

- alias: "Motion Detection - Kitchen On Off V2"
  trigger:

    platform: state
    entity_id: binary_sensor.node_16 # Motion Kitchen

  condition:
    condition: state
    entity_id:
      - switch.scene_daytime
      - switch.scene_goodnight
      - switch.scene_cooking
    state: "on"

  action:
    - service: mqtt.publish
      data_template:
        topic: 'homeassistant/motion/kitchen'
        payload: "{{ 1 if is_state('binary_sensor.node_16', 'on') else 0 }}"

    - choose:
# CHRISTMAS
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Christmas"
          sequence:
            - service: light.turn_on
              data_template:
                entity_id: light.kitchen_all
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 70
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_all
                color_name: "red"
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_2
                color_name: "green"
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

# HALLOWEEN
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Halloween"
          sequence:
            - service: light.turn_on
              data_template:
                entity_id:
                  - light.kitchen_all
                  - light.sink_all
                color_name: "orange"
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

# KINGSDAY
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Kingsday"
          sequence:
            - service: light.turn_on
              data_template:
                entity_id: light.kitchen_all
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 70
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_1
                color_name: "red"
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_2
                color_temp: 153
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_3
                color_name: "blue"
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

# BITHRDAY
        - conditions:
            - condition: state
              entity_id: sensor.holiday
              state: "Birthday"
          sequence:
            - service: light.turn_on
              data_template:
                entity_id: light.kitchen_all
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - service: light.turn_on
              data_template:
                entity_id: light.sink_1
                effect: 0
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - delay: 00:00:01

            - service: light.turn_on
              data_template:
                entity_id: light.sink_2
                effect: 0
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

            - delay: 00:00:01

            - service: light.turn_on
              data_template:
                entity_id: light.sink_3
                effect: 0
                brightness_pct: >-
                  {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
                  {% else %}0
                  {% endif %}

# DEFAULT
      default:
        - service: light.turn_on
          data_template:
            entity_id:
              - light.kitchen_all
              - light.sink_all
            kelvin: 2800
            brightness_pct: >-
              {% if states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'on' %} 20
              {% elif states('binary_sensor.node_16') == 'on' and states('switch.scene_goodnight') == 'off' %} 100
              {% else %}0
              {% endif %}
3 Likes

Just a quick question:

is it possible to put multiple conditions in a “choose”?
like a “state” AND a “time”?
if yes, what’s the right syntax?

Sure, you can put as many conditions in a choose as you like:

action:     
  - choose:              
	- conditions:
		- condition: state
		  entity_id: person.me
		  state: 'home'
		- condition: time
		  after: '06:00'		  
	  sequence:
		- service: script.do_stuff