when should you promote your pawn into a knight

When playing I realized you could turn a pawn into any peice including a knight when is turning a pawn into a knight better than turning it into a queen
That will depend on the situation, but there are times when under promotion is needed to prevent stalemate. Promotion to a knight might be wanted to give check or be better as a tactic to win material.

Knight promotions are actually the second most common promotion, after queens.
The rarest promotion is the bishop - this is only done in extreme cases where promoting to anything else would lead to stalemate.

Years ago (I think the 1980s) in the Israel Woman's Championship. One side had a Pawn and 2 Knights, the other only one Knight. If the Pawn did not move to the promotion square it would be taken by the Knight and if promoted to QRB would fall victim to a fork. Leaving one side with only the two Knights which cannot force Mate. So the player promoted to a Knight! Come to find out 3 Knights can force mate against a sole Knight. WHO KNEW!!

When playing I realized you could turn a pawn into any peice including a knight when is turning a pawn into a knight better than turning it into a queen
You may need to make it knight-promote to Get Out of Trouble !
If there's a knight-promote Check available.
Usually - there' isn't.
Because the other guy's King isn't in the right spot for it.
But that knight-promote check can save your King's life in some situations.
(Reminds me of somebody in park chess: "If you lose - You Die")

Point: Queen-promote might not give you the tempo you need to stop your opponent mating you. For example.
If its a check - it might. But it often wouldn't be.
Material - tempos - position.

Years ago (I think the 1980s) in the Israel Woman's Championship. One side had a Pawn and 2 Knights, the other only one Knight. If the Pawn did not move to the promotion square it would be taken by the Knight and if promoted to QRB would fall victim to a fork. Leaving one side with only the two Knights which cannot force Mate. So the player promoted to a Knight! Come to find out 3 Knights can force mate against a sole Knight. WHO KNEW!!
Check out this famous endgame study: A. Herbstmann and L. Kubbel, 1937, 1st prize. At the end Black promotes to a third knight for the reason that you mentioned (a queen would be lost to a knight fork). So with 3 knights vs 1 knight, Black should normally win. But then White sacrifices the last knight, forcing the 3 black knights to line up in such a way that the white king can fork all 3 of them!! Black has one final move that could save all 3 knights, only to stalemate White.

I like that one ! Triple Fork !
Sounds like the name of somewhere in a Western movie and there's an Ambush.
And that can be visualized. The lone King up against the central knight of the three- and that King is therefore Locked as soon as the other King gets opposite. Even in the center of the board - the knight/King control would encircle the other King. But he's got that Zone in the eye of the storm. That one square.
That may be a good way to think about three knight theory.
Or a good starting point - from which to develop or think about the theory of three knights.

You could have a little chessboard on your safe combination.
Gotta make moves instead of pressing numbers.
I once promoted to a knight to fork my opponent's king and queen. It wasn't completely necessary, a queen would have been winning as well (and I don't know what the engine would have said) but it was more convenient to just get rid of the opposing queen. That's the only time I've underpromoted for an actual reason besides not having the piece with me (in casual games, a friend of mine once insisted I mate him with a rook instead of a queen).

In the 50,000+ chess tactics problems - there's many beautiful underpromotion problems.
"things aren't always what they seem to be"
Many players overconfidently and too quickly promote to queen ...
and fail the problem. Causing a minus score and minus tactics rating points.
They call them 'puzzles' nowadays.

heres a puzzle an underpromotion puzzle i made
why must you promote to a rook?
Took me about 20 seconds to see why.
But whatever the length of time - nobody is going to understand the why of that problem unless 1) they spot black's pawn at d2 at the bottom of the board in post #13 here ... and 2) they start thinking about that pawn and what its presence on the board means
Nobody will ever think about something unless they start thinking about that something !!
When playing I realized you could turn a pawn into any peice including a knight when is turning a pawn into a knight better than turning it into a queen