The way it works, you can call TargetUnit("Rare") in your script anytime you want.
If "Rare" doesn't exist in your vicinity - nothing will happen.
However, if "Rare" does exist then this call will fail because you are not allowed to target units w/o hardware event and you will get a standard error frame popping up telling you that.
And that's how you detect a rare - by causing you script to fail.
To make it more practical you might want to add a reload UI button and add a check if your target is alive so not to trigger that fail again over the corpse but strictly it terms of detection it's the fastest and highest range way to do it.