WHILE condition

I need a rule to stop running completely if a condition is met. I have to activate a device, wait, activate again, wait, activate again BECAUSE my device is IR and I have no feedback. This rule may take 5 mins to run completely BUT I would like to stop the rule if a specific switch is activated. Say I turn a TV ON, then my 5 min long rule runs, but within 2 mins, I turn the TV OFF. The off rule will run, but the ON rule was not finished so the TV goes back on by itself.