There are three main reasons in my view why a dog doesn't comply with what we ask.I want to understand why he listens sometimes and other times doesn't.
First, he doesn't understand. That's where training comes in, you need to teach him what you want, and reward when he gets it right so he knows he has done the right thing.
Second, the motivation or reward of doing what he is already doing is higher than the motivation or reward of doing what you are asking. This is why some dogs won't, for example, recall when they are playing or chasing squirrels. So make sure what you offer is of far higher value - or, if you can't beat something, don't allow the opportunity for it to happen (i.e. don't set him up to fail).
Third, you are working against a deeply rooted breed trait that the dog has been selectively bred for over centuries. There is a reason why we don't use terriers to herd sheep - it can be done but it is a lot harder.
You said that once he is in his bed, he sleeps all night. It sounds like he just wants to be closer to you - if his bed was right next to yours would he sleep all night there? If so, that sounds like an easy win-win.