Caffeinate with Tasker

Alberto Piras
Geek Culture
Published in
3 min readSep 10, 2021

--

I keep the timeout of my phone set to 30 seconds. Usually, this is enough time, but sometimes I need to keep my screen on longer.

There are several apps called Caffeinate on the Play Store that do precisely this. Unfortunately, the one I was using stopped working for me since Android 11, so I made an alternative with Tasker. Today, I’ll show how I accomplished this.

Behavior

The applications usually keep the screen on for some time defined by a timer. After the time is up, the screen turns off. The first tap on the Quick Settings tile starts the timer while subsequent taps increase it.

With Tasker, I preferred a different approach. With each tap on the Quick Settings tile, the display timeout increase. The default timeout is 30 seconds, the other steps are:

  • First tap: 1 minute.
  • Second tap: 5 minutes.
  • Third tap: 10 minutes.
  • Forth tap: 5 hours.
  • Fifth tap: default timeout.

Update: You can find in this story the download link for an improved version personalizable with a task. It allows customizing the default timeout and each step.

Original: You can download the project here. Feel free to adjust the steps and the default timeout according to your needs.

Step 1. Tasks

The project has the following two tasks.

Caffeinate

This task increases the display timeout. It uses the following variables:

  • %caffeinateState: defines if the function is active. Its value is either true or false.
  • %caffeinateDefault: is the default display timeout. I manually defined this.
  • %DTOUT: is the display timeout value in seconds.

It performs the following actions:

  1. Variable Set: sets the %caffeinateState variable to true.
  2. If: checks if the %DTOUT variable is equal to %caffeinateDefault.
  3. Display Timeout: sets the display timeout to 1 minute.
  4. Else If: checks if the %DTOUT variable is equal to 60.
  5. Display Timeout: sets the display timeout to 5 minutes.
  6. Else If: checks if the %DTOUT variable is equal to 300.
  7. Display Timeout: sets the display timeout to 10 minutes.
  8. Else If: checks if the %DTOUT variable is equal to 600.
  9. Display Timeout: sets the display timeout to 5 hours.
  10. Else: if the previous conditions were not met.
  11. Perform Task: runs the Caffeinate Reset task to restore the display timeout.
  12. End If: closes the conditions.
  13. Notify: shows a persistent notification with information about the display timeout.

The notification allows the user to:

  • Increase: increases the timeout by calling the task Caffeinate with the Perform Task action.
  • Stop: restore the display timeout by calling the task Caffeinate Reset with the Perform Task action.

Caffeinate Reset

This task restores the display timeout, resets the variable, and removes the notification.

It performs the following actions:

  1. Variable Set: sets the %caffeinateState variable to false.
  2. Notify Cancel: removes the notification.
  3. Display Timeout: sets the display timeout to %caffeinateDefault seconds.

Step 2. Profile

This profile reset the display timeout to its default value when the screen turns off.

It triggers with the following conditions and runs the Caffeinate Reset task.

  • Display Off Event: this happens when the display turns off.
  • Variable Value: checks if the %caffeinateState variable is true.

Step 3. Quick Setting tile

It is possible to set the Caffeinate task as a Quick Setting tile by adding it to the Quick Settings Tasks in the Tasker preferences under the Action tab.

You can then add it to your Quick Settings and use it.

--

--

Alberto Piras
Geek Culture

Software development engineer at Amazon. Thoughts and articles are my own.