Automate Smart Plug to charge laptop using Webhook and Shell Script.
In the world of automation, it little gives me heebie-jeebies to turn on and off charging when needed cause I read that always plugged in charging will damage your laptop battery but since I'm using a laptop it always draws power so keeping on charging all the time is good or bad that is debate we don't want to get in, so here we go..
Table of Contents
ASUS Laptop where they have inbuilt a hardware charging limit to 80% so I thought that it must be important that's why ASUS has done it. But now, the office gave me another working laptop which is DELL, and sadly given the model doesn't have a hardware charging limit available and while it's plugged and charging at full, sometimes laptop gets hot and its bad if you live in tropical area like myself. 😩
Anyhow, You can see limiting the charging can increase your laptop's battery life a little better.
So here I have a problem so I thought, What if I can build a simple plug as a charger solution that can be turned automatically ON when charging gets low or reaches a set limit and it will get off when laptop charging reaches above 90%. and vice versa. So it a simple and fun to build this want to share this little project. 😎
Get a SMART Switch
The first step is to get a wireless plug so I found the cheapest smart plug available on Amazon. which advertised as works with Alexa so without any research I thought it must work with everything. Boy! I was wrong but eventually, it worked out just fine, just went to my super helpful everyone's favourite friend who helped me to fix this kind of issue as always and you all know by its name "google". 🤓
So this Solimo Smart Plug has this app called "Smart Life" to set up your device with your local centralized Wi-Fi router so you can talk to this "IOT" device from anywhere.
https://amzn.to/354UpQW
So I register myself on the app and proceed with usage turning ON and OFF. Also, This app has multi-functionality like I can set it up like a timer but sadly I have to set up the timer again and again like Microwave or create a scene where I can connect to other services like location, temperature, etc. which is all fine but it's useless for the purpose and what I want to achieve.
Create an account on Sequematic
If you are familiar with IFTTT it's simply "IF THIS THEN THAT" and my project is all about that so I wanted to find an option to turn the plug state from my laptop whenever charging is necessary. Then I found out that I can turn on the device through Webhook, where I change the plug state as I want and the webhook is available on IFTTT. Then I searched in IFTTT if there's an applet available for "Smart Life" and found some applets.
Great !! but then the PROBLEM occurred, where it showed my plug is unavailable in the applet settings so I search about it and found "Smart Life" broke ties with IFTTT so I search more than found out about another app called "Brilliant Smart" and really it's not that brilliant which turns out the same
Table of Contents
1. Get a SMART Switch
2. Create an account on Sequematic
3. Write or Download Bash Script for an automation.
4. Cronjob for scheduled task run.
Table of Contents
1. Get a SMART Switch
2. Create an account on Sequematic
3. Write or Download Bash Script for an automation.
4. Cronjob for scheduled task run.
failure as "Smart Life" then I went again for help on the internet and finally someone on Reddit said that there's another website which still can access "Smart Life" API and that site is "sequematic" which finally done all the work for me.
Sequematic is really easy to set up so I created a step by creating a webhook where I gave the state name as a "turn_on" and set parameter "on" and another step I have added a switch to turn on whenever a POST request on the web-hook is being sent.
In the above screenshot, you can see two sequences are created which are "socket-switch-on" and "socket-switch-off". In those sequences, two steps have been created.
Cool !! Now my plug can be turned on from the internet using just a POST request. It was so simple so what now? How can I turn it on whenever my laptop needs charging?
Write or Download Bash Script for an automation.
As always I wrote a very simple bash script [ Literally, It is very simple ], Check it out.
#!/bin/bash
# get battery percentage
battery_current_charge=$(upower -i $(upower -e | grep '/battery') | grep --color=never -E percentage|xargs|cut -d' ' -f2|sed s/%//)
# check if battery percentage with your set value, mine is 40
if [[ $battery_current_charge -le "40" ]]; then
# if you using Ubuntu then below notify command will show you notification notify-send "Auto Charging" "Script Executed!"
#This will trigger switch on if condition is satisfied
curl -X POST https://sequematic.com/trigger-ifttt-webhook/your-id//switch_on >/dev/null 2>&1
fi
if [[ $battery_current_charge -ge "90" ]]; then
# This condition will turn off the charging when limit hit to 90 curl -X POST https://sequematic.com/trigger-ifttt-webhook/your-id//switch_off >/dev/null 2>&1
fi
So if you have some Linux experience then you can understand from the shell script that I have created two if conditions and in the first condition, it compares the current battery percentage to start the plug when the battery reaches below or equal to 40%.
The second condition when the battery percentage reaches above or equal to 90% smart plug should automatically turn off.
In the shell script, the URL taken from sequematic can change the plug state when the condition is satisfied so you need to replace both of your IFTTT URLs from the sequematic link in the script.
Cronjob for scheduled task run.
This is a simple task given in a simple script but how can this script know every time this battery value changes?
As always, Linux has inbuilt tools like Cron, Where, I have given a timer to run this script for every 5 min to check battery percentage and the script will check its logic.
Check out the below cronjob.
Â
*/5 * * * * bash charge-battery-script.sh
Voila!! It worked like magic.
I always use Ubuntu as my daily driver OS so it was a piece of cake for me to automate this process and if you want to do this on your Windows then, I suggest you should turn on WSL Feature "Windows Subsystem for Linux" which of course enables Linux (Ubuntu) in your windows.
I can think of many usages from this automation somethings like turn on smart lights or change the color of smart light, smart socket or plug activity from your laptop.
Â
 Hope you found this article interesting and let us know your opinion if you have a way to improve it.Thank you for reading!