Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculating Behind the Meter Solar Usage
require 'selenium-webdriver'
require 'dotenv/load'
require 'csv'
@headless = Selenium::WebDriver.for :firefox
def powershopLogin
@headless.navigate.to 'https://secure.powershop.com.au/'
username = @headless.find_element(:id, "email")
password = @headless.find_element(:id, "password")
username.send_keys ENV['POWERSHOPUSERNAME']
password.send_keys ENV['POWERSHOPPASSWORD']
# Submit login form
@headless.find_element(:name, "commit").click()
end
def extractPowershopData
powershopLogin
@headless.navigate.to "https://secure.powershop.com.au/properties/31480/reports/meter_readings"
@solarExportReadingYesterday = @headless.find_element(:xpath => "//div[@id='consumer_meter_readings']/table/tbody/tr[1]/td[@class='readings' and 5]/table[1]/tbody[1]/tr[4]/td[1]/acronym[1]").text.to_s
@solarExportReadingDayBeforeYesterday = @headless.find_element(:xpath => "//div[@id='consumer_meter_readings']/table/tbody/tr[2]/td[@class='readings' and 5]/table[1]/tbody[1]/tr[4]/td[1]/acronym[1]").text.to_s
@powerImportReadingYesterday = @headless.find_element(:xpath => "//div[@id='consumer_meter_readings']/table/tbody/tr[1]/td[@class='readings' and 5]/table[1]/tbody[1]/tr[5]/td[1]/acronym[1]").text.to_s
@powerImportReadingDayBeforeYesterday = @headless.find_element(:xpath => "//div[@id='consumer_meter_readings']/table/tbody/tr[2]/td[@class='readings' and 5]/table[1]/tbody[1]/tr[5]/td[1]/acronym[1]").text.to_s
puts @solarExportReadingYesterday
puts @solarExportReadingDayBeforeYesterday
puts @powerImportReadingYesterday
puts @powerImportReadingDayBeforeYesterday
extractInverterData
end
def extractInverterData
@headless.navigate.to "http://10.0.0.243/"
sleep 10
@solarGeneratedYesterday = @headless.find_element(:id, 'EnergyYesterday').text
puts @solarGeneratedYesterday
end
def quit
@headless.quit
end
def performCalculations
solarFeedIn = (@solarExportReadingYesterday.to_f-@solarExportReadingDayBeforeYesterday.to_f).round(2)
solarFeedInRebate = ((solarFeedIn.to_f*11.17)/100).round(2)
powerPurchase = (@powerImportReadingYesterday.to_f-@powerImportReadingDayBeforeYesterday.to_f).round(2)
powerPurchaseCost = ((powerPurchase.to_f*30.14)/100).round(2)
solarConsumption = (@solarGeneratedYesterday.to_f-solarFeedIn.to_f).round(2)
solarConsumptionSavings = ((solarConsumption.to_f*30.14)/100).round(2)
totalConsumption = (solarConsumption+powerPurchase).round(2)
potentialBatterySavings = ((solarFeedIn.to_f*30.14)/100).round(2)
potentialBatteryModifiedGridUsage = (powerPurchase-solarFeedIn).round(2)
overallCost = (powerPurchaseCost-solarFeedInRebate).round(2)
report = "Yesterday we consumed "+solarConsumption.to_s+"kWh of solar (saving $#{solarConsumptionSavings.to_s} and purchased "+powerPurchase.to_s+"kWh (costing $#{powerPurchaseCost.to_s}). Total consumption was "+totalConsumption.to_s+"kWH). We exported "+solarFeedIn.to_s+"kWh back to the grid (making $#{solarFeedInRebate}). If we had a battery, we'd have saved $#{potentialBatterySavings} and would've only purchased #{potentialBatteryModifiedGridUsage} kWh from the grid. Overall, yesterday cost $#{overallCost}."
puts report
sendNotification(report)
writeToCSV(solarFeedIn, solarFeedInRebate,powerPurchase,powerPurchaseCost,solarConsumption,solarConsumptionSavings,totalConsumption,potentialBatterySavings,potentialBatteryModifiedGridUsage,overallCost)
end
def sendNotification(report)
url = URI.parse("https://api.pushover.net/1/messages.json")
req = Net::HTTP::Post.new(url.path)
req.set_form_data({
:token => ENV['PUSHOVER_APP_TOKEN'],
:user => ENV['PUSHOVER_USER_KEY'],
:message => report,
:title => "Your daily ⚡️ report",
:html => 1,
})
res = Net::HTTP.new(url.host, url.port)
res.use_ssl = true
res.verify_mode = OpenSSL::SSL::VERIFY_PEER
res.start {|http| http.request(req) }
end
def writeToCSV(solarFeedIn,solarFeedInRebate,powerPurchase,powerPurchaseCost,solarConsumption,solarConsumptionSavings,totalConsumption,potentialBatterySavings,potentialBatteryModifiedGridUsage,overallCost)
@csv_headers = [
"date",
"solar Feed In (kWh)",
"solar Feed In Rebate ($)",
"power Purchase (kWh)",
"power Purchase Cost ($)",
"solar Consumption (kWh)",
"solar Consumption Savings ($)",
"total Consumption (kWh)",
"potential Battery Savings ($)",
"potential Battery Usage (kWh)",
"overall Cost ($)"
]
@csv_name = "electricityUsageData.csv"
def write_to_csv(row)
if csv_exists?
CSV.open(@csv_name, 'a+') { |csv| csv << row }
else
# create and add headers if doesn't exist already
CSV.open(@csv_name, 'wb') do |csv|
csv << @csv_headers
csv << row
end
end
end
def csv_exists?
@exists ||= File.file?(@csv_name)
end
todaysDate = Time.now
array = [ [todaysDate,solarFeedIn,solarFeedInRebate,powerPurchase,powerPurchaseCost,solarConsumption,solarConsumptionSavings,totalConsumption,potentialBatterySavings,potentialBatteryModifiedGridUsage,overallCost]]
array.each { |row| write_to_csv(row) }
end
puts Time.now.to_s+" - Starting Electricity use calculations"
extractPowershopData
extractInverterData
quit
performCalculations
puts Time.now.to_s+" - Electricity Calculations finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment