Try to have a consistent work time each day. Give yourself enough time to complete the important tasks for the day, leave the ones that can be done another day for another day. Sleep is important so try to prioritize tasks during your work time that have to be finished that day.
It’s all about being aware and taking the right action in the moment. If it’s the afternoon and it’s the right time to do work, then sit at the desk and do work no matter how unfocused, how tired, how uncomfortable you are, how slowly you work, or what quality of output you’re creating. Eventually, the habit will settle in.