# HomeyScript for Wallbox Pulsar plus charger lock/unlock pause/resume

**URL:** <https://community.homey.app/t/homeyscript-for-wallbox-pulsar-plus-charger-lock-unlock-pause-resume/54616>\
**Category:** Ideas & Suggestions\
**Tags:** homeyscript\
**Created:** [November 10, 2021, 6:58pm UTC](https://community.homey.app/t/homeyscript-for-wallbox-pulsar-plus-charger-lock-unlock-pause-resume/54616 "2021-11-10T18:58:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Sen-Sai](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sen-sai/32/13164_2.png) [@Sen-Sai](https://community.homey.app/u/Sen-Sai)\
**Post date:** [November 11, 2021, 8:50pm UTC](https://community.homey.app/t/homeyscript-for-wallbox-pulsar-plus-charger-lock-unlock-pause-resume/54616/2 "2021-11-11T20:50:39Z")

</div>

So, through facebook I had a couple of requests to share what I’ve done.  
To run this script you need your login information to your Wallbox account ( [https://my.wallbox.com](https://my.wallbox.com) ) and the charger ID you want to lock or unlock.  
Getting the ID is simple. Just login to [my.wallbox.com](http://my.wallbox.com) and click on your charger. When you are on the page with all the details of your charger you can read the ID from the URL in your browser. It’s the number after the last slash.  
 ![wallbox_charger_nr](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/c/e/cedfecb956238863d637c7d0b0b14236cb9494d0.jpeg)

Go to homeyscript and create a new script. Call it e.g. wallbox ( I did ).  
Copy and paste the code below in your new script and enter the details in the top three lines of the code.  
Save it.

In my case I then created a virtual device called “laadpaal”.  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/c/0cd27c02992e3e49c25bfe9790a913c718c9490a.png)

And two flows for when turned on or off ( unlock or lock ).  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/e/beaf0b5de7e10e4b0e4d905eb239287cecf49848.png)

To unlock your charger, run the wallbox script with argument **unlock**.  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/0/90274d31e21f9f106e244c7ed9a5f4916526f204.png)

To lock your charger, run the wallbox script with argument **lock**.  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/d/9d0500f56d5d88c8b58e19fe3f4c7ec5032e9790.png)

If you have Google connected to your Homey, you can simply say **zet laadpaal aan** or **set laadpaal uit** to unlock or lock the charger.

Code :

```auto
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/';

var token = '';
var unlocked = { 'locked': 0 };
var locked = { 'locked': 1 };

/**
 * 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;
}

/**
 * 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.code === 200 ) {
    return false;
  }
  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.code === 200 ) {
    return false;
  }
  return true;
}

// Make sure this script works with 1 single argument
if ( typeof args[0] !== 'string' ) {
  throw new Error( 'Script must be run from a flow with a string variable' );
}

switch ( args[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' );
    }
    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' );
    }
    break;
}

```

---

_[View the full topic](https://community.homey.app/t/homeyscript-for-wallbox-pulsar-plus-charger-lock-unlock-pause-resume/54616)._
