-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar.4m.rb
118 lines (98 loc) · 4.21 KB
/
solar.4m.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/ruby
# By [email protected]
# Extract data from local Envoy and send to STDOUT in the format for the BitBar tool for Mac.
# see https://github.com/matryer/bitbar
# <bitbar.title>Enphase Solar Status for Envoy</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Rodos</bitbar.author>
# <bitbar.author.github>iamrodos</bitbar.author.github>
# <bitbar.desc>Display the status of your Enphase Envoy solar system by quering to Envoy on the local network.</bitbar.desc>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>http://url-to-about.com/</bitbar.abouturl>
require 'net/http'
require 'net/http/digest_auth'
require 'json'
ENVOY_IP = "enter your IP address here"
SYSTEM_SIZE_WATTS = 6000 # Be good to derive this from the system but can't find it in local data, only via the Enlighten API
# If the number is great than 1000 shorten it to a k value
def shortNumberFormat(val)
if val.abs < 1000
return val.round(0).abs.to_s
else
return (val.abs/1000).round(1).to_s + "k"
end
end
# Put comma separator into numbers
def separateComma(number)
number.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
end
begin
raise "Not a valid IP address. Update ENVOY_IP in script" unless ENVOY_IP.match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/)
http = Net::HTTP.new(ENVOY_IP)
# Determine load values
uri = URI("http://" + ENVOY_IP + "/production.json?details=1")
req = Net::HTTP::Get.new(uri.request_uri)
req['Content-Type'] = 'application/json'
res = http.request(req)
raise "Error on http request. Response: " + res.message unless res.is_a?(Net::HTTPSuccess)
importing = JSON.parse(res.body)["consumption"][1]["wNow"]
consuming = JSON.parse(res.body)["consumption"][0]["wNow"]
producing = JSON.parse(res.body)["production"][1]["wNow"]
# Set the appropraite icon based on the power. Power plug, or shade or sun depending on grid load
case
when importing > 0
icon = "🔌" # Power plug
when producing < (SYSTEM_SIZE_WATTS / 2)
icon = "⛅" # Cloudy
else
icon = "☀️" # Sun
end
# Send out the first portion of the data for BitBar to display
puts "#{icon} #{shortNumberFormat(importing)}W| color=#{importing > 0 ? "red":"green"} size=12"
puts "---"
puts "Producing #{separateComma(producing.round(0))}W|size=12"
puts "Consuming #{separateComma(consuming.round(0))}W|size=12"
# Get the serial number of the envoy
uri = URI("http://" + ENVOY_IP + "/info.xml")
req = Net::HTTP::Get.new(uri.request_uri)
res = http.request(req)
raise "Error on http request. Response: " + res.message unless res.is_a?(Net::HTTPSuccess)
# Hack out the <sn>...</sn> from the front of the XML returned. Not fancy.
serial_number = res.body.scan(/sn>(\d*)<\/sn>/).first.first
# Now lets see how much the panels are producing. This is an authenitcated request based on the serial number
uri = URI('http://' + ENVOY_IP + '/api/v1/production/inverters')
uri.user = 'envoy'
uri.password = serial_number[-6,6]
# Make the first request to get the auth
req = Net::HTTP::Get.new uri.request_uri
res = http.request(req)
# Now send with an authentication digest
digest_auth = Net::HTTP::DigestAuth.new
auth = digest_auth.auth_header(uri, res['www-authenticate'], 'GET')
req = Net::HTTP::Get.new(uri.request_uri)
req.add_field('Authorization', auth)
res = http.request(req)
raise "Error on http request. Response: " + res.message unless res.is_a?(Net::HTTPSuccess)
# Determine the range of invert output
min = 99999999
max = 0
count = 0
inverters = JSON.parse(res.body)
inverters.each do |inverter|
val = inverter["lastReportWatts"]
next if val < 2
count += 1
min = val if val < min
max = val if val > max
end
# Send out the inverter results for BitBar to display
if count > 0
puts "#{inverters.length} from #{min}W to #{max}W| size=12"
else
puts "No inverters generating.| size=12"
end
rescue StandardError => e
puts ":warning: Error| size=12"
puts "---"
puts e.message
end