Kodi has a simple "shutdown timer" which you can set via the power icon in the top-left corner of the home screen. Once the timer has been set the system shuts down (without prompting to remain awake) when the timer value is reached. So if you are confident of falling asleep in ~20 mins, set the value to 30 or 40 mins and the system will be off shortly after you are.
If you are playing local video media or some form of PVR or internet stream, the system remains in an active state for the duration of the media or stream and the screensaver will not show until x minutes after the system is seen to be inactive.
Detecting when you fall asleep and sending a remote shutdown command X mins later would be a fun RPi + sensors project 
NB: Most TV's have a built-in sleep timer function. I have elderly relatives that have their TV configured for 60 mins. It prompts to reset/continue by hitting OK on the remote and if not pressed (because they dozed off) it shuts down.