Having the same problem with my S5. Screen doesn't rotate at all until I reboot then it works fine for about 5-10 minutes then back to no rotation.
My S-5 has had that problem, but I didn't realize what it was until I needed to do some professional installation work, utilizing a compass application. It simply wouldn't work. Fortunately, I had an older S-3 on hand with the same app, and I was able to complete my work. But ...
Just FYI ... The problem is with the mobile device, in my case. I did a system reset/restore. No good. Same results ....
The accelerometer sensor is defective or inoperable:
Using keypad, I got into the sensor diagnostic (*#0*#). Checked "Sensors" Accelerometer is stuck on angle 90 (thankfully).
x angle: 0 y-angle: 0. Image test does not move from vertical.
Magnetic Sensor is working. Azimuth:0 Pitch: 0 Roll:0
Self test "Good"
Sending the device back to Samsung.
My guess: The accelerometer sensor drives Azimuth Pitch and Roll utilized in the magnetic sensor. I did attempt using the magnetic sensor (as suggested) and the app still didn't work, at all.
What really pissed on me is that the device is 14 days out of warranty, but the problem occurred at least two months ago, as the screen hasn't auto rotated for at least that long when I used the device for GPS navigation in my car. I've contacted Samsung on the issue, and I'm sending it back for repair.
C