Motion sensor During the Day and socket timmer Triggered from Light switch

Please How to use code fences instead of quotes for posting code.

The Rules above look like a much more drawn out and complicated way to implement Design Pattern: Simple State Machine (e.g. Time Of Day). You could cut a couple of hundred lines of duplicated code if you follow that DP