1

Device: LG G4
Android version: 6.0
Status: rooted

What can I do to power off the device (with a 20 sec delay) when I remove the charger. I tried with AutomateIt app, but after first cycle of power off / power on, it doesn't work if I don't open the app again, to run in the background.

I want to use my old phone in a old car, as a GPS device. I make it power on automatically as is described here: https://forum.xda-developers.com/att-lg-g3/general/lg-d3-d850-autoboot-t3714070

0

Using Macrodroid:

  1. On the Triggers tab, under Battery/Power section select "Power Connected/Disconnected", then choose "Power Disconnected".
  2. On the Actions tab, under Macrodroid Specific, select "Wait Before Next Action", then set the delay period to 20 seconds.
  3. On the Actions tab, under Device Actions, select Reboot/PowerOff, then choose "Power Off". If "Power Off" does not work on your device, choose "Power Off (Alternative)".
  4. Save the macro and give it a name.

When completed, the macro will look like the image below. Use the 3-dot to test the macro.

Note: Powering off requires the device be rooted.

Power off when charger is removed macro

Update: The way the macro is programmed above does not allow to stop the device from being power off if the charger is connected again before the 20 seconds delay expires. To stop the trigger, a condition to check the state of the device after the delay expires but before the action to power it off.

Edit the macro. Add a new action. Under the Actions, on the Conditions/Loops section, select "If clause". Add a condition. Select "Power Connected" under the Battery/Power section and choose "Power Disconnected", then OK. Click the up and down arrows button on the Actions box, move the "Power Off" action inside the "If...End If". Save the macro. The macro will look like the image below.

Power off when charger is removed macro version 2

  • @Firelord I did not mentioned it before because the OP stated that his/her device is rooted. Now mentioned as a note in the answer. – Reddy Lutonadio Jun 26 at 15:49
  • That is great. It works fine. Thanks. But if I connect the charger within those 20 sec, why the trigger isn't canceled? – Ionut Buftea Jun 26 at 16:04
  • @IonutBuftea It's because the trigger (power disconnected) has started, and no check is in place to prevent the device to power off if power is connected again. I'll soon modify the macro to take into consideration when power is connected during the 20 seconds delay. – Reddy Lutonadio Jun 26 at 16:55
  • @IonutBuftea See edit. – Reddy Lutonadio Jun 26 at 17:31
0

Run this shell script from init.d or /data/adb/*.d/ in case of Magisk:

#!/system/bin/sh

is_charging()
{
    [ $(cat /sys/class/power_supply/battery/status) = Charging ] || return 1
}

# keep waiting
while is_charging; do sleep 1; done

# charger is disconnected, countdown
sleep 20

# again start waiting if charger is connected
is_charging && exec $(realpath $0)

# else, shutdown
/system/bin/reboot -p charger_disconnected ||
/system/bin/setprop sys.powerctl shutdown,charger_disconnected
0

Try Automate (free):

  1. Power source plugged block
  2. Delay block; 20s
  3. Device shutdown block (rooted device required)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.