Hairmyres Hospital staff called the police when a patient refused to go home after treatment.
When officers arrived at accident and emergency, Joseph Breen launched a barrage of abuse at them.
Hours earlier he had been arrested after a disturbance at East Kilbride's Lindsay House homeless unit where he was staying.
Breen's own lawyer admitted this week his client's drink-fuelled behaviour was "disgraceful".
Breen, 54, of Mallard Crescent, Greenhills, appeared at Hamilton Sheriff Court.
He admitted threatening or abusive behaviour on October 6 and 7. He also admitted assaulting Lindsay House staff members by spitting at them.
The court heard police spoke to Breen after an incident outside the homeless unit.
He was abusive towards them, but the officers left him in the care of staff in the reception area.
Breen then spat repeatedly at two employees, only narrowly missing them, and headbutted a glass partition.
Police returned to the unit and he hurled abuse at them, threatening to "slash" officers, and warning 'If you don't take these handcuffs off I'm going to kill you'.
Breen was arrested then released only to end up at Hairmyres Hospital hours later.
The court heard nurses who treated him there concluded he was fit to go home, but he refused to move and the police were called.
Officers arrived to find him sitting on a chair with his feet on a bed.
They offered him a lift home, but he refused and became abusive, shouting: "What the f*** have I done now? I've done nothing wrong. The doctors are f****** at it. I want to go somewhere else."
Breen continued to shout and swear as he was being taken to Motherwell police station.
Breen's lawyer said he had "no real memory of events", but accepted his behaviour was "disgraceful".
Alcohol abuse had caused him long term difficulties, but in recent months he has made "significant" efforts to turn his life around.
The solicitor added: "He has distanced himself from associates who also abuse alcohol.
"The criminal justice social work report says a period of imprisonment at this stage could be counter-productive.
"I would suggest he is given a final opportunity."
Sheriff Linda Nicolson imposed an electronic tagging order as an alternative to prison. It means Breen is confined to his home between 8pm and 8am daily for 163 days.
The sheriff told him: "This behaviour is entirely unacceptable.
"You are very close to a custodial sentence if you continue in this way or don't comply with the tagging order."
*Don't miss the latest headlines from around Lanarkshire. Sign up to our newsletters here.
And did you know Lanarkshire Live is on Facebook? Why not head over and give our page a like and share.