‘Ali (may Allah be pleased with him) reported that the Prophet (may Allah's peace and blessings be upon him) said: "There are three (types of people) whose actions are not recorded by the Pen: the sleeper till he awakes, the minor till he reaches puberty, and the insane till he regains his sanity.”
عن علي -رضي الله عنه- عن النبي -صلى الله عليه وسلم- قال: "رُفِعَ الْقَلَمُ عن ثلاثة: عن النائم حتى يَسْتَيْقِظَ، وعن الصبي حتى يَحْتَلِمَ، وعن المجنون حتى يَعْقِلَ".
شرح الحديث :
This Hadīth proves that being under age, asleep, or insane is a reason for losing 'Ahliyyah', which is the person's legal competence for fulfilling rights that are due upon him and enjoying rights that are due for him. Accordingly, the minor, the insane, the sleeping person are not competent under Shariah to observe commands or avoid prohibitions, a fact that features Allah's mercy and kindness. However, reaching puberty removes the excuse of the minor, waking up removes the excuse of the sleeper, and sanity removes the excuse of the insane.