However, a judge rejected Charles McCann's claim for £3000 in compensation, stating that he must have saved about £8000 on the cost of cigarettes since the ban came into force at the State Hospital at Carstairs.
The State Hospitals Board for Scotland banned smoking in December 2011 and McCann, who suffers from schizophrenia and has been detained for 18 years, went to the Court of Session in Edinburgh to challenge the move in a judicial review.
He objected to management prohibiting the possession of tobacco and smoking inside the hospital and also in its grounds, where he is allowed unescorted access during daylight hours.
Lord Stewart said he was prepared to make a restricted declaration that the policy was unlawful as affects the patient and that it was a breach of his rights under the European Convention on Human Rights (ECHR).
He said he was allowing the application "with a degree of reluctance" and ruled: "The decision to compel the petitioner to stop smoking was flawed in every possible way."
However, he added: "I want to make clear I am not endorsing the idea of a 'human right to smoke'. The fundamental right in terms of this aspect of article eight ECHR is to have your identity, how you choose to express it and other personal, private and intimate choices, whatever they may be, respected, even if your choices are harmful to yourself, morally reprehensible or laughable.
"If you are an adult, the state cannot interfere with your choices in the private sphere except for weighty reasons to do with the protection of others and the good of the community as a whole."
Lord Stewart said if McCann was of sound mind or his condition was such he could be treated in the community he would be able to smoke at home and in other places. If he were a prisoner he could smoke in jail.
The judge said: "I infer that the smoke-free policy has been imposed on mental health detainees and not on penal detainees simply because the latter are in a better position to defend their smoking habit whereas the former are not."
The judge said the hospital had failed to show an "objective and reasonable justification" for treating him differently from long-term prisoners who can smoke.
He said: "Reading between the lines, I infer that Scotland does not have a complete statutory prohibition on smoking in the buildings and grounds of psychiatric hospitals in general and of the State Hospital in particular because the Government could not, at the time, gather the requisite consensus for such a ban.
"If the legislature will not support a measure it is wrong to enforce it by extra-statutory means. It may be of course ... the time is right to try and put the ban on a statutory footing."
Lord Stewart said he agreed with a submission that it was not appropriate to award damages in the case.
He said: "I have no difficulty with the idea the petitioner has been deprived of one of his few pleasures, that he dwells on his inability to smoke and feels frustrated and aggrieved and so on.
"On the other hand, the unequivocal effect of all the information put before me is that he must have gained significant health benefits from not smoking. He must also have saved a lot of money."
The judge said the orders he would make would allow the patient's case to be reconsidered by the hospital authorities.
A spokeswoman for the State Hospital said: "We have today received the determination on the Judicial Review of the Board's decision on having a smoke-free environment. The determination is that the petition raised by the patient has been upheld in part.
"We are disappointed and are currently considering its contents and the options and implications."