Wallbox : Get The Most Of It (with API)
Two month ago, I got myself a PHEV (Plug In Hybrid Electric Vehicle) and of course, thinking at the future, I also installed a class-2 charger at home. The obvious choice to me was the Wallbox Pulsar Plus charger. Small form factor, easy to set up, affordable and reliable. Wallbox software offering is great : centralized statistics on their website, mobile app, ... everything. But, is there any API available ? ;)
Of course there is!
Since the charger is connected to my network, I tried to directly connect to it. Only port 22 and 53 are opened. Yes, weird, port 53... In short, there is nothing you can do locally, you need to access the Wallbox portal to start playing with APIs.
I must admit that I was disappointed : no public documentation exists. Some people tried to integrate the home charger with some home automation platform like Jeedom, Homey or Home Assistant, but this is very light in terms of interactions and only provides elaborated code in either Python or JavaScript, I'm looking for the source APIs to create my own scripts.
What I was looking for was : getting charger details, current status, current configuration, lock/unlock remotely, get the statistics and play with it.... and anything else I could find on the way. Nothing available on the wallbox website, nothing available in the wallbox academy portal, .... very frustrating.
Then, I started to do some reverse engineering on the plugins mentioned above and I've found some endpoints. Furthermore, using Chrome browser, you have the ability to sniff the API sent from the Wallbox official portal. Very interesting ;)
Wallbox APIs works in two stages :
1) Get an authentication token;
2) Use the token to retrieve data for your charger(s)
The Token
The token is a 856 characters string which stays valid for 15 days. So, you need to manage that parameter and think at renewing it from time to time. Why not renewing it at each call you do ? I don't know what is the best practice at this point.
Here is the function to get the token :
function wbGetToken($auth)
{
$API="api.wall-box.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Authorization: Basic ".base64_encode($auth["username"].":".$auth["password"]),
'Accept: application/json, text/plain, */*',
'Content-Type: application/json;charset=utf-8'
));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$API."/auth/token/user");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result)->jwt;
}
$auth is an array containing your credentials for accessing the wallbox portal.
Here is a sample :
$auth=array(
"username" => "your_email_address",
"password" => "your_pass"
);
When successfully executed, the token is returned this way :
string(856) "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im[...]Qk1BqrVqaZZgVJ49wVuhI8GcrRGC_RwLw604FsMcYScMUZZmdVwBV2Vq_ioBhIFh9vMWgTi4DaZjtHuV--T82gTBJipJIZCnh7ABCDFP4WocIVzEZJM51UnQT4gjxk3nt8-iZY3kuXjbFOfMhMthjpj3fRWBQL8HvKwiMegGIY3c_N2tUVdLolVIPuZGyGQmWEv74wuZBBLrrv52KTzw65tKjjuYyejQnA_mXQNsnsbilE"
Of course, I have stripped the output for security reason.
The code
Now that we have a token, we have 15 days to issue any API calls we want :)
You need to know the charger ID of your charger to start interacting with it. This is easy to get, when on the Wallbox portal, you can see your charger ID in the URL :
Let's try to get something useful.
This function return all information about a specific charger :
$chargerInfo=wbGetCharger($token,$id);
var_dump($chargerInfo);
$ php wallbox.php
object(stdClass)#4 (30) {
["id"]=>
int(xxxxxx)
["uid"]=>
string(26) "XXXXXXXXXXXXXXXXXXXXXXXXXX"
["access"]=>
bool(true)
["name"]=>
string(20) "PulsarPlus SN XXXXXX"
["status"]=>
int(161)
["chargerType"]=>
string(10) "PulsarPlus"
["ocppReady"]=>
string(9) "ocpp_1.6j"
["ocppConnectionStatus"]=>
int(1)
["connectionType"]=>
string(4) "wifi"
["locked"]=>
int(0)
["maxChargingCurrent"]=>
int(32)
["addedEnergy"]=>
float(10.986)
["chargingPower"]=>
int(0)
["chargingTime"]=>
int(7437)
["energyUnit"]=>
string(3) "kWh"
["powerUnit"]=>
string(2) "kW"
["image"]=>
string(55) "https://api.wall-box.com/uploads/PLP1-0-2-4-0-002-A.png"
["lastConnection"]=>
int(1660242798)
["lastSync"]=>
int(1660242936)
["wifiSignal"]=>
int(75)
["chargerLoadType"]=>
string(7) "Private"
[...]
From the above example, you see how easy it is to get some information about the charger, the status, the configuration, ... And there is more.
I have created a quick code that display all the relevant information in one screen :
Happy with this result, I decided to create a background script recording the various status during a charging session : energy injected against power of charge. This is leading to an interesting graphic :
On this graph we see in orange the charging power in kW. My charger is capable (both the wallbox and the onboard charger in my car) of delivering 7.22 kW. Sometimes, the charging power is less, this is because my setup is equipped with a powerboost which is giving priority to the house. For example, when using the dishwasher or the frier, the energy demand for the house is higher, then the car is becoming secondary and the charger is lowering the load to prevent any circuit break down. This is a mandatory option in my opinion.
At the end of the charing session, we see the charging power is less, this is very common on EV when reaching the end of the charge.
The blue line is of course the energy delivered to the battery in kWh.
I'm still learning the API, and I'm also trying to get in touch with some Wallbox engineers but they are hard to get in touch with. I still have a lot of question of course ! ;)
All the information and scripts in this post can be found on my GitHub Repo.
- Token generator;
- Charger info page;
- Stat Collector;
- The entire framework and its documentation
More to come ...
I hope this helps !
Hi, thats awesome! I was wondering, are you able to control (i.e. turn on and off) the charging the EV charger using that token?
ReplyDeleteHello, if you mean stop the charge, yes you can pause it. Sorry, not sure I totally got your point.
DeleteHi Frederic, thanks for this, do you know if the API also supports restarting the unit (if there is an error). Thanks!
ReplyDeleteHi There, I never tried it, probably, there is a POST command allowing a unit restart. What do you mean by "if there is an error" ?
DeleteI'm looking at a way how to turn on charging based on current kwh pricing. In The Netherlands there are energy suppliers delivering power with an hourly rate. My idea is to build a script to monitor the pricing and based on set values start the charging of my car and stop it once it goes over the threshold. I think your research is of nice value in getting my idea to reality. Thanks!
ReplyDeleteVery interesting ! Please consider sharing your script when completed, I'm sure it will help others !
DeleteHello Anonymous (and Frederic).
DeleteI too am trying to work on a small app to charge my car based on the hourly-pricing (which is how I came across this article - thank you).
Although - I am not a coder /developer - so I'm not moving too fast. But I am using a low-code platform (Retool).
I have managed to find an API which has the NL (or any other EU country for that matter) hourly pricing.... and have managed to pull this info into my app.
I plan to then control my wallbox (with the help of this article) based on the least expensive hour(s).
Would be happy to share more and learn more from you - my email is above. Hope to hear. thanks again. Neil
Hi Neil, welcome to my blog. I would say that anything useful has to be shared. This is the whole purpose of this blog actually. So, if you found something useful, I cannot encourage you more than sharing it ! Either on your own blog (why not ?) or in the comments here. Thanks ! ;)
DeleteNot sure this helps you, but the charger supports OCPP protocol. There is Home Assistant implementation of it. It acts as local server and the charger connects to it, not only it reports all the data, but you can also control the charge.
ReplyDeleteThanks a lot ! Unfortunately I'm not using Home Assistant (yet). But I'm sure this will allow others to play with it ! ;)
DeleteBonjour Frédéric,
ReplyDeleteTrès intéressant votre framework PHP.
Question naïve : comment l'implémenter ? Faut-il un serveur Apache ? Local/distant ?
Quel est le point d'entrée ?
Merci pour votre réponse.
Bien à vous.
Hervé
Bonsoir, merci ! ;)
DeleteLa bonne nouvelle c'est qu'il ne faut absolument aucun serveur web. Je fais tourner tout ça en ligne de commande avec php-cli. Php n'a pas absolument besoin de tourner dans une page web/serveur web. On peut continuer sur twitter @flhoest car mon audience est principalement anglophone.
Thanks for this article. By the way, that’s not just a random auth token; it’s a JSON web token. It can be decoded (using a tool like jwt.io) to reveal your Gmail address and user ID. Not sure if you consider those to be sensitive info.
ReplyDeleteHi, thanks for your comment. If you have noticed, I have changed the article. Well spotted !
DeleteWallbox has OCPP for controlling your charging station:
ReplyDeletehttps://support.wallbox.com/en/knowledge-base/ocpp-activation-and-setup-guide/
Hello !! Thank you very much for sharing this, it is indeed a shame that the API is not documented. I tested your scripts, great work!!
ReplyDeleteDo you know if it is possible to retrieve the history saved on myWallbox? I don't see any integration, plugin or other that knows how to do this, probably no one has found the API command that needs to be passed to fetch previous sessions. Good luck and thank you again!
Hi Sigalou, thanks for the kind words ;) I have no idea about the history, I can dig into it !
DeleteHello, thank you for a great API. I'm trying to expand a function called wbSetEnergyCost. I want to be able to set the kWh price since I have an agreement that changes the price every hour. Do you know what the Wallbox API URL is to set that?
ReplyDeleteHello, I see what you are trying to do, but I’m not sure there is time setting on the price. I think when you set it, this is once for all and it is applied even to previous sessions. Need to confirm it… The issue is Wallbox is not communicating about their API stack and getting documentation is impossible. I tried many times and once received promise… nothing more. Sorry.
DeleteYou should have tried a lektrico 1p7k station. Its far more useful than an wallbox. Local api, load blancing with solar ready. The sensor for load balancing has wifi and local api it-self.
ReplyDeleteIf you say so ... Wallbox is a perfect match for my personal use case and wallet ...
Delete