Rule no.1 of my household:

No mobile notifications in bed

Creating a smart sleep profile  or sleeping on a sofa?  This is where the Tasker comes in. I know, that you already figured out  how to do time conditions, however my profile is little more advanced. I work nights, away from home. I also stay up late at home, when an article like this one needs writing. So my sleep profile needs to account for these possibilities. We are going to use our previous project Home Profile to determine when our sleep profile is enabled.

The plan

Before you open the Tasker and start adding actions and profiles, it’s a good idea to write down, or draw a logic chart of what you are going to make. This doesn’t have to be anything sophisticated, just enough details to outline your plan of action.

Please don’t try to read into the notes too much. It’s a road map of what I’m going to do with the profile. This can help you find any potential problems before you start to create anything in Tasker. I do this a lot. In fact I have a dedicated notebook, as most of the time I will use a pen and paper over Journal software.

I’m going to create a main profile, which will enable/disable the sleep mode on my mobile. This profile will be toggled by supporting profiles, one for home location, and one for ‘not home’. The supporting profiles will trigger notifications, that will allow me to either cancel, or postpone sleep of my device when I’m home, or ask to enable the sleep profile when I’m not home. Notifications will have a button to launch independent tasks to create appropriate behaviour.

Profiles

There are three profiles:

  • Sleep Time (uses a time condition where our starting value is a variable  %SleepTime, and ends at 7am)
  • Warning at Home (uses the variable %Home=1 from the previous tutorial – home profile, and time condition to launch the task an hour before the sleep time)
  • Warning Outside (uses the variable %Home=0 from the previous tutorial – home profile, variable set when I’m not at work %Work=0 and time condition to launch the task an hour before the sleep time to ask if I need the sleep profile)

Sleep Profile is linked to 2 tasks Sleep+ (in) and Sleep- (out), and this is where I will enter all my night mode adjustments (silent mode etc). For now, I will just add an alert action as a place holder. Keep in mind that we will have to come back to Sleep- to add a Cancel task, and to Sleep+ to add a notification that our phone is in a sleep mode.

Warning at Home is linked to Enable Sleep which Sets  %SleepTime=23 (1h later), enables our Sleep Profile and issues an Sleep Profile notification with options to Postpone or Cancel (both actions will trigger special tasks).

Warning Outside just launches new notification also called Sleep Profile, which has 3 actions: Set Now, Postpone, Cancel – each will launch tasks corresponding to their names.

Because I don’t have any tasks I want to run when the buttons are pressed, I will only assign label name and an icon for now, and when this is done, I’m going to create the needed tasks. Additional tasks will consist of:

  • Cancel (will wipe all variables used, and cancel notifications visible on the screen, will also disable the Sleep Time profile)
  • Postpone (will set the %SleepTime to an hour later, cancel currently open notification, then display confirmation of the Sleep Time profile)
  • Sleep Now (will launch Sleep Time profile with a 30 sec delay, cancel all existing notifications )

Tasks

Cancel and Postpone tasks are used in several notifications. Once created, you will have to go to each notification, check what options (buttons) have you assigned to the notification, and add action Run Task, then select the correct one.

Cancel

A1: Notify Cancel [ Title:Sleep Profile Warn Not Exist:Off ]

A2: Notify Cancel [ Title:In Sleep Warn Not Exist:Off ]

A3: Notify Cancel [ Title:Pending Sleep Warn Not Exist:Off ]

A4: Variable Clear [ Name:*Time* Pattern Matching:On ]

A5: Profile Status [ Name:Sleep Time Set:Off ]

 As we are using limited notification names – we need to make sure when a cancel button is tapped – all of them  named the same way, will be removed. (A1,A2). Next, the variables are cleared, I could use Variable clear action twice, however, you can select pattern matching and use RegEx to find the names of variables to clear. If you want to learn more about RegEx in Tasker see this! Lastly the sleep profile is disabled.

Postpone

A1: Notify Cancel [ Title:Sleep Profile Warn Not Exist:Off ]

A2: Variable Set [ Name:%SleepTime To:%TIMES + 3600 Do Maths:On Append:Off ]

A3: Variable Convert [ Name:%SleepTime Function:Seconds to Date Time Store Result In:%PostponeTime ]

A4: Notify [ Title:Pending Sleep Text:The sleep will be enabled at %PostponeTime Icon:hd_aaa_ext_bulb Number:0 Permanent:Off Priority:3 Actions:(2) Run Task: Postpone, Cancel ]

Once notification is cancelled we are using %TIMES system variable (Time in seconds)  and we are setting it to 1h later by adding 3600 seconds (make sure do math is selected). This will set our variable %SleepTime to the time an hour from now. To make the value easier for us to read I use convert variable, and display the time in the normal format in the next notification under %PostponeTime – this notification also has 2 buttons: Postpone and Cancel.

Sleep Now

A1: Variable Set [ Name:%SleepTime To:%TIMES + 30 Do Maths:On Append:Off ]

A2: Profile Status [ Name:Sleep Time Set:On ]

A3: Notify Cancel [ Title:Sleep Profile Warn Not Exist:Off ]

A4: Notify Cancel [ Title:Pending Sleep Warn Not Exist:Off ]

This task will run when Set Now button is pressed in the notification given from Warning Outside. It cancels the notifications present on the screen and sets our profile Sleep Time on.

End Thoughts

As you see, Tasker can be very flexible. I’m able to set the smart sleep profile to run only when needed, and even then I have the option to adjust the settings. The project like this can be also linked to your alarms and other location-based profiles. It is possible to use Tasker to create an elaborate night mode that will disable other devices, lights and whatever you desire. For now, let’s leave it here and in the next article, we will have a look on the usage of plugins and variables more. Files for download and detailed description available in here.