Gerard Bryceland, 53, was accused of touching the complainant inappropriately at his home in Glasgow's West End in August 2019. The woman said she 'froze' as she and her partner woke to find the man in bed beside them with his hand over her body.
But the defendant has now been acquitted of the sex assault charge after telling a court that he was sleepwalking at the time.
A sheriff at Glasgow Sheriff Court accepted a psychiatrist's view that Bryceland's sleepwalking was a 'mental disorder'.
The special defence states: 'A person is not criminally responsible for conduct constituting an offence, and is to be acquitted of the offence, if the person was at the time of the conduct unable by reason of mental disorder to appreciate the nature or wrongfulness of the conduct'.
Glasgow Sheriff Court heard Bryceland, his wife and the young couple had gone out together that day before going back to the flat.
Prosecutor Chris Farrell told the court the pair went to sleep in Bryceland's spare bedroom when the 53-year-old started groping her.
'The woman said she was frozen and woke up to find a hand on her breast,' he said.
'Buttons on her pyjamas were undone and his hand moved down to her thigh area.'
Her partner woke up and was able to shake off Bryceland who 'rose quickly, almost immediately and sprung up out of bed.'
Mr. Farrell added: 'He walked out to the hallway laughing.'
Bryceland and his wife reportedly met psychiatrist Saduf Riaz, who diagnosed his sleepwalking condition.
Glasgow Sheriff Court heard Bryceland had been sleepwalking since he was a boy and it can be triggered by alcohol use and anxiety.
Ann Ritchie, defending, said: 'He was horrified and shocked about what happened in the room and wanted to apologise to both of them. The psychiatrist said he was worried about it and he was depressed.'
Sheriff Charles Lugton said: 'I find that the special defence has been established. This was a serious incident as a result of your sleepwalking which is a condition. It is appropriate to consider the need to protect the public following on from your acquittal.'