The Messenger of Allah ﷺ said, "There is no man or woman except that upon his head, when he goes to sleep, are three knots tied with silk. If he wakes and remembers Allah the Exalted, one knot is untied. If he rises and performs ablution, another knot is untied. And if he stands to pray, all the knots are untied."
حدثنا أبو معاوية حدثنا الأعمش عن أبي سفيان
قال رسول الله ﷺ ما من ذكر ولا أنثى إلا وعلى رأسه حرير معقود ثلاث عقد حين يرقد فإن استيقظ فذكر الله تعالى انحلت عقدة فإذا قام فتوضأ انحلت عقدة فإذا قام إلى الصلاة انحلت عقده كلها
Its isnad is sahih, and Haythami also graded it sahih (2/261), saying, "Its narrators are the narrators of the Sahih." It is found in Ibn Khuzayma (2/175, no. 1133). The hadith has already appeared with the wording, "The devil ties a knot at the back of the head of each of you," in 7306.