#!/usr/bin/env ruby require 'rexml/document' require 'net/http' require 'pvoutput/client' require 'yaml' # Load the configuration from the yaml file sajcollector_config = YAML::load_file('saj_collector.yaml') # Create a pvoutput with the configured system_id and api_key which both # can be obtained from the pvoutput website pvoutput = PVOutput::Client.new(sajcollector_config[:system_id], sajcollector_config[:api_key]) options = { 'temperature' => 0, # Temperature in celcius 'energy_generated' => 0, # Energy generation in watt hours 'power_generated' => 0, # Power generation in watts 'voltage' => 0, # Voltage in volts 'amperage' => 0, # Amperage 'power_today' => 0, 'power_all_time' => 0, 'runtime_today' => 0, 'runtime_all_time' => 0 } # Get the current realtime data from the SAJ device c = Net::HTTP.get(sajcollector_config[:saj], '/real_time_data.xml') # Let REXML parse the XML site doc = REXML::Document.new c # Get the temperature doc.elements.each('real_time_data/temp') { |e| options[:temperature] = e.text } # e-today is in kWh where pvoutput is Wh doc.elements.each('real_time_data/e-today') { |e| options[:energy_generated] = e.text.to_f * 1000 } # Get the power generated doc.elements.each('real_time_data/p-ac') { |e| options[:power_generated] = e.text } # Get the current amperage doc.elements.each('real_time_data/i-grid') { |e| options[:amperage] = e.text } # Get the voltage doc.elements.each('real_time_data/v-grid') { |e| options[:voltage] = e.text } doc.elements.each('real_time_data/e-today') { |e| options[:power_today] = e.text } doc.elements.each('real_time_data/e-total') { |e| options[:power_all_time] = e.text } doc.elements.each('real_time_data/t-today') { |e| options[:runtime_today] = e.text } doc.elements.each('real_time_data/t-total') { |e| options[:runtime_all_time] = e.text } puts '-------------------------' puts puts "Time: #{Time.now}" puts puts 'Realtime:' puts " Temp: #{options[:temperature]} C" puts " V: #{options[:voltage]} V" puts " I: #{options[:amperage]} A" puts " P: #{options[:power_generated]} W" puts puts 'Total:' puts " Power (today): #{options[:power_today]} kWH" puts " Power (all time): #{options[:power_all_time]} kWH" puts " Run Time: #{options[:runtime_today]} Hours" puts " Run Time: #{options[:runtime_all_time]} Hours" pvoutput.add_status( :energy_generated => options[:energy_generated], :power_generated => options[:power_generated], :temperature => options[:temperature], :voltage => options[:voltage] )