This can be a lifelong lesson. Don't expect babies and young children to do what you say all of the time. Young children do things for no reason. They will hit, yell or fall apart at times because. Their feelings of sadness and anger are often too strong to control.
Babies begin to learn self-control when they begin to soothe themselves.
They learn to fall asleep on their own and begin to calm themselves with a little assistance from caregivers. Babies learn as their brains grow. It takes time. It is normal for children to break or change a rule. Respond to their actions in a firm and caring way.