Skip to content

Instantly share code, notes, and snippets.

@ofross
Created April 2, 2018 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofross/b99aaf1101f261e5768451cdc8d97978 to your computer and use it in GitHub Desktop.
Save ofross/b99aaf1101f261e5768451cdc8d97978 to your computer and use it in GitHub Desktop.
Script to take electric consumption data from PG&E and post it to PVOutput.org so as to compare net energy use
#!/bin/sh -x
# Get hourly consumption data from PG&E, modify it to be daily, and them upload
# consumption data to PVOutput.org. One main/sad limitation is data is aggregated
# daily, not hourly. wah wah.
#
# Super thanks, and totally depends on: https://github.com/qwertangel/getPGEUsageData
#
# Post as per https://pvoutput.org/help.html#api ; Read the "Service API" section
#
#
# Call as `pge_to_pvoutput.sh 5 4` to get the data for the single day, 5 days ago.
#
#
# Globals - YOU MUST UPDATE THESE
PGEUSER=""
PGEPASSWORD=""
PVAPIKEY=""
PVSYSTEMID=""
PGEDATADIR="" # From dependency install
# Relative days
RECENT=$1
FARTHER=$2
DATE1=`date -v-${RECENT}d "+%Y-%m-%d"`
DATE2=`date -v-${FARTHER}d "+%Y-%m-%d"`
DATE=`echo $DATE1 | sed 's/-//g'`
TMPFILE=/tmp/$DATE1/$DATE1.pgedata
TMPDIR=`dirname $TMPFILE`
# Get data from PG&E
mkdir -p $TMPDIR
cd $PGEDATADIR
$PGEDATADIR/executeCasper.sh $DATE1 $DATE2 $TMPFILE $PGEUSER $PGEPASSWORD &> ./log.txt
# Unzip the zip file to csv
cd $TMPDIR
unzip $TMPFILE
CSVFILE=`ls -1tr | grep pge_electric | head -1`
# Grok the fields and do some math
KILO="1000"
USAGE=`grep $DATE1 $CSVFILE | awk -F\, '{print $5}' | awk '{s+=$1} END {print s}'`
CONSUMPTION="$(echo "$USAGE*$KILO" | bc)"
# Upload to PVoutput.
curl -d "d=$DATE" -d "c=$CONSUMPTION" -H "X-Pvoutput-Apikey: $PVAPIKEY" -H "X-Pvoutput-SystemId: $PVSYSTEMID" https://pvoutput.org/service/r2/addoutput.jsp
#Clean Up
rm -rf $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment