Thursday, May 16, 2013

[Tasker] Energy saving mode

Battery life isn't great on smartphones these days... So it's no surprise the Nokia 105 was in the news when it was announced earlier this year. It claims to have 30 days of battery life and is sold for only 20$. It would be nice to have a smartphone lasting that long. I will show you an easy way to expand the battery life of an android smartphone with Tasker. Nowadays smartphones are online all the time, radiating information continuously. I noticed that it makes a huge difference in battery life wether I'm connected to a WiFi AP or not. Even data connection (4G/3G/EDGE/GPRS) seems to have a significant impact on the standby time of my phone. Permanently disconnecting from the internet is no solution in the 21. century, you will miss all the notifications, messages and other goodnesses of our information age. A solution I came up with is to connect to the network only once every 14 minutes.

Our task, lets call it "periodic internet", will disconnect the phone from the network for 14 minutes, then connect for 1 minute and get all the notifications before starting over again by disconnecting for 14 minutes... All calls and SMS will be received in real time, while all the notifications will be delayed by up to 14 minutes. The benefit is the significantly improved battery life. Because of this task, my phone uses only 2% of the battery per hour! I configured Tasker to run this task while I'm at the gym, or at night when not connected to a power supply. Sometimes I activate it manually, because I know my phone won't make it trough the day otherwise.

What are we going to do:
  • We will configure a widget on our home screen one can tap to activate and deactivate the "periodic internet" task
  • We will show a permanent notification while running this task, after clicking the notification the task we be canceled and the permanent internet connection will be restored
  • The notification will show the time when the phone was connected to the internet the last time and how often the internet has been turned off and on already
What do we need:
  • Android phone
  • Tasker - create your own small programs
  • Zoom - an app able to create interactive widgets, install it after Tasker.
  • Crystal Project HD icon pack - we will use one of the icons
  • 10-20 minutes lifetime

The "periodic internet On" task

The task checks which internet connection radios are on, saves them into global variables for later use and turns WiFi and data off for 14 minutes, after that it turns them on for 1 minute before repeating itself by turning them off again. At the beginning of the task a zoom widget is modified, I will explain this later. Moreover a notification informing that the task is running is shown. Skip the line A2, add it later after you have created a "Periodic internet widget" as described later in this post.
Periodic internet On
  Abort Existing Task
  A1: Variable Set [ Name:%PERIRADIO To:1 Do Maths:Off Append:Off ]
  A2: Zoom Element Visibility [ Element:Periodic Radio.w / StateON Set:On ]
  A3: Variable Set [ Name:%num To:0 Do Maths:Off Append:Off ]
  <Loop>
  A4: Anchor
    A5: Variable Add [ Name:%num Value:1 Wrap Around:0 ]
    A6: Notify [ Title:Periodic internet Text: Icon:ipack:crystalhd:quick_restart Number:%num Permanent:On Priority:3 ]
    A7: Variable Set [ Name:%MOBILDATA To:1 Do Maths:Off Append:Off ] If [ %AIR ~ off ]
    A8: Variable Set [ Name:%MOBILDATA To:0 Do Maths:Off Append:Off ] If [ %AIR !~ off ]
    A9: Variable Set [ Name:%WLANTMP To:1 Do Maths:Off Append:Off ] If [ %WIFII ~R CONNECTION ]
    A10: Variable Set [ Name:%WLANTMP To:0 Do Maths:Off Append:Off ] If [ %WIFII !~R CONNECTION ]
    A11: Variable Set [ Name:%noradio To:%MOBILDATA+%WLANTMP Do Maths:On Append:Off ]
    A12: If [ %noradio ~ 0 ]
      A13: Stop [ With Error:Off Task: ]
    A14: End If
    A15: Mobile Data [ Set:Off ]
    A16: WiFi [ Set:Off ]
    A17: Wait [ MS:0 Seconds:0 Minutes:14 Hours:0 Days:0 ]
    A18: WiFi [ Set:On ] If [ %WLANTMP ~ 1 ]
    A19: Mobile Data [ Set:On ] If [ %MOBILDATA ~ 1 ]
    A20: Wait [ MS:0 Seconds:0 Minutes:1 Hours:0 Days:0 ]
  A21: Goto [ Type:Action Label Number:1 Label:Loop ]
Click here to see the periodicinterneton.tsk.xml you can import directly into Tasker.
<TaskerData sr="" dvi="1" tv="1.3.3u2m">
    <Task sr="task175">
        <cdate>1366139544095</cdate>
        <edate>1368747046322</edate>
        <id>175</id>
        <nme>Periodic internet On</nme>
        <pri>10</pri>
        <rty>1</rty>
        <Action sr="act0" ve="3">
            <code>547</code>
            <Str sr="arg0" ve="3">%PERIRADIO</Str>
            <Str sr="arg1" ve="3">1</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act1" ve="3">
            <code>721</code>
            <Str sr="arg0" ve="3">Periodic internet.w / StateON</Str>
            <Int sr="arg1" val="1"/>
        </Action>
        <Action sr="act10" ve="3">
            <code>547</code>
            <Str sr="arg0" ve="3">%noradio</Str>
            <Str sr="arg1" ve="3">%MOBILDATA+%WLANTMP</Str>
            <Int sr="arg2" val="1"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act11" ve="3">
            <code>37</code>
            <lhs>%noradio</lhs>
            <op>1</op>
            <rhs>0</rhs>
        </Action>
        <Action sr="act12" ve="3">
            <code>137</code>
            <Int sr="arg0" val="0"/>
            <Str sr="arg1" ve="3"/>
        </Action>
        <Action sr="act13" ve="3">
            <code>38</code>
        </Action>
        <Action sr="act14" ve="3">
            <code>433</code>
            <Int sr="arg0" val="0"/>
        </Action>
        <Action sr="act15" ve="3">
            <code>425</code>
            <Int sr="arg0" val="0"/>
        </Action>
        <Action sr="act16" ve="3">
            <code>30</code>
            <Int sr="arg0" val="0"/>
            <Int sr="arg1" val="0"/>
            <Int sr="arg2" val="14"/>
            <Int sr="arg3" val="0"/>
            <Int sr="arg4" val="0"/>
        </Action>
        <Action sr="act17" ve="3">
            <code>425</code>
            <lhs>%WLANTMP</lhs>
            <op>1</op>
            <rhs>1</rhs>
            <Int sr="arg0" val="1"/>
        </Action>
        <Action sr="act18" ve="3">
            <code>433</code>
            <lhs>%MOBILDATA</lhs>
            <op>1</op>
            <rhs>1</rhs>
            <Int sr="arg0" val="1"/>
        </Action>
        <Action sr="act19" ve="3">
            <code>30</code>
            <Int sr="arg0" val="0"/>
            <Int sr="arg1" val="0"/>
            <Int sr="arg2" val="1"/>
            <Int sr="arg3" val="0"/>
            <Int sr="arg4" val="0"/>
        </Action>
        <Action sr="act2" ve="3">
            <code>547</code>
            <Str sr="arg0" ve="3">%num</Str>
            <Str sr="arg1" ve="3">0</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act20" ve="3">
            <code>135</code>
            <Int sr="arg0" val="1"/>
            <Int sr="arg1" val="1"/>
            <Str sr="arg2" ve="3">Loop</Str>
        </Action>
        <Action sr="act3" ve="3">
            <code>300</code>
            <label>Loop</label>
        </Action>
        <Action sr="act4" ve="3">
            <code>888</code>
            <Str sr="arg0" ve="3">%num</Str>
            <Int sr="arg1" val="1"/>
            <Int sr="arg2" val="0"/>
        </Action>
        <Action sr="act5" ve="3">
            <code>523</code>
            <Str sr="arg0" ve="3">Periodic internet</Str>
            <Str sr="arg1" ve="3"/>
            <Img sr="arg2" ve="2">
                <nme>quick_restart</nme>
                <pkg>net.dinglisch.android.ipack.crystalhd</pkg>
            </Img>
            <Int sr="arg3">
                <var>%num</var>
            </Int>
            <Int sr="arg4" val="1"/>
            <Int sr="arg5" val="3"/>
        </Action>
        <Action sr="act6" ve="3">
            <code>547</code>
            <lhs>%AIR</lhs>
            <op>1</op>
            <rhs>off</rhs>
            <Str sr="arg0" ve="3">%MOBILDATA</Str>
            <Str sr="arg1" ve="3">1</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act7" ve="3">
            <code>547</code>
            <lhs>%AIR</lhs>
            <op>2</op>
            <rhs>off</rhs>
            <Str sr="arg0" ve="3">%MOBILDATA</Str>
            <Str sr="arg1" ve="3">0</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act8" ve="3">
            <code>547</code>
            <lhs>%WIFII</lhs>
            <op>11</op>
            <rhs>CONNECTION</rhs>
            <Str sr="arg0" ve="3">%WLANTMP</Str>
            <Str sr="arg1" ve="3">1</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
        <Action sr="act9" ve="3">
            <code>547</code>
            <lhs>%WIFII</lhs>
            <op>12</op>
            <rhs>CONNECTION</rhs>
            <Str sr="arg0" ve="3">%WLANTMP</Str>
            <Str sr="arg1" ve="3">0</Str>
            <Int sr="arg2" val="0"/>
            <Int sr="arg3" val="0"/>
        </Action>
    </Task>
</TaskerData>


The "periodic internet Off" task

This task must be executed to turn the periodic internet mode off. Skip the line A6, code it after adding the "Periodic internet widget" on your home screen:
Periodic internet Off
  A1: Stop [ With Error:Off Task:Periodic internet On ]
  A2: Mobile Data [ Set:On ] If [ %MOBILDATA ~ 1 ]
  A3: WiFi [ Set:On ] If [ %WLANTMP ~ 1 ]
  A4: Notify Cancel [ Title:Periodic internet Warn Not Exist:Off ]
  A5: Variable Set [ Name:%PERIRADIO To:0 Do Maths:Off Append:Off ]
  A6: Zoom Element Visibility [ Element:Periodic internet.w / StateON Set:Off ]
Click here to see the periodicinternetoff.tsk.xml you can import directly into Tasker.
<TaskerData sr="" dvi="1" tv="1.3.3u2m">
 <Task sr="task176">
  <cdate>1366195609780</cdate>
  <edate>1368748409608</edate>
  <id>176</id>
  <nme>Periodic internet Off</nme>
  <pri>10</pri>
  <Action sr="act0" ve="3">
   <code>137</code>
   <Int sr="arg0" val="0"/>
   <Str sr="arg1" ve="3">Periodic internet On</Str>
  </Action>
  <Action sr="act1" ve="3">
   <code>433</code>
   <lhs>%MOBILDATA</lhs>
   <op>1</op>
   <rhs>1</rhs>
   <Int sr="arg0" val="1"/>
  </Action>
  <Action sr="act2" ve="3">
   <code>425</code>
   <lhs>%WLANTMP</lhs>
   <op>1</op>
   <rhs>1</rhs>
   <Int sr="arg0" val="1"/>
  </Action>
  <Action sr="act3" ve="3">
   <code>779</code>
   <Str sr="arg0" ve="3">Periodic internet</Str>
   <Int sr="arg1" val="0"/>
  </Action>
  <Action sr="act4" ve="3">
   <code>547</code>
   <Str sr="arg0" ve="3">%PERIRADIO</Str>
   <Str sr="arg1" ve="3">0</Str>
   <Int sr="arg2" val="0"/>
   <Int sr="arg3" val="0"/>
  </Action>
  <Action sr="act5" ve="3">
   <code>721</code>
   <Str sr="arg0" ve="3">Periodic internet.w / StateON</Str>
   <Int sr="arg1" val="0"/>
  </Action>
 </Task>
</TaskerData>


Turn off "Periodic internet" by clicking on the notification

Create a new Tasker profile, name it "Periodic internet Off". Select event, UI, notification click. Owner Application is "Tasker" and the title should be "Periodic internet". As the performed action select the just created "Periodic internet Off" task.

The "periodic internet On/Off" widget

Now we need the above mentioned program "Zoom", this will allow us to place a interactive widget on our home screen so we can turn our new battery saving mode on and off by tapping on it. Import the widget template by copypasting the following code into a "Periodic_internet.ztl.xml" file or follow the instructions bellow to create a widget yourself.
<class name="Template" index="">
 <backColour>#00000000</backColour>
 <borderColour>#FFFFFFFF</borderColour>
 <borderWidth>0</borderWidth>
 <cellData>160,200,20,18;212,148,0,0</cellData>
 <cellsHigh>1</cellsHigh>
 <cellsWide>1</cellsWide>
 <marginWidth>4</marginWidth>
 <name>Periodic internet</name>
 <class name="Element" index="elements0">
  <elementType>Image</elementType>
  <heightLand>191</heightLand>
  <heightPort>191</heightPort>
  <name>StateOFF</name>
  <visible>true</visible>
  <widthLand>151</widthLand>
  <widthPort>151</widthPort>
  <xLand>0</xLand>
  <xPort>0</xPort>
  <yLand>0</yLand>
  <yPort>0</yPort>
  <class name="ImageElement" index="state0">
   <alpha>75</alpha>
   <stateName></stateName>
   <uri>ipack://net.dinglisch.android.ipack.crystalhd/quick_restart</uri>
   <class name="TaskAction" index="onClick0">
    <name>Periodic internet On</name>
   </class>
  </class>
 </class>
 <class name="Element" index="elements1">
  <elementType>Image</elementType>
  <heightLand>191</heightLand>
  <heightPort>191</heightPort>
  <name>StateON</name>
  <visible>false</visible>
  <widthLand>151</widthLand>
  <widthPort>151</widthPort>
  <xLand>0</xLand>
  <xPort>0</xPort>
  <yLand>0</yLand>
  <yPort>0</yPort>
  <class name="ImageElement" index="state0">
   <alpha>255</alpha>
   <stateName></stateName>
   <uri>ipack://net.dinglisch.android.ipack.crystalhd/quick_restart</uri>
   <class name="TaskAction" index="onClick0">
    <name>Periodic internet Off</name>
   </class>
  </class>
 </class>
</class>
Start Zoom and tap on the green plus in the lower right corner of the templates register. Configure it like this and tap on the green check button:
  • name: Periodic internet
  • cell width x height: 1 x 1
  • background colour: #00000000
  • margin width: 0
  • border width: 0
  • border colour: #ffffffff
  • visible: true
Now we will need an image element, therefore long press on the blank black area and select image. Fill in the fields like this:
  • name: StateOFF
  • source: select a nice looking icon. I like the "quick_restart" icon from the Crystal Project HD pack, you can see it on the screenshot at the top of this post.
  • alpha: 75 (that's important)
  • Click action: select the Tasker task "Periodic internet On"
Accept with the green check button. Long press on the newly created image and select Fit to widget. Now we will create a second image on top of this one. Press the menu button of your phone and select Add element. Select image again but configure it this time like shown here:
  • name: StateON
  • source: select the same nice looking icon
  • alpha: 255
  • Click action: select the Tasker task "Periodic internet Off"
After accepting, expand the image again over the whole widget area. You should end up with one image on top of the other, covering it completely. That's perfect, accept again with the green check button. You should see the template window again with a Periodic internet template. Exit Zoom by clicking on the button in left bottom corner (green check).

Now we need to place the widget on our home screen, just add a Zoom 1x1 widget and select the "Periodic internet" template when asked, confirm every dialog. Make sure the Zoom Element Visibility lines in our Tasker tasks (lines A2 and A6 respectively) point to the created "Periodic internet" Zoom widget on your home screen. Check it!

That's it! Now it's your turn - automate the activation and deactivation of the "periodic internet" task, I'm excited to hear about your experience with it! The only limit is yourself!

19 comments:

  1. Maybe one can improve it by setting the AutoSync On and Off, maybe this would force a data sync when internet is available.

    I recommend to run the "Periodic internet Off" task automatically after plugging the phone in into a power supply.

    ReplyDelete
  2. Hi Benny, I am very interested in using your Periodic Internet but am quite new to tasker, so I'm sorry for asking what may be obvious. For the Periodic Internet On task, how do I create the following elements after A3:

    Loop
    Anchor

    TIA!

    ReplyDelete
    Replies
    1. Hi Tia,

      to create an anchor named "loop" (thats what I did there) you have to add the following action in the task:

      go to "Task", then select "Anchor", and label it "Loop".

      at line A21 I selected the action under "Task" -> Goto -> Action Label and selected the Loop anchor. Thats it.

      Now we created an infinite loop with Tasker :)
      The lines

      A12: If [ %noradio ~ 0 ]
      A13: Stop [ With Error:Off Task: ]

      have a break condition for the whole task and therefore exit the loop.

      Everything clear :) ?

      Delete
  3. Hi Benny,

    Thanks a lot for the instructions to create such an useful task. I have a question about if you included instructions for the following (which you mention in the beginning)
    - The notification will show the time when the phone was connected to the internet the last time and how often the internet has been turned off and on already

    As far as I could tell, I do not see this being achieved with the steps you have provided. As cool as Periodic internet task already is, it will still give additional kicks to get some additional stats in the notification.
    Since I am new to tasker, please pardon me if I am completely wrong in my conclusion.

    Thanks once again.
    AP

    ReplyDelete
    Replies
    1. There is a time in the right upper corner of the notification. This time shows you, when the notification was created/modified. Therefore it mirrors the time when the task ran the last time to connect you to the internet :) Of course it is not a 100% guarantee that you were connected, but if you want, you can add a small check (a ping or a HTTPGET) before refreshing the notification. By this, you archive exactly what you wanted! Shall I help you with this?
      Please excuse my late answer :'(

      Delete
  4. Heya,

    I'm new to tasker and wanted to import your code straight to tasker. But I have no idea how, would you be able to walk me through the process.

    thanks
    Kontraption

    ReplyDelete
    Replies
    1. Maybe this helps you?
      http://www.pocketables.com/2013/05/how-to-import-tasksprofilesscenesprojects-into-tasker.html

      Delete
  5. Hi Benny,

    I got the task/profile working!!(now it's test period =D), but my widget only setting off and it's not switching between StateOn/StateOff images.

    srry, im not a programmer =/

    Thanks,
    Alex

    ReplyDelete
    Replies
    1. :O Do you have the lines
      A6: Zoom Element Visibility [ Element:Periodic internet.w / StateON Set:Off ]

      and

      A2: Zoom Element Visibility [ Element:Periodic Radio.w / StateON Set:On ]

      in your tasks "periodic internet Off" and "periodic internet On" ?

      Delete
  6. Whenever I click on the link to download directly into tasker, nothing happens... I am on a Galaxy s4 thats not rooted. Thanks for all help!

    ReplyDelete
    Replies
    1. One cannot download directly into Tasker :( no feature for that.
      Please create an empty file, name it as suggested (*.xml), copypaste and save the expanding code you will see after clicking the "import directly into tasker" button and follow these instructions:
      http://www.pocketables.com/2013/05/how-to-import-tasksprofilesscenesprojects-into-tasker.html

      Delete
  7. awesome thanks..did a little editing to suit my needs, such that the profile is active only between certain hours at night :) also paired it with another context where mobile data is off when wifi is connected.
    thanks for the detailed explanation.

    ReplyDelete
    Replies
    1. The same for me!! But I have another if clause, it is only activated at night, if I'm not charging :)
      Another idea would be that it is only activated at night if you are not home... that means not in reach of a charger...

      Delete
  8. Hi Benny,
    Thanks for the very detailed post, it's much appreciated! Not sure how often you check this because I'm a new reader to the blog but I'm very interested in this task but am very new to tasker and I'm having some trouble with creating it. When I go to create A4 the anchor task nothing comes up when I go into the anchor setting, it won't let me create a label, have you any ideas why this might be? Also have you any ideas how you might create a profile so that this is automatically turned off if you have turned on wifi or data yourself?
    Thanks for the time, hoping to hear from you.
    Donal

    ReplyDelete
    Replies
    1. I cannot help you with the anchor problem :( You create it by going to the Action Task -> Anchor and choose a good label name of your choice, sorry to hear that it makes problem for you. Maybe you have a very old version of Tasker? There was a bug in Tasker where this action did nothing, check this out:
      http://tasker.dinglisch.net/changes/changes1.3.2.html
      under Additions the last point:
      "action Anchor, does nothing"

      The second point: Make a Tasker task which turns on wifi and/or mobile data and turns off the periodic internet profile. Create a shortcut to this task on your home screen. This is a workaround, but I think it is a matter of getting used to it, to turn on data and/or wifi this way ;)

      Delete
  9. Idk what I did but when I click the widget nothing happens..

    ReplyDelete
  10. Great post sharing information related to energy saver

    ReplyDelete
  11. Really good post! Hope there will be more good post here!Thanks for sharing valuable information..How Solar Panels work

    ReplyDelete