How I automated my laptop’s battery charging

atul khatri
5 min readJul 13, 2021

--

And maintained the battery health

Over the last year, home-offices have become very common and so did the frustration of frequently turning the power on/off for charging our laptops.

To overcome this, I tried to automate this task with the help of an IOT device and some software magic.

For now, this method will only work for Macbooks since I only develop apps for macOS :). I hope someone can help to port the source code for Windows.

Let’s go through the details on how I did it.

Requirements:

  • Tuya Compatible IOT Smart Plug
  • IFTTT Account
  • macOS app

Hardware:

The only thing we need is any Tuya compatible Smart Plug. For people who don’t know what Tuya is, Tuya provides the base chipset to IOT companies to build smart devices like smart bulbs, plugs, camera, sensors etc. It also provides a white label app to all vendors, although the branding is different for some of the brands, yet they all look and function the same. Moreover, all Tuya compatible devices are mostly compatible with other apps as well.

Some of the brands that should work are given below:

  • Gosund
  • Yutron
  • ZooZee
  • Le
  • Moko
  • Brilliant*

I went with 4 packs of Gosund smart plugs from Amazon.

Steps:

Smart Plug app-

  • Regardless of the Smart Plug brand you chose, download the ‘Brilliant Smart’ app from the App Store.
  • Create an account in this app and link your Smart Plug.
  • It is very important to use 2.4Ghz frequency of your WiFi for these plugs as these are not compatible with 5Ghz networks. In case it cannot connect to your network, go to your router settings and allocate a different SSID for 2.4Ghz frequency.

IFTTT Setup-

  • Create an IFTTT account if you haven’t already and sign in
  • Go to ‘My Applets’
  • Select ‘Create Applet’
  • Click ‘Add’ in If This, search for “webhooks” and select ‘Webhooks’
  • Click ‘Receive a web request’
  • Enter event name “battery_low” and click ‘Create Trigger’
  • Click ‘Add’ in Then That, search for “brilliant” and select ‘Brilliant Smart’
  • Select ‘Turn on’ action
  • In the drop down, select your plug and click ‘Create action’
  • Click ‘Continue’ and then click ‘Finished’ to finish the setup
  • Click ‘Webhook’ icon again
  • Click ‘Documentation’
  • Note down the key as shown in the next screen

Note: Key remains same for the webhook

  • Next, repeat the same steps again with the only exception being, this time use event name “battery_full” instead of “battery_low” and select Brilliant service action “Turn off” instead of “Turn on”.

It should show up something like this:

That’s all for IFTTT setup

Battery Check app setup-

Just download the executable file from this link:

https://github.com/atulkhatri/battery-check/releases/download/1.0.0/BatteryCheck.app.zip

  • Unzip it and drag it to ‘Applications’
  • Run it

It should show the following warning, click ‘Open’

  • Now, go to ‘Security & Privacy’ in System Preferences and click ‘Open Anyway’
  • Finally, go to Applications and run the app again. Click ‘OK’ this time

You will see that the app is now running on top.

  • Click the green app icon and fill the service key in the first column
  • Click ‘Save’ and you’re done.

Now, whenever the battery reaches 12%, it will automatically turn on charging and when battery percentage reaches 100%, charging will turn off. Although these numbers are configurable, it’s better to keep it unchanged.

In order to run this app each time your Macbook restarts, you can add it into ‘Login Items’ by following these steps:

  • Select ‘Login Items’ tab and click on the ‘+’ icon below
  • Select ‘BatteryCheck’ app and click ‘Add’

Now this app will automatically run whenever the Macbook restarts.

That’s it. Thank you for reading this. I hope this little article helped you save your Macbook’s battery life and potentially saved some trouble to turn the charging on/off frequently.

Source code is hosted on my Github repository:

Please comment on this blog for any feedback.

--

--

atul khatri

Software Engineer (iOS) @ Meta | Tech Enthusiast | Learner