Caffeinate with Tasker — Improved version

Alberto Piras
Geek Culture
Published in
4 min readOct 22, 2021

In this article, I described a Tasker project to increase the display timeout with a task.

The problem with that version was that the timeout steps were hard-coded, making the Caffeinate task intricate, repetitive, and hard to edit.

I decided to invest some time and improve it as follows.

Project description

The project now works using the following variables:

  1. %caffeinateState: it is true when the task is active; false otherwise.
  2. %caffeinateDefault: defines the default timeout in seconds.
  3. %caffeinateSteps: it’s an array that contains all the steps of timeouts the task uses.
  4. %caffeinateStepIndex: marks which step from the %caffeinateSteps array is active.

It contains the following tasks:

  • Caffeinate Setup: asks the user for the default timeout and the various steps that the Caffeinate task needs. It runs when importing this project.
  • Caffeinate: cycles through the steps and, when it reaches the last one, it resets the timeout to the default value. It shows a notification to go to the next step or reset the timeout to its default value.
  • Caffeinate Reset: restores the default timeout.
  • Caffeinate Old: the original task from the story at the beginning.

There is also a profile, called Caffeinate Reset, that runs the homonym task when the user turns off the display. You can read more about it in Step 2. Profile from the original article.

You can download the project here.

Improved tasks overview

In the following paragraphs, I’ll explain the tasks introduced before.

Caffeinate Setup

It performs the following actions:

  1. Anchor: defines a “Prepare array” label.
  2. Array Clear: clears the value of the %caffeinateSteps array.
  3. Variable Set: sets the value of the %caffeinateStepIndex variable to 0.
  4. Anchor: defines a “Define default” label.
  5. Input Dialog: asks the user for the default display timeout in seconds and saves it in the %input variable. It accepts a number.
  6. Variable Set: sets the value of the %caffeinateStepIndex variable to %input.
  7. Anchor: defines a “Define step” label.
  8. Variable Add: increases the %caffeinateStepIndex variable by 1.
  9. Input Dialog: asks the user for a step of display timeout in seconds and saves it in the %input variable. It accepts a number.
  10. Array Push: adds the %input to the %caffeinateSteps array at %caffeinateStepIndex position.
  11. Pick Input Dialog: asks the user to add another step and saves it in the %input variable. It is a yes/no dialog.
  12. Goto: goes back to the Anchor with the “Define step” label if the value of the %input variable is yes.
  13. Anchor: defines an “Initialize Caffeinate” label.
  14. Perform Task: runs the Caffeinate Reset task to restore the display timeout and the values of the variables.

Caffeinate

On the left, the Caffeinate task. On the center and the right, the configurations of respectively Action 6 and 7.

It performs the following actions:

  1. Anchor: defines an “Initialize variables” label.
  2. Variable Set: sets the value of the %caffeinateState variable to true.
  3. Variable Add: increases the %caffeinateStepIndex variable by 1.
  4. If: branch of execution that runs if the %caffeinateStepIndex variable is less than or equal to the %caffeinateSteps array size.
  5. Display Timeout: sets the display timeout to the value of %caffeinateSteps at the %caffeinateStepIndex.
  6. Notify: shows a notification as explained below. It runs this action only if the %caffeinateStepIndex is less than the %caffeinateSteps array size. It shows the two buttons described below.
  7. Notify: shows a notification as explained below. It runs this action only if the %caffeinateStepIndex is equal to the %caffeinateSteps array size. It shows only the Stop button described below.
  8. Else: branch of execution that runs if Action 4 condition is false.
  9. Perform Task: runs the Caffeinate Reset task to restore the display timeout and the values of the variables.

Actions 6 and 7 show a persistent notification with information about the display timeout. It has the following buttons:

  • Increase: increases the timeout by calling the Caffeinate task with the Perform Task action. This button appears only when there are other steps available.
  • Stop: restore the display timeout by calling the Caffeinate Reset task with the Perform Task action.

Caffeinate Reset

It performs the following actions:

  1. Variable Set: sets the value of the %caffeinateStepIndex variable to 0.
  2. Variable Set: sets the value of the %caffeinateState variable to false.
  3. Notify Cancel: removes the notification shown by the Caffeinate task.
  4. Display Timeout: sets the display timeout to the value of the %caffeinateDefault variable.

Caffeinate Old

You can refer to Step 1. Tasks > Caffeinate from the original article for its description.

Usage

You can refer to Step 3. Quick Setting tile from the original article for an example of usage as Quick Setting tile.

--

--

Alberto Piras
Geek Culture

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