I set it in routines. To have my phone be unlocked when I am in my car as well as my house. All other times, when the reverse happens, phone is set to lock.
It is just random. For example, today, the routines functioned correctly. I got in my car, phone unlocked. I got to work, phone locked and...