Yea basically sin works on a three strikes rule, all three conditions must be met for it to be a mortal sin:
Grave matter
Full knowledge
Full consent
Meaning that murder is a mortal sin, because it violates the sanctity of human life (grave matter), you know full well it is wrong (full knowledge), and murder is typically pre-meditated (full consent). Manslaughter on the other hand, only fulfills two out of the three (the first two), so is actually venial rather than mortal.
I’m in the same position you are, and I really do know how it feels, on both sides, and it is a sin, but it is minor (venial). Here’s why:
**
Grave matter
**Check, masturbation is contrary to its purpose. Sexual pleasure is between a married man and woman.
Full Knowledge
Check, you know why it is sinful, otherwise you would not be struggling against it.
Full Consent
Nope. Not only were you struggling against it, but you were not in a position to make a decisive moral choice. You were half asleep.
I’ve done that a couple of times, and I remember I was almost in shock when I looked at myself. But then, if I was in shock, then it must have been a surprise which means I did not give my full consent.
The act of contrition and realisation that it is wrong was the right thing to do. Now you should keep praying. Mary is awesome for company, as well as St. Joseph, and also spend some time alone with Christ and the Holy Spirit.