In this usage "temper" means something like "composure". But when someone is said to "have a temper" it means they are often "out of temper" or often "lose their temper". (this obviously suggests a riddle, something like - "what can a person have even when they lose it?").
There seem to be a variety of unrelated usages : (only giving examples of usages still common today)
Temperate climate Temperance movement The Well Tempered Clavier Tempering Steel or Chocolate Hold your temper / lose your temper / out of temper / have a temper / be in good temper / temper tantrum
Interestingly, temper the verb used to be more common than temper than noun, and the primary meaning was to "mix" or "moderate" or even "compromise", such as in tempering the sweet with a little sour. This usage is now rare.
Here are some modern dictinary definitions of the archaic usage :
9. to moderate or mitigate: to temper justice with mercy. 10. to soften or tone down. 11. to bring to a proper, suitable, or desirable state by or as by blending or admixture.
And some from the 1828 Webster's :
1. To mix so that one part qualifies the other; to bring to a moderate state; as, to temper justice with mercy. 2. To compound; to form by mixture; to qualify, as by an ingredient; or in general, to mix, unite or combine two or more things so as to reduce the excess of the qualities of either, and bring the whole to the desired consistence or state. Thou shalt make it a perfume, a confection after the art of the apothecary, tempered together, pure and holy. Ex.30. 3. To unite in due proportion; to render symmetrical; to adjust, as parts to each other. God hath tempered the body together. 1 Cor.12. 4. To accommodate; to modify.
The "Tempered Clavier" of Bach seems to stem from this usage; it's a way of mixing the ideal tuning for the different keys to create a single tuning that's not quite right for any of them, but is okay enough for all of them; it's a "well mixed tuning" if you will.
Tempering Chocolate obviously comes from analogy to tempering metal, and in fact they are sort of similar, in both cases you are controlling the crystal formation by raising and lowering the temperature carefully through a small range. Understanding the origin of tempering metal is not obvious, but maybe it comes from the "compromise" origin like Bach's usage. Tempering metal is a way to acheive a good mix of hardness and softness that keeps it from being too brittle. (BTW many of the standard dictionary definitions for temper in the metallurgy usage are just wrong; you'll see definitions like "the degree of hardness" ; The Barbarian Keep has a nice little thing on tempering and a rant about misuse).
We're still left with the problem of the meaning of "temper" in reference to moods. The Webster's 1828 definition of temper has the two opposing meanings for the noun :
3. Calmness of mind; moderation. 4. Heat of mind or passion; irritation.
For laughs Wordia has the same two meanings but in opposite order :
3) noun, a tendency to exhibit uncontrolled anger; irritability 4) noun, a mental condition of moderation and calm
It seems to me that the meaning with respect to moods was originally "moderation", and perhaps just misunderstanding of the expression made it flip.
Another posibility involves another meaning of "temper". Temper can also just mean "mood or state of mind", thus you could have an ill temper, a good temper, a magnanimous temper, a generous temper, etc. Over time temper may have been mainly used in the form "bad temper" and "ill temper" and thus simply become "temper" meaning "bad mood". ( some people still use temper with other adjectives, but this is no longer standard English; presumably phrases like The Heroic Temper would now mainly be rendered as The Heroic Temperament, at least in America; I find a lot of usage in Australia of "Temper" in the more general sense).
Anyway, this leads to funny possibilities, such as : To temper his image of having a temper, Giulani shows good temper . When confronted with intemperance, to show you don't have a temper you must keep your temper.