A man grew cannabis at his Crosland Moor home to help his joint pain and poor circulation.
James Lees, of Hawthorne Terrace, pleaded guilty to producing the Class B Drug.
Kirklees magistrates were told that police had reason to attend his address on April 4.
They were looking for someone else and the 38-year-old allowed them inside, prosecutor Andy Wills said.
He told magistrates: “He was reluctant to allow them upstairs towards one of the bedrooms and a baseball bat was jimmying the door shut.
“The officers entered the room and found six very large cannabis plants five to 6ft tall and 24 smaller plants.”
These smaller plants were inside the built-in wardrobes and there were also heating lamps, fans and an extractor fan hanging down from the ceiling.
The window was covered in silver foil lining and there were trays filled with cannabis, magistrates were told.
Lees accepted growing the cannabis for his own personal use and said that half of the plants had died.
He had 26 offences on his record, including two for production of cannabis.
Catherine McCullough, mitigating, explained that most of the plant cuttings were taken from the lager plants.
She told magistrates that her client had mild learning difficulties and ADHD.
Mrs McCullough said: “The drugs were for his personal use. They were for pain relief as he has joint pain and bad circulation.”
Magistrates were told that Lees had started using heroin at the age of 16 and sometimes uses a bag of the drug a day.
They heard that Lees is attending the CHART Kirklees drug support service on a voluntary basis.
Magistrates sentenced him to a 12 month community order with 10 rehabilitation activity days, He will have to abide by an electronically-monitored curfew for 26 weeks between the hours of 7pm and 7am.
He must pay £85 costs and £85 victim surcharge and the cannabis will be forfeited and destroyed.