My truck will start out in 4WD, lights do not work sometimes. on 2005 GMC Sierra 1500

It does it usually when I first start it. Started out just doing it every 3 months, now it does something crazy every time I start it. I have read similar posts and they recommend replacing the Encoder Motor Sensor. Is this an expensive fix? Sometimes it starts and says it is in Neutral, sometimes Auto only, sometimes no lights are working. Sure is scary to pull out and realize it is in 4 Low.

Asked by for the 2005 GMC Sierra 1500