Tesla Powerwall 2 – API interface

Meters / Power output stats

Calling the below URLs does not require authentication. Each will return JSON output with key-value pairs:

GET /api/meters/aggregates

request: curl http://192.168.xxx.xxx/api/meters/aggregates

Full list of discovered APIs – Summary:

 

  • BatterySOC = “/api/system_status/soe”;
  • MeterAggregates = “/api/meters/aggregates”;
  • CustomerRegistration = “/api/customer/registration”;
  • PowerWalls = “/api/powerwalls”;
  • ConnectionStatus = “/api/sitemaster”;
  • Version = “/api/status”;

 

Application

It includes a script which gets the SoC from the gateway, writes the value to a RRD file and an example graph that can be created from the RRD.
http://www.bowmill.net/examples/example.zip

Unpack it to c:\example or change the paths in the script.

createsocrrd.bat will create an rrd db called soc.rrd with one variable socpct
readsoc.ps1 needs to be modified to your GW address and will write the soc value to soc.rrd and after running the PS for a while …
graphit.bat will run rrdtool graph and (hopefully) create soc.png – a 4 hr graph of the soc value.

It uses RRDtool https://oss.oetiker.ch/rrdtool/ Full details of parameters can be found on his site.

If this is your first time running PS scripts on your Windows machine you may have to modify the default execution policy – running Powershell ISE as an admin task and entering Set-ExecutionPolicy RemoteSigned should be sufficient but here Google is your friend.

 

 

Application / Implementation

Sources

Advertisements

Opticon OPN2001 – CSV file data manipulation using PowerShell

CSV file RAW data should be like this =

BarCode Number, Time of Scan, Date of Scan
20120711001, 21:00:00, 2012-07-11
321530, 21:01:01, 2012-07-11
123456, 21:02:02, 2012-07-11
20120711001, 22:00:00, 2012-07-11
123456, 22:01:01, 2012-07-11
321530, 22:02:02, 2012-07-11

After the table modification with the PowerShell script, it should result as follow =

Mission ID, Person ID, Start Time, Start Date, Stop Time, Stop Date
20120711001, 20120711001, 21:00:00, 2012-07-11, 22:00:00, 2012-07-11
20120711001, 321530, 21:01:01, 2012-07-11, 22:01:01, 2012-07-11
20120711001, 123456, 21:02:02, 2012-07-11, 22:02:02, 2012-07-11

The basic PowerShell cmdlet to read and write CSV file are:

  • Import-Csv Cmdlet – Read in a Comma-Separated Values File
    Import-Csv c:\scripts\test.txt
  • Export-Csv Cmdlet – Saving Data as a Comma-Separated Values File
    Export-Csv c:\scripts\test.txt
  • Filter result before Importing
    Import-Csv c:\scripts\test.txt | Where-Object {$_.department -eq "Finance"}
  • Get-Content Cmdlet – Reading a Text File
    Get-Content c:\scripts\test.txt

    Blog example to manipulate data inside PowerShell:

  • parse-textObject
  • Typecasting imported CSV data
    import-csv test.csv | set-type -type_hash @{id=[int];workingset=[int];cpu=[float]} | ogv
  • Append column data
    Import-Csv d:\temp\so\csv1.txt -Delimiter ‘;’ | % { $_.col3 = ‘append\’ +$_.col3; $_ } | Export-Csv d:\temp\so\csv2.txt -Delimiter ‘;’
    or
    Import-Csv -Delim ';' cols.csv | Foreach {$_.col3 = "prepend\$($_.col3)";$_} | Export-Csv cols2.csv -Delim ';' -NoTypeInformation
  • Replacing Data in .CSV Field
    $SCRIPT:IMP = Import-CSV C:\Tools\PSScripts\ShellAuditSubSet.csv
    $IMP | foreach-object { $_.Message
    $CleanReturn = (CleanData ($_.Message))
    if($Messy -cne "")
    {
    $_.Message = $CleanReturn
    Write-Host "MESSAGE DATA IS " + $_.Message
    }
    }
  • Manipulate array data gathered from import-csv