Gabriel Yad-Elohim's lawyer says her client was hearing voices and had only been out of Auckland Hospital's mental health ward for three days when he killed a man.
Mr Yad-Elohim is accused of stomping on the head of Michael Mullholand and murdering him in September last year.
He has pleaded not guilty by reason of insanity.
Mr Yad-Elohim's lawyer Annabell Cresswell told the jurors her client had been diagnosed with schizophrenia and it was resistant to treatment.
He had turned up to Auckland Hospital's Te Whetu Tawera, the psychiatric ward, and was initially held for five days. That term was extended for a further two weeks after he was found to be psychotic and hearing voices.
However, Mr Yad-Elohim did not stay there for the full term.
"When Te Whetu Tawera became full on the 23rd of September 2017, three days before the death, Mr Yad-Elohim was reviewed by an on-call consultant and discharged into respite care in the community.
"The defence say that Mr Yad-Elohim believed God or another entity was talking to him at the time of the offending and telling him what to do."
Mr Yad-Elohim had also changed his name.
"In fact, the name Gabriel Yad-Elohim is a name he has given to himself and it means: 'messenger of God'."
She told the jurors that both the Crown and defence agreed Mr Yad-Elohim was involved in the killing.
"The issue for you, ladies in gentlemen, in this trial, is whether he was labouring under a disease of the mind to such an extent as to make him incapable of understanding the nature and quality of the act, or knowing that the act was morally wrong."
Ms Cresswell said Mr Yad-Elohim has been in the custody of the forensic mental health unit, the Mason Clinic, since shortly after being arrested for the killing.
'Truth is stranger than fiction'
But the Crown prosecutor Kirsten Lummis said Mr Yad-Elohim was angry after being ripped off in a drug deal.
She said Mr Yad-Elohim met a transgendered woman on Auckland's Karangahape Rd and asked her to help him get drugs.
He was taken to a flat in nearby Western Springs and told to wait in the stairwell after handing his money over but the woman never returned - instead she actually climbed down the balcony and ran off with his money.
Ms Lummis said Mr Yad-Elohim realised he had been swindled and called Mr Mulholland to the door.
"You will see then Mr Mulholland being pulled from the apartment and head-butted. Mr Mulholland tries to put up a fight, you'll see him swing some punches but they're ineffectual on a much younger, fitter man."
The entire attack was caught on security cameras and the disturbing footage has been played in court today.
Mr Yad-Elohim continued with the attack once Mr Mulholland was on the ground.
"From what you can see on the CCTV, it's fair to say he's quickly rendered unconscious and there is very limited movement to his body as it sustains a brutal beating with kicks and stomps being administered to his head and torso area," Ms Lummis said.
Ms Lummis said at one point Mr Yad-Elohim stopped the attack and walked off, only return and continue the attack.
Mr Mulholland's body was found by a neighbour later that night.
Mr Yad-Elohim was arrested the following day.
Ms Lummis said it was hard to understand why someone would be so outraged about losing $200 that they would kill someone.
"Truth is stranger than fiction. This is perhaps crazy, it's odd, misguided but not legally insane, the Crown would say."
She said it was for the defence to satisfy the jury that Mr Yad-Elohim was insane at the time.
"Yes, he has schizophrenia, yes that's a problem - he's unwell but ... that's quite separate from the motivation for this attack which was related to the fact that he's standing there thinking: 'I've just been ripped off $200'."
Ms Lummis said the matter was also complicated by Mr Yad-Ilohim's use of methamphetmine and cannabis.
The trial before a jury and Justice van Bohemen has been set down for two weeks.