I roast my own coffee beans so was inclined to drink so much it affected my sleep patterns. I started substituting decaffeinated coffee for my second pot of the day (mid afternoon). Commercial decaf coffee generally sucks because they use the cheapest coffee beans they can find, and extract the caffeine with solvents. I am able to buy quality green coffee beans decaffeinated using a water process, so self-roasted decaf can taste as good as high test, just without the caffeine kick..
These days I stopped making my second pot of decaf daily, but every two weeks I roast up a half pound of good decaf for my neighbor's widow who is even older than me. It is a simple pleasure and she appreciates it because store bought decaf is like you know.
======
Just like we don't see cat skeletons up in trees because they will come down by themselves eventually, our body will force us to sleep eventually sometimes at inconvenient times. The body can do quite well on only a couple hours a night for extended periods, maybe a short cat nap mid day. The old joke says there are two things you can't catch up on and one of them is sleeping (use your imagination about what the other is, but also done in bed). Kind of true you can't make up for long term sleep deficits but in the short term a decent snooze session can recharge your batteries and normalize your systems.
Mental stress can keep us awake worrying about some impending drama, and stimulants (like caffeine) consumed too late in the day can have some effect on our alerting mechanism. If you have trouble sleeping the best thing is not to "think" about it, but that is easier said than done. I have heard about breathing exercises that reportedly work, like counting sheep, but focussing on inhaling and exhaling.
JR
PS: There are some amusing stories about embarrassing things like eating or worse, that people have done while asleep after taking a popular sleeping aid.