An unhinged man got caught in an air duct at a Midtown New York hotel while trying to set himself on fire during a bizarre burglary and suicide attempt early Wednesday, police said.

Hotel staff found buff and barefoot Juan Estella, 35, wearing only long underwear, stuck inside the ductwork on the seventh floor of the Renaissance New York Midtown Hotel on W. 35th St. near Seventh Ave. about 5:30 a.m.

Estella snuck into the hotel, but was spotted wandering around the sixth floor lobby, cops said.

When security guards tried to grab him, he gave them the slip and ran up to the seventh floor.

A security feed tracked Estella into a closed-off area on the seventh floor — but then he disappeared, cops said.

A short time later, workers smelled burning rubber coming out of the vents near the hotel’s seventh-floor pantry.

Estella, who managed to get stuck inside the vent, used a small cigar lighter to set one of his shoes on fire, cops said. He had hoped that the fire would set off the smoke detectors.

 As first responders extracted the soot-covered man from the shaft, Estella told them he crawled inside the shaft to commit suicide.

“He said he was attempting to light himself on fire,”

a police source said.

Cops charged Estella, who lives near Times Square, with burglary, arson and criminal trespass.