If / Then / Else in script

Hi,

I tried really hard but can’t figure it out. I try to do the following. I have written a script which arms my alarm system and one which disarms the alarm system:

alarm_arm:
  alias: Alarm-Arm
  mode: single
  sequence:
  - data:
      code: xxxx
      entity_id: alarm_control_panel.security
    entity_id: alarm_control_panel.security
    service: alarm_control_panel.alarm_arm_away
  icon: mdi:shield-lock
alarm_disarm:
  alias: Alarm-Disarm
  icon: mdi:shield-home
  mode: single
  sequence:
  - data:
      entity_id: alarm_control_panel.security
    entity_id: alarm_control_panel.security
    service: alarm_control_panel.alarm_disarm

I trigger them via a button in Lovelace. Works great so far. I also created a group which includes all my window sensors. This group either has the status Off or On depending on if one of the windows is open or closed. Also works great. Now I would like to include some magic in the above arm script which does the following:

If Group = On
Then
send some notifications etc. that the windows have to closed
terminate the script
Else
arm the alarm system with the working code above

I would really appreciate some help here or some building blocks for the code. Thank you.

You can use choose in your script for this:

alarm_arm:
  alias: Alarm-Arm
  mode: single
  icon: mdi:shield-lock
  sequence:
    - choose:
        - conditions:
            - condition: state
              entity: group.windows
		      state: 'on'
          sequence:
			- service: notify.telegram
			  data_template:
				title: "Window open"
				message: "Check your windows!"
      default:
		- service: alarm_control_panel.alarm_arm_away
		  entity_id: alarm_control_panel.security
		  data:
		    code: xxxx
			entity_id: alarm_control_panel.security

Hi Dennis,

That’s awesome. Thanks so much. Works perfectly fine with the following code now.

alarm_arm:
  alias: Alarm-Arm
  mode: single
  icon: mdi:shield-lock
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: group.windows
        state: 'on'
      sequence:
      - data:
          data:
            method: all
            type: announce
          message: Please close windows first. Alarm is not armed.
          target:
          - media_player.living_room_echo_show
          - media_player.nicolas_echo
          title: Home Assistant
        service: notify.alexa_media
    default:
    - entity_id: alarm_control_panel.security
      service: alarm_control_panel.alarm_arm_away