The door closes at night because that is when the most predators are out looking for food (defenseless chickens).

How To Build An Automatic Hinged Chicken Coop Door DIY