Automation: Return to previous state on exit

Basically I have some lightning animations I would like to play when the YR symbol is one of the lightning symbols.

Of course, it doesn’t need to do this the whole day. So when the YR symbol is no longer a lightning-containing symbol, I would like for it to disable this animation.

Is there a way to perhaps have it run a script when it enters the condition, then run a different script when it exits?

I don’t mean “run a script when any non-lightning-containing symbol is activated” because that would have to check about 20 conditions.