listening to podcasts while sleeping

I have a habit of falling asleep while a podcast is being played in mpv . This means that my PC will remain powered on while I’m sleeping and I often forget to turn it off before going to work in the morning. The worst side effect is that, when I get home from work, and want to use the computer i think it is powered off, (because screen is turned off) , and pressing the powerbutton, turns off the PC.. I did this almost every day.

A solution to this would be if mpv did shutdown or suspend the system before it quits.

The functionality of mpv can easily be extended with lua or javascript user scripts , this is what i ended up with:

~/dotfiles/mpv/scripts/suspend-on-close.lua
-- Execute command on close if toggled
-- default command is: `systemctl suspend`
-- Toggle with keybinding, default: ctrl+u
-- To customize place exec-on-close.conf
-- into ~/.config/mpv/lua-settings
--
-- Place this script into ~/.config/mpv/scripts/ for autoload

local cfg = {
  key  = 'ctrl+u',
  cmd  = 'systemctl suspend'
}

local activated = false

local options = require("mp.options")
options.read_options(cfg, 'exec-on-close')

local function exec()

  if not activated then return end

  local stdcom = io.popen(cfg.cmd..' 2>&1'):read('*all')
  mp.msg.info("exec '" .. cfg.cmd .. "'")
  if stdcom then mp.msg.verbose(stdcom) end  
end

local function toggle()
  activated = not activated
  mp.osd_message((activated and "" or "DE-") .. "ACTIVATE ExecOnClose")
end

mp.add_forced_key_binding(cfg.key, toggle)
mp.register_event("shutdown", exec)