Enphase enlighten script to get time of use solar production

I am currently on a time-of-use energy rate with my utility and I have been keeping a spreadsheet with my daily energy use. I wanted to add the solar production data from my Enphase system as well. The problem is that the Enphase Enlighten web site doesn’t have a way to get totals for the daily time periods I need without manually adding up the data.

So I signed up for the enlighten API and wrote a small html/javascript utility page that retrieves the daily five minute interval data and adds up the energy for peak and off peak usage. The five minute interval data is subject to quite a bit of rounding which causes the results to be off by a few percent, but it is good enough for my purposes.

enphase TOU

To use the script:

Sign up for an enlighten API key *Edit: More information at the bottom.
Save the script below to a text file with an .html extension
Edit the file fill in the var’s at the top of the script with your API key, User Id, System Id, and utilities peak start and end times (Set to 12pm to 6pm currently).
Save and open the html file in your web browser
Select the date you want to get data for and click the Get Stats button.
The date will auto increment to the next day for convenience.

Tested in Chrome and Firefox.

<!DOCTYPE HTML>
<html>

<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<script>
  //User and system information
  var key="";
  var user_id="";
  var system_id="";
  var peak_start=43200;  //Seconds since midnight
  var peak_end=64800;  //Seconds since midnight


  $(function(){
    $("#DataDate").datepicker();
    
    var today = new Date();
    var yesterday = new Date();
    yesterday.setDate(today.getDate() - 1);
    
    var sDate = (yesterday.getMonth()  + 1) + '/' + yesterday.getDate() + '/' + yesterday.getFullYear();
    
    $("#DataDate").val(sDate);
  });
  
  function IncrementDate()
  {
    var currentDate = new Date($("#DataDate").val());
    currentDate.setDate(currentDate.getDate() + 1);
    
    var sDate = (currentDate.getMonth()  + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();
    
    $("#DataDate").val(sDate);
  }
  
  function GetStats()
  {
    $("#offpeak").val("");
    $("#onpeak").val("");
      
    var UTCDate = Date.parse($("#DataDate").val())/1000;
  
    sURL = "https://api.enphaseenergy.com/api/v2/systems/" + system_id + "/stats?key=" + key + "&amp;user_id=" + user_id + "&amp;start_at=" + UTCDate;
    
    $("#status").text("Requesting: " + sURL);
    
    var peak_start_time = Number(UTCDate) + peak_start;
    var peak_end_time = Number(UTCDate) + peak_end;
    
    $.getJSON(sURL, function( data ) {
      var offpeak = 0;
      var onpeak = 0;
      for(var i=0; i<data.intervals.length; i++){
        if(data.intervals[i].end_at <= peak_start_time || data.intervals[i].end_at > peak_end_time)
          offpeak += data.intervals[i].powr / 12; //.enwh;
        else
          onpeak += data.intervals[i].powr / 12; //.enwh;
      }
      //Convert from Wh to kWh
      var offpeak_kWh = (offpeak / 1000.0).toFixed(3);
      var onpeak_kWh = (onpeak / 1000.0).toFixed(3);
      
      $("#offpeak").val(offpeak_kWh);
      $("#onpeak").val(onpeak_kWh);
      $("#status").text("Finished for " + $("#DataDate").val());
      
      $("#result_log").find("tbody").append(
        $('<tr>').append($('<td>').text($("#DataDate").val()))
                .append($('<td>').text(offpeak_kWh))
                .append($('<td>').text(onpeak_kWh)));
      
      //For convenience when getting consecutive days
      IncrementDate();
      
    }).fail(function(jqxhr, textStatus, error) {
      $("#status").text("Failed, throttling?");
    });	
  }
  
</script>
<style>
#result_log{
  font-family: monospace;
  border-collapse: collapse;
}

#result_log td, th
{
  border: solid 1px;
}
</style>
</head>
<body>

<p>Date: <input type="text" id="DataDate" class="datepicker"></p>

<p>Off peak kWh: <input type="text" id="offpeak"></p>
<p>On peak kWh: <input type="text" id="onpeak"></p>

<p><button onclick="GetStats();">Get Stats</button></p>

<div id="status"></div>
<br/>
<h5>Data retrieval log</h5>
<table id="result_log">
<thead>
<tr><th>Date</th><th>Off Peak</th><th>On Peak</th></tr>
</thead>
<tbody>
</tbody>
</table>

<p>Powered by 
<a href='http://enphase.com/'><img src='https://developer.enphase.com/images/ENPH_logo_scr_RGB_API_sm.png' alt='Enphase Energy Logo' /></a></p>

</body>
</html>

*Edit to add more instructions on setting up the Enphase Enlighten developer account and getting your keys and authorization. This stuff is all over the place!

Enphase Enlighten Developer setup

Thanks to RegGuheert on the mynissanleaf forums for pointing out the need for more information on getting this setup! You can read his post here.

  • Sign up for the “watt plan” at Enphase here. It’s free, but limits the number of requests, which is fine for what we are doing. This script relies on you setting up a developer account – so this essentially becomes YOUR APP in the perspective of Enphase.
  • Get your API Key and the authorization URL from the developer application tab by clicking on your application name (this should have been created at sign up, if not, do it now). The authorization URL is not clickable, just copy it and we will later paste it into a browser window to allow the app to access your data.
  • Get your system id from Enlighten Manager: Log onto your Enlighten Manager site. Click on the settings/gear tab on the top right to get to your system details page.
    enphasegear
    This page will show your System ID (It’s also in the URL of your browser). While on this page, scroll down to the Privacy Settings section and make sure the “Allow API access to my system data” is checked.
  • Get your user id from Enlighten Manager: Go to the account details page and scroll down to the API Access section and you will find your user id.
  • Finally, authorize the app to access your systems data by pasting the authorization URL link into your browser and clicking the “Yes, allow access” button.
  • Now you should have your API key, system id, and user id to enter into the script and the script should be authorized to read your data.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s