Twenty-year-old Jordan Goodwin, also known as Jordan Hagan, stuffed pillows under the covers of his bed to give the impression he was sleeping, Wolverhampton Crown Court heard.
Then he forced open a secure window and ran away from the Huntercombe Centre in Underhill Street, Langley, that caters for people with mental health problems, on March 13.
He left a letter of explanation at the home of his partner and flew out of the country, said Mr Andrew Tucker, prosecuting.
He added: “He got to Spain but was arrested pretty quickly.
“A number of weeks earlier he had been seen filling in a passport application form and when asked why came up with some innocuous reason.”
But Judge Amjad Nawaz asked: “What other purpose could there be for a passport?”
The defendant had been made the subject of an indefinite hospital order at Derby Crown Court in 2011 after committing a serious sexual offence at the age of 12, it was said.
A European Arrest Warrant was issued four days after he vanished and, on March 24, he was flown from Madrid to Birmingham.
He was then detained by police at the airport on landing.
Mr Simon Hanns, defending, said: “There was some element of planning but whether the authorities should have been aware of this is another matter.
“He feels that the hospital order is no longer appropriate and he should now be back in society.”
Goodwin admitted escaping from custody and was given six months detention in a Young Offenders Institution.
However, he had already spent more than half the term in custody waiting for the case to be resolved.
That meant he was eligible for immediate release and could be transferred to a secure unit in Northamptonshire to continue his treatment.
Judge Nawaz told him: “You feel you are being dealt with unfairly by still being held on the hospital order but that is a matter for professionals and a tribunal to decide.
“I am keeping the sentence short to allow you to return to this order because that is in the best interest of, not just you, but also the general public.”