TESLA POWERWALL2 API


• Accessing The Tesla Powerwall2 API
• Tesla Powerwall2 API Password
• Tesla Powerwall2 API JSON Data
• Get Powerwall2 API JSON Data In Browser
• Powerwall2 API JSON Data Via Logged In Browser
• Curl Grab Tesla Powerwall2 API JSON Data
• Nodejs Code For Powerwall2, Battery Percentage, Load Instant Power, Solar Instant Power
• Nodejs Example - Powerwall2 Code Output
• Powerwall2 Nodejs Code - Asynchronous Rather Than Nested Callbacks

ACCESSING THE TESLA POWERWALL2 API

Tesla-Energy-Gateway-nodejs-api.png

The Tesla Powerwall2 local server webpage can be found (in my case) on my LAN at https://192.168.1.137.
Refer to the LAN section of your router in order to find the correct address.

On the physical Tesla Gateway box there is a password printed on the device. Something like:

TESLA POWERWALL2 PASSWORD

Powerwall2 Password:  FTUAKSTNFP
Powerwall2 TSN:    CN333725U6A1AAv

Use the last 5 characters from the above password STNFP in order to log in to Tesla's powerwall2
Energy Gateway LAN server:

Tesla-Powerwall2-LAN-Login

This results in the following Tesla Energy Gateway webpage being displayed:

Tesla-Energy-Gateway

TESLA POWERWALL2 API JSON DATA

All of the above data shown can be accessed via the Tesla Powerwall2 API as I will illustrate below…
I based the below on the following excellent webpage: https://github.com/vloschiavo/powerwall2

GET POWERWALL2 API JSON DATA IN BROWSER

Now, via the web browser that you used to log into the Tesla system above,
you can easily grab the Tesla Powerwall2 JSON data relating to the percentage charge of the powerwall
battery/batteries:

https://192.168.1.137/api/system_status/soe

{"percentage":61.88742549166577}

Useful JSON data such as the following can now be easily accessed via the below URL in the browser:

• The load instant power (the load that the house is drawing in watts),
• The solar instant power (incoming solar power in watts),
• The site instant power(amount you are exporting to the electricity grid in watts).

https://192.168.1.137/api/meters/aggregates

{
  "site":{
   "last_communication_time":"2022-03-22T11:57:11.746638557+01:00",
   "instant_power":-5019,
   "instant_reactive_power":-675,
   "instant_apparent_power":675.1896030005202,
   "frequency":0,
   "energy_exported":361146.03577413695,
   "energy_imported":2093286.3464779663,
   "instant_average_voltage":426.13646018617084,
   "instant_average_current":0,
   "i_a_current":0,
   "i_b_current":0,
   "i_c_current":0,
   "last_phase_voltage_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_power_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_energy_communication_time":"0001-01-01T00:00:00Z",
   "timeout":1500000000,
   "num_meters_aggregated":1,
   "instant_total_current":0
  },
  "battery":{
   "last_communication_time":"2022-03-22T11:57:11.761835772+01:00",
   "instant_power":-5120,
   "instant_reactive_power":90,
   "instant_apparent_power":5120.790954530365,
   "frequency":50.097,
   "energy_exported":1580450,
   "energy_imported":1819760,
   "instant_average_voltage":242.3,
   "instant_average_current":0,
   "i_a_current":0,
   "i_b_current":0,
   "i_c_current":0,
   "last_phase_voltage_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_power_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_energy_communication_time":"0001-01-01T00:00:00Z",
   "timeout":1500000000,
   "num_meters_aggregated":2,
   "instant_total_current":0
  },
  "load":{
   "last_communication_time":"2022-03-22T11:57:11.746638557+01:00",
   instant_power":1597.5,
   "instant_reactive_power":-600.5,
   "instant_apparent_power":1706.6360186050217,
   "frequency":0,
   "energy_exported":0,
   "energy_imported":2901298.7466677437,
   "instant_average_voltage":426.13646018617084,
   "instant_average_current":3.7487991506337734,
   "i_a_current":0,
   "i_b_current":0,
   "i_c_current":0,
   "last_phase_voltage_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_power_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_energy_communication_time":"0001-01-01T00:00:00Z",
   "timeout":1500000000,
   "instant_total_current":3.7487991506337734
  },
  "solar":{
   "last_communication_time":"2022-03-22T11:57:11.80448197+01:00",
   "instant_power":6708,
   "instant_reactive_power":11,
   "instant_apparent_power":6708.00901907563,
   "frequency":0,
   "energy_exported":1417869.389144842,
   "energy_imported":9400.953180927841,
   "instant_average_voltage":426.14223368886275,
   "instant_average_current":0,
   "i_a_current":0,
   "i_b_current":0,
   "i_c_current":0,
   "last_phase_voltage_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_power_communication_time":"0001-01-01T00:00:00Z",
   "last_phase_energy_communication_time":"0001-01-01T00:00:00Z",
   "timeout":1500000000,
   "num_meters_aggregated":1,
   "instant_total_current":0
  }
}

TESLA POWERWALL2 API JSON DATA VIA LOGGED IN BROWSER


Tesla Powerwall2 API Gateway

Tesla Powerwall2 API meters aggregates

Tesla Powerwall2 API system_status soe

Tesla Powerwall2 API Energy Gateway aggregates

Tesla Energy Gateway aggregates Battery Charge Percentage

CURL - GRAB TESLA POWERWALL2 API JSON DATA

Using curl, you can log into the Tesla Powerwall2 API and then access the Tesla API:

Create a login script called "login_script.sh""
[Enter your password and save something to a cookie file]

curl -s -k -c cookies.txt -X POST https://192.168.1.137/api/login/Basic -H "Content-Type: application/json" -d "{\"username\": \"customer\",\"email\": \"YourEmailAddress@xyz.com\",\"password\": \"STNFP\"}"

Create a script called "test_script.sh"
[This script perform the API request shown above in the web browser]

curl -k -b cookies.txt https://192.168.1.137/api/meters/aggregates

Create a script called "BatteryChargePercentage_script.sh" to get the current Tesla Powerwall2 battery charge percentage
curl -s -k -b cookies.txt https://192.168.1.137/api/system_status/soe

NODEJS CODE FOR TESLA POWERWALL2 TO GET BATTERY PERCENTAGE,
LOAD INSTANT POWER, SOLAR INSTANT POWER



/*
 1. Test nodejs code to Log in to the Tesla Powerwall2 API
 2. Callback ... After logging into Tesla Powerwall2 API. API call to get Solar, HouseConsumption, And Exporting power.
 3. Callback ... After API call to get Solar, HouseConsumption, And Exporting power, get the Battery Charge Percentage
 4. Callback ... After getting the Battery Charge Percentage, process the JSON data
*/

var Obj;
var IncomingSolarPower;
var HouseConsumption;
var ExportingToGrid;
var SolarSurplus;
var BatteryCharge;

function Login_Then_Do_API_Call() //Log In to Tesla
{
  var exec = require('child_process').exec;
  exec("sh login_script.sh", function(error, stdout, stderr) {

  if (error) {
   console.log('stderr: ' + stderr);
   // node couldn't execute the command
   return;
  }
  else
  {
   API_SolarHouseConsumptionAndExporting()
  }
});
}

function API_SolarHouseConsumptionAndExporting() //Get loads of JSON data
{
  var exec = require('child_process').exec;
  exec("sh test_script.sh", function(error, stdout, stderr) {
  if (error) {
   console.log('stderr: ' + stderr);
   // node couldn't execute the command
  return;
  }
  else
  {
   Obj = JSON.parse(stdout);
   IncomingSolarPower=Obj["solar"]["instant_power"];
   HouseConsumption=Obj["load"]["instant_power"]
   ExportingToGrid=Obj["site"]["instant_power"];
   SolarSurplus=IncomingSolarPower-HouseConsumption
   API_BatteryChargePercentage();
  }
});
}

function API_BatteryChargePercentage()
{
  var exec = require('child_process').exec;
  exec("sh BatteryChargePercentage_script.sh", function(error, stdout, stderr) {
  if (error) {
   console.log('stderr: ' + stderr);
   // node couldn't execute the command
   return;
  }
  else
  {
   Extract_JSON(stdout)
  }
});
}

function Extract_JSON(rawdata)
{
 var tempobj = JSON.parse(rawdata);
 BatteryCharge=tempobj["percentage"];
 console.log();
 console.log("-----------------------------------------------------------------------------------------------");
 console.log('Incoming Solar (Solar Instant Power)........................... '+IncomingSolarPower+'W');
 console.log('House consumption (Load Instant Power).......................... '+HouseConsumption+'W');
 console.log('Exporting To Grid (site Instant Power).......................... '+ExportingToGrid+'W');
 console.log('Tesla Battery Charge Level...................................... '+BatteryCharge+'%')
if(SolarSurplus>3000)
{
  console.log('Solar Surplus (Incoming Solar - House Consumption)............. '+SolarSurplus+'W'+' [** I have >3000 watts of extra power **]')
  TurnOnRelay();
}
else
{
  console.log('Solar Surplus (Incoming Solar - House Consumprion)............. '+SolarSurplus+'W')
  TurnOffRelay();
}
 console.log("-----------------------------------------------------------------------------------------------");
 console.log();
}

function TurnOnRelay(){console.log("Turning on Relay");}
function TurnOffRelay(){console.log("Turning off Relay");}

Login_Then_Do_API_Call();

EXAMPLE NODEJS CODE OUTPUT


Tesla-Powerwall2-API-nodejs

TESLA POWERWALL2 NODEJS ASYNCHRONOUS CODE USING PROMISES
RATHER THAN CALLBACKS


var Obj;
var IncomingSolarPower;
var HouseConsumption;
var ExportingToGrid;
var IncommingSolarSurplus;
var BatteryCharge;
var SurplusEnergyThresholdInWatts=3000; //sufficient surplus for say 2 electric heaters

function execShellCommand(cmd) {
  const exec = require('child_process').exec;
  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
    if (error) {
     console.warn(error);
   }
  resolve(stdout? stdout : stderr);
  });
});
}

function TurnOnRelay(){console.log("Turning on Relay");}
function TurnOffRelay(){ console.log("Turning off Relay");}

async function TeslaAPI() {
  var JSON_Data_From_API;

  //1
  console.log("Logging in to TeslaPowerWall2 API");
  await execShellCommand('sh login_script.sh');

  //2
  console.log("Requesting API data for Incoming Solar power, House Consumption power, Exporting to grid...");
  JSON_Data_From_API = await execShellCommand('sh test_script.sh');
  Obj = JSON.parse(JSON_Data_From_API);
  IncomingSolarPower=Obj["solar"]["instant_power"];
  HouseConsumption=Obj["load"]["instant_power"]
  ExportingToGrid=Obj["site"]["instant_power"];
  IncomingSolarSurplus=IncomingSolarPower-HouseConsumption

  //3
  console.log("Requesting API data for Batteries...");
  JSON_Data_From_API = await execShellCommand('sh BatteryChargePercentage_script.sh');
  var tempobj = JSON.parse(JSON_Data_From_API);
  BatteryCharge=tempobj["percentage"];

  //4
  console.log("Done. Here are the results:"); console.log();
  console.log("-----------------------------------------------------------------------------------------------");
  console.log('Incoming Solar (Solar Instant Power).................. '+IncomingSolarPower+'W');
  console.log('House consumption (Load Instant Power)................. '+HouseConsumption+'W');
  console.log('Exporting To Grid (site Instant Power)................. '+ExportingToGrid+'W');
  console.log('Tesla Battery Charge Level............................. '+BatteryCharge+'%')
  if(IncomingSolarSurplus>SurplusEnergyThresholdInWatts)
  {
   console.log('Solar Surplus (Incoming Solar - House Consumption).. '+IncomingSolarSurplus+'W'+' [** I have >'+SurplusEnergyThresholdInWatts+' watts of extra power **]')
   TurnOnRelay();
  }
  else
  {
   console.log('Solar Surplus (Incoming Solar - House Consumption).. '+IncomingSolarSurplus+'W')
   TurnOffRelay();
  }
  console.log("-----------------------------------------------------------------------------------------------"); console.log();
}
TeslaAPI();


DISCLAIMER

The material and information contained on this website is for general information purposes only.
You should not rely on this material or information on the website as a basis for making any business, legal or any other decisions.
Whilst we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express
or implied about the completeness, accuracy, reliability or availability with respect to the website or the information,
products, services or related graphics contained on the website for any purpose. Any reliance you place on such material is at your own risk





Linux Examples - Comments