Making a Scene over a Power Cut

December 26, 2012

Vista busy cursor  Last Sunday we woke early to find the power was out, and clearly had been for hours. It wasn’t just the house circuit-breaker which had tripped; the whole street was dark. I used my phone to check the Scottish Power website and found that the problem was known about and a fix expected by around 10am. In the event the power came back on a little earlier than that.

There was no harm done; the outage was not long enough for food in the freezers to start thawing.  All the same, I wondered if there might have been some way I could have been alerted earlier. It is not as if we get power cuts every other week, but this happens two or three times a year and an outage just after we had gone to bed might go unnoticed until the morning, easily long enough to risk problems with the food in the freezers.

A thought had come to me as I picked up my phone to check the power company’s website. My phone was plugged in to the charger, as it is every night. At the hardware level, the circuitry would have detected that the battery was no longer charging when the power cut out. Could this not have been intercepted programmatically and made to trigger an audible alert?


I searched the Google Play Store for apps that could be used to detect power cuts. I only found two of note. One was a paid app designed to detect outages on key circuits, e.g. the circuit that powers the freezers, at a holiday home or business – in any event somewhere remote. It would send a text to a chosen mobile number to warn of an outage. But I did not want to be alerted by text; I just wanted a simple audible alarm to warn of a local residential area power cut. This app could not do that.

There was another app which did have the audible alert functionality I was after. Except that it was very poorly written. Once the alarm had started sounding, there was no way to stop it without rebooting the phone! Both apps were uninstalled in double quick time.

For a short period I seriously thought of writing my own app to do the job properly, maybe marketing it.  I have been dabbling in Android app development and am well up to the task. Then I had my second lightbulb moment. I could probably program the Tasker app to provide the functionality I wanted. Tasker is a wonderful app which can be used to customise the behaviour of Android phones in any number of creative ways. I’ve written posts about Tasker before, for example the profile I created to ensure notification sounds were muted at night time. To use Tasker you specify “what” should happen and “when”. The “what” will be some specific action you wish to happen automatically, such as a change in notification volume, turning on the wi-fi or the sounding of an audible alarm. Determining the “when” consists of creating “profiles”, descriptions of particular statuses, such as proximity to a particular geographical location, the period between specified hours of the day, etc. One of the options for profile is “on AC power”, so Tasker can be programmed to trigger actions when the phone is plugged in to the mains and when it is disconnected. Note that from Tasker’s perspective there is no difference between unplugging the charger and a power cut. Either way, the hardware detects that the supply of AC power has stopped.

It is very easy to create a Tasker profile which detects when AC power is disconnected and sounds an alarm. Unfortunately, that would cause the alarm to sound every time I disconnected the charger in the morning. I needed something a bit cleverer – maybe arrange for the sounding of the alarm to be deferred for a minute or so and meantime display a dialog box which the user would press to cancel the alarm. That way, each morning when I took the phone off the charger I would see the dialog box and tap to prevent the alarm going off. If, on the other hand, there was a power cut during the night I would sleep through the display of the dialog box and be awakened by the power cut warning alarm a minute later. The question is whether Tasker was clever enough to make such a solution possible.

The answer is yes. At one time it would not have been. What has made the difference is the introduction of “Scenes” into its functionality around a year or so ago. When Scenes were first announced, I was not sure what to do with them. I had a brief play but did not really “get” them so left them be. It was only when I started thinking about Tasker and power cuts that I realised I could use a Scene to add the dialog box functionality that would make the whole idea workable. The Scene I created is a dialog box occupied in its entirety by a button. When the phone exits the “on AC power” profile, it kicks off a series of actions.  The first is to display the dialog box with its button, labelled “Cancel Disconnection Alarm”. The second is to set a “user variable” named %POWERCUTALARM to status ON. It then performs a 60 second “wait” operation and the third is to play an mp3 file (the power cut alarm) but only if  %POWERCUTALARM is still set to ON.

Should the user have tapped on the button in the dialog box before the 60 second wait had run its course, this would have triggered the execution of Tasker commands to set %POWERCUTALARM to OFF with the result that the alarm mp3 would not play. 

I have tested all this with a simulated power cut, the simulation taking the form of switching the mains off at the socket where the charger is plugged in. It may be a while before we get a real power cut to test with, but I’m happy if that does not happen for months and months.

There is a postscript. Another idea, but less of a lightbulb and more a slap on the head.  This is to do with the Tasker profile which mutes notifications at night. I had this originally set so the profile would be active at specific times of the day, e.g. coming on at midnight and off at 8am. Those times proved to be too rigid, so I later moved on to switching the profile on and off manually by linking the profile to a button on my phone’s desktop. But the best solution is to have notifications muted when the phone is on AC power, because that corresponds exactly to when I am in bed asleep. But until the power cut incident I had never thought to check whether Tasker could respond to changes in mains power connection status. I have now adapted the Tasker Power Cut profile to also control muting of notification sounds.  All obvious in hindsight.




  1. When setting up a profile you choose “State” then “Power” then “Power” again. Under the heading “Source” there is a drop-down which is initially set to “Any” but you can change that to AC, USB or Wireless.

    • OK understood but how do I insert a wait of 2 mins BEFORE I choose state then power….

      • Maybe you could have two profiles, one triggered by connecting an AC power source and another triggered by connecting a USB power source. In both cases they would just run a task that ran a two minute wait then changed the value of a user variable say %POWERCHECK from 0 to 1. You would then have two more profiles, one triggered by both being connected by AC and %POWERCHECK = 1, another triggered by both being connected by USB and %POWERCHECK = 1. Only the “right” profile would be triggered depending on whether Tasker was now (after 2 mins) recognising phone as being on AC or USB. You could have the latter 2 profiles action whatever tasks you wanted, but remember to include resetting %POWERCHECK to 0. I’m not certain this will work but you could try something along those lines.

  2. Happy to read your blog post. Can you please help me. I am looking to create a profile that would tell me if the power source is USB or not. However, my challenge is I want tasker for wait for 2 minutes before it checks for the state of the Power source. So something like:

    – Wait for 2 minutes (I want to do this because even after plugging in AC, it initially shows USB as power source for sometime & then changes to AC Power)
    – Check if Power source is USB
    – If yes, notify “not efficient, please charge with AC power”

    Please please please guide me. Thanks!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: