One of the popular questions on r/Tasker forum is how to prevent mobile phone from charging when it reaches 100%. Even though modern phones are smart enough to minimise the damage caused by overnight charging, some people would sleep better knowing that their phone isn’t constantly hooked up, so let’s fix that. The project aims at Sonoff Micro (review), but you can use it with other smart switches and sockets.

I’m going to use a wireless stand for 2 reasons.

Reason 1: I use a wireless stand. There are many wireless stands, but this one is mine – Choetech Wireless Charger (review).

Reason 2: I can show you how to use Any Sensor to “recognise” a charger, or a position the phone is in, to be specific.

Gyro – position in 3D space

The new Tasker trigger has the ability to read raw sensor data. A stream of information usually reserved for the Android OS. One of the sensor – gyroscope, defines the phone orientation in 3D space. I can use this data to “save” the position of the phone when on the wireless stand.

Place the phone on the stand and wait a couple of seconds. Record the gyroscope values. I set a very low tolerance 1 . This profile should trigger only when my phone is resting on the stand and not being held in my hand in a similar orientation.

Now, the phone knows when it’s connected to this particular charger. And guess what else is connected to this charger? Sonoff Micro! I defined the charger, I will use eWeLink API to control my Sonoff Micro.

Buy Sonoff Micro Buy it using these links to support NotEnoughTech. Amazon UK ITEAD store Banggood

NodeRED

If you don’t know what NodeRED is and why you should care, take a look at this NodeRED series for beginners. Thanks to eWeLink API, I don’t even have to flash a custom firmware to control the Sonoff Micro. Simply install node-red-contrib-ewelink node in the Palette Manager.

I’m going to use HTTP requests to control when to turn the charger on and off. The flow is simple – receive the payload from my mobile phone, change the values to match eWeLink interface and submit the on|off to my Sonoff Micro when needed.

I could resolve this with a single function node, but I want to keep the flow as visual as possible.

Tasker Logic

There is more than one way we can skin this cat. You can modify my scenario to your liking but it works like this by default:

Sonoff Micro power on logic keeps it OFF

Mobile phone turns the charging ON when the battery is lower than 90%

Mobile phone turns the charger OFF when the battery is at 100%

Charger doesn’t turn off when the phone is removed (can be done)

With these rules set, my phone will turn on the charger (if needed) and charge the battery till 100%. Once charged the power is cut off, unless the battery power drops down under 90%.

Standard overnight charging

Smarter overnight charging

To send the ON|OFF command I use HTTP Post action. The action is wrapped in IF condition, and sent after 5 seconds, as I want to make sure the phone is placed on the charger and it is not moving. An associated profile responsible for charger detection sets a variable when the phone is resting on the wireless stand.

If the phone is in that position for 5 seconds, then the HTTP Post is sent to start the charger (battery 0-90% ) or stop the charging process (battery 100% ).

In my overnight test, this 100%>90%>100% happened just once. It will depend on the discharge speed of your mobile and the threshold set.

TASKER PROFILE: Fully Charged Profile: Fully Charged (Off) Restore: no State: Battery Level [ From:100 To:100 ] State: Variable Value [ %ChargingStand ~ on ] Enter: SM Battery Charged A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] A2: If [ %ChargingStand ~ on ] A3: Variable Set [ Name:%noderedserver To:http://credentials@192.168.1.107:1990/charger Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] A4: HTTP Request [ Method:POST URL:%noderedserver Headers: Query Parameters: Body:{"battery":"charged"} File To Send: File/Directory To Save With Output: Timeout (Seconds):30 Trust Any Certificate:On Automatically Follow Redirects:Off Use Cookies:Off ]

TASKER PROFILE: Top up Profile: Topup (On) Restore: no State: Battery Level [ From:0 To:90 ] State: Variable Value [ %ChargingStand ~ on ] Enter: SM Top up A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] A2: If [ %ChargingStand ~ on ] A3: Variable Set [ Name:%noderedserver To:http://credentials@192.168.1.107:1990/charger Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] A4: HTTP Request [ Method:POST URL:%noderedserver Headers: Query Parameters: Body:{"battery":"uncharged"} File To Send: File/Directory To Save With Output: Timeout (Seconds):30 Trust Any Certificate:On Automatically Follow Redirects:Off Use Cookies:Off

TASKER PROFILE: Top up Profile: Gravity Sensor Restore: no State: Any Sensor [ Output Variables:* Type:gravity Non-wakeup Interval (ms):1000 Interval Type (Check Help):Throttle ] [ %as_values1 < 1.0794 & %as_values1 > -0.98785 & %as_values2 > 7.30474 & %as_values2 < 9.34599 & %as_values3 < 6.21491 & %as_values3 > 4.14925 ] Enter: On Wireless Charger+ A1: Variable Set [ Name:%ChargingStand To:on Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] Exit: On Wireless Charger- A1: Variable Set [ Name:%ChargingStand To:off Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] ]

Is it good for battery?

The battery tech improved a lot over the years and your phone should be fine being plugged overnight as is. The gain of using this profile is not going to be amazing, but it can reduce the number of charging cycles. If this profile helps you sleep better at night, that will be its biggest contribution to your wellbeing.

I have done this “because I could” and to illustrate how you can interact with Tasker, NodeRED and other systems to create something interesting.

Conclusion

Playing with Tasker is always fun. Now you should know how to use sensors to define the phone’s location and how to take advantage of it. Feel free to modify, add more features and share this profile with others, as it’s fun to work on projects when locked indoors! Let me know if you have any questions in this Reddit thread.