A neighbour saw Scott MacKenzie, 20, outside her home on Tranent's Caesar Way at about 8.30pm on July 14, Alison Innes, procurator fiscal depute, told Haddington Sheriff Court last Wednesday.
"The accused smashed a kitchen window with his elbow," she explained.
"She immediately called police. They attended and he told them he in fact had a knife with him."
MacKenzie, now staying with his mother at Newbigging, Musselburgh, had pleaded guilty at an intermediate diet to smashing the window and also to having the flick knife on him at Tranent's New Row.
Robert Low, MacKenzie's solicitor, said he had had "some concerns" about his Caesar Way home and what "other people in the building may be doing".
There had been criminal activity going on and he was "slightly apprehensive".
Although he acknowledged MacKenzie's level of responsibility for the offence at first "appears concerning", Mr Low added that MacKenzie had changed his bail address subsequently after "a number of other individuals were trying to get into his flat" to exact "some form of retribution".
He had phoned police, identified one of the individuals and then moved in with his mother.
But Sheriff Pamela Bowman was concerned that MacKenzie had been carrying "not an ordinary knife" but a flick knife. Mr Low said the knife had belonged to a friend but Sheriff Bowman angrily responded: "Does he know how often the court has heard that explanation, for a flick knife?
"I'm seriously considering custody."
However, Sheriff Bowman decided to impose a year-long community payback order on MacKenzie, as part of which he will have to carry out 80 hours of unpaid work within three months.