Had a quick look as I didn’t have much time.
I’ve updated the script so when you unlock the Wallbox it will set three tags:
- This month total energy in kWh ( needs to /1000 )
- This months charging time in seconds
- This months total sessions
It will be set and updated globally whenever you unlock you wallbox.
If you need the values you can call them wherever you can use tags and look for HomeyScript tags. You will find them there.
const username = '<your username (usually email address)';
const password = '<your password>';
const MYCHARGERID = 000000; // ID of your charger
const BASEURL = "https://api.wall-box.com/";
const URL_AUTHENTICATION = 'auth/token/user';
const URL_UNLOCKCHARGER = 'v2/charger/';
const URL_PAUSECHARGER = 'v3/chargers/';
const URL_PAUSECHARGER_ACTION = '/remote-action';
var token = '';
var unlocked = { 'locked': 0 };
var locked = { 'locked': 1 };
var resume = { 'action': 1 };
var pause = { 'action': 2 };
/**
* Get JWT Token from wallbox using login and password
*/
async function connectToWallbox() {
let connect = await fetch(BASEURL + URL_AUTHENTICATION, {
headers: {
'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64'),
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
}
}).then(response => response.json());
return connect.jwt;
}
/**
* set wallbox maxCharging function with token from Connect function
*/
async function setMaxCharging(token, max) {
let maxCharging = { 'maxChargingCurrent': max };
let mC = await fetch(BASEURL + URL_UNLOCKCHARGER + MYCHARGERID, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(maxCharging)
}).then(response => response.json());
if (mC.error === true) {
return mC.message;
}
return true;
}
/**
* Unlock wallbox function with token from Connect function
*/
async function unlockWallbox(token) {
let unlock = await fetch(BASEURL + URL_UNLOCKCHARGER + MYCHARGERID, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(unlocked)
}).then(response => response.json());
if (unlock.error === true) {
return unlock.message;
}
await tag('WBTotalSessionsMonth', unlock.data.chargerData.resume.totalSessions );
await tag('WBTotalCharchingMonth', unlock.data.chargerData.resume.charchingTime );
await tag('WBTotalEnergyMonth', unlock.data.chargerData.resume.totalEnergy );
return true;
}
/**
* Lock wallbox function with token from Connect function
*/
async function lockWallbox(token) {
let lock = await fetch(BASEURL + URL_UNLOCKCHARGER + MYCHARGERID, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(locked)
}).then(response => response.json());
if (lock.error === true) {
return lock.message;
}
return true;
}
/**
* Resume wallbox function with token from Connect function
*/
async function resumeWallbox(token) {
let wbResume = await fetch(BASEURL + URL_PAUSECHARGER + MYCHARGERID + URL_PAUSECHARGER_ACTION, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(resume)
}).then(response => response.json());
if ( wbResume.error === true ) {
return wbResume.message;
}
return true;
}
/**
* Pause wallbox function with token from Connect function
*/
async function pauseWallbox(token) {
let wbPause = await fetch(BASEURL + URL_PAUSECHARGER + MYCHARGERID + URL_PAUSECHARGER_ACTION, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(pause)
}).then(response => response.json());
if ( wbPause.error === true ) {
return wbPause.message;
}
return true;
}
// Make sure this script works with 1 single argument
let actionArgs = args[0].split('=');
// Make sure this script works with 1 single argument
if (typeof actionArgs[0] !== 'string') {
throw new Error('Script must be run from a flow with a string variable');
}
switch (actionArgs[0]) {
case "unlock":
token = await connectToWallbox();
result = await unlockWallbox(token);
// Optional. You can leave the next 3 lines out and place them in a flow
if (result === true) {
say('Wallbox is ontgrendeld');
} else say( result );
break;
case "lock":
token = await connectToWallbox();
result = await lockWallbox(token);
// Optional. You can leave the next 3 lines out and place them in a flow
if (result === true) {
say('Wallbox is vergrendeld');
} else say( result );
break;
case "pause":
token = await connectToWallbox();
result = await pauseWallbox(token);
// Optional. You can leave the next 3 lines out and place them in a flow
if (result === true) {
say('Wallbox is gepauzeerd');
} else say( result );
break;
case "resume":
token = await connectToWallbox();
result = await resumeWallbox(token);
// Optional. You can leave the next 3 lines out and place them in a flow
if (result === true) {
say('Wallbox is weer an het laden');
} else say( result );
break;
case "setMaxCharge":
if (typeof actionArgs[1] === 'undefined' ) {
break;
}
token = await connectToWallbox();
result = await setMaxCharging(token, actionArgs[1]);
// Optional. You can leave the next 3 lines out and place them in a flow
if (result === true) {
say('Wallbox is ingesteld op maximaal laden van ' + actionArgs[1]);
} else say( result );
break;
}