Insomnia is a real bummer for many of us in the initial weeks after knee surgery. Definitely sleep whenever (and wherever it's safe and comfy!)
Icing, elevating, napping.... All help, but for some of us it was endangering our mental health. Before it gets there consider some nonprescription remedies....
If you're lactose tolerant, a glass of warm milk! If you're not diabetic, a teeny bit of honey makes it even more comforting... plus milk is high in magnesium! Helps us relax!
If you aren't a milk drinker, or want an easier way to, an evening magnesium supplement has been very helpful for some members.
OTC diphenhydramine, an antihistamine also used as a sleep aid (Benadryl) because it makes you drowsy; it's also included in some cold, flu, etc "nighttime" combination meds.
Low dose melatonin, sustained release - not for long term use but I have used for a week at a time without ill effect. It should be taken an hour to hour and a half before going to bed, and during that time stay in a dimly lit area to allow this hormone to activate.
Valerian root - an herbal remedy, it can be taken in capsule form but that dose might be high compared to my preference, herbal "sleep" blends that include a smaller dose of valerian and make me pleasantly drowsy. Plus the other tea flavorings make it nice tasting! Celestial Seasonings and Traditional Medicinals sell teabags by the box. Their sleep blends without valerian have never ever made me sleepy.
What finally worked for me was a combo of sleep tea AND melatonin.
Be aware: even benign herbals and supplements can interact with your routine or post op prescription meds, either making their effects weaker or stronger. If you're on meds you may want to check with your doctor or pharmacist, or even check online for interactions.