For complex rules, including making sure that the motion sensor doesn’t override the wall switch, see the following.