The 38-year-old father was in tears and holding one of his sons when gardaí arrived at the family home.
Judge David McHugh ordered the man to enter into a peace bond for one year, after he heard Tusla had investigated the matter and had no ongoing concerns.
The defendant, who cannot be named to protect his sons, admitted two counts of cruelty to children.
Garda Alan Ludlow told Blanchardstown District Court that gardaí went to a house in west Dublin on April 25 last year after neighbours reported hearing the defendant threaten to kill his two sons.
Gda Ludlow said the defendant had consumed three "truffles" and thought the devil was inside his sons, who were aged two and four years old at the time.
He said this appeared to be a once-off incident, and the children had not suffered any lasting effects.
Defence solicitor Patrick McGarry said his client was minding the children on the night in question, and they were upstairs asleep.
He was trying to relax at home after a hard day at work. He was drinking wine and had taken three "truffles", which were similar to magic mushrooms.
He believed the truffles would be relaxing but he took too many and suffered a "drug-induced hellish nightmare".
The solicitor said a quick google of the substance showed that "truffles" could cause a "temporary altered state of consciousness", where a person's "perception of reality changes".
The defendant became concerned that ghosts were upstairs, and he went upstairs to protect his sons. However, he got it into his head that a ghost had got into his four-year-old son, and that is when the shouting began.
Mr McGarry said the man was shouting at the ghost, rather than his son.
Mr McGarry said the defendant would "never forgive himself" for what had happened, and he believed the man would never touch drugs again.
Tusla had investigated the matter, and the defendant had attended a drugs counsellor, and had rebuilt his relationship with his children.