It occurs to me this might be a good use of the Expire binding. Then the timing out of the profile would actually be implemented outside of Rules. It would require a rethinking of your Rule as now instead of looping through all your RulesLastRun Items you would trigger a Rule when one of those members goes to UNDEF or OFF. See Design Pattern: Expire Binding Based Timers.
It also occurs to me, too late to be of much use to you, that this Rule is a special case of Design Patterns: Generic Is Alive.
Either or both of these might give you some ideas for refactoring or refinement.
Thanks for posting the script solution too.