[Tasker Tips & Tricks] MishaalRahman:
Sleep timers are a pretty crucial feature for anybody who likes to listen to music, podcasts, or other audio at night. Using Tasker, we can make our own custom sleep timer.

Disclaimer: this was created by Rachet_Guy over on /r/Tasker. Credits to him for putting this together!

Instructions

Create a new profile and name it "Sleep Timer".

Add a Time context, and set it to every 2 minutes (or however long you want it to run before the volume decreases).

Create a new task, and name it "Sleep Timer".

Variables --> Variable Set. Name %media_start_vol and set it to a value you feel comfortable with.

Variables --> Variable Set. Name is %vol_decr and set it to a decrement value. NOTE: you must pick a starting volume and/or a decrement volume such that when you subtract the two eventually the value should equal 0, otherwise Tasker will not be able to set the final volume level and will fail.

Task --> If. If %SleepTimerRunning != 1 AND %PENABLED !~ *Sleep Timer. This is basically checking where I'm at. If I haven't started the sleep timer yet, then we'll start it from the beginning.

Alert --> Flash. Text: SLEEP TIMER - CYCLE BEGIN.

Audio --> Media Volume. Set Level to %media_start_vol.

OPTIONAL: if your music player supports an intent broadcast or has a plugin to start audio playback, then you can create an action to start it here. Otherwise, start audio playback before running the task.

Tasker --> Profile Status. Name: Sleep Timer. Set: On.

Task --> Wait. Wait 1 second.

Variables --> Variable Set. Name %SleepTimerRunning To 1.

OPTIONAL: Plugin --> Zooper Widget Variable. Configuration: #TSLEEPTIMER# = 1. This is if you want a Zooper widget that shows when the sleep timer is on.

Task --> Else If. If %SleepTimerRunning = 1 AND %PENABLED ~ *Sleep Timer* AND %VOLM > 0. This is checking to see if the sleep timer is still active and if the media volume is still active.

Variables --> Variable Set. Name %curr_vol to %VOLM. Set the current volume variable to the media volume.

Variables --> Variable Set. Name %next_media_vol to %curr_vol-%vol_decr. We're decrementing the current volume.

Audio --> Media Volume. Level: %next_media_vol. Changing the media volume.

Alert --> Flash. Text: VOLUME LOWERED TO - %next_media_vol

Task --> Else.

Tasker --> Profile Status. Name: Sleep Timer, Set Off.

Task --> Wait. Wait 2 seconds.

Media --> Media Control. Cmd: Toggle Pause. Simulate Media Button: On. For App: choose the music app you want it to broadcast to. Check If and set it to If %SleepTimerRunning eq 1.

Variables --> Variable Set. Set %SleepTimerRunning to 0.

OPTIONAL: Plugin --> Zooper Widget Pro Variable. Configuration: #TSLEEPTIMER# = 0. We're toggling the Zooper Widget to show us that it's off.

Audio --> Media Volume. Level: %media_start_vol

Alert --> Flash. Text: SLEEP TIMER - CYCLE END

Task --> End If.

Exit out, and the profile creation is done.

Now, TURN OFF THE PROFILE! You don't want it running unless you explicitly tell it to! You can run it in many different ways, me personally I use a Zooper Widget to make it run, but you can simply create a homescreen shortcut using Nova or Apex Launcher to launch the Sleep Timer task.

Here are screenshots of the task:

I'll attach the XML file below. Import it by moving it to /sdcard/Tasker/Profiles, then opening Tasker and long-pressing on the Profile tab up top then pressing Import.