You can do what you're trying but not in combat. If...Then...Else statements tied to spells/abilities are protected while in combat, specifically to prevent this kind of automation.