forked from RDols/dzRequestSolarInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdzRequestSolarInfo.lua
More file actions
166 lines (156 loc) · 6.33 KB
/
dzRequestSolarInfo.lua
File metadata and controls
166 lines (156 loc) · 6.33 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--------------------------------------------------------------------------------
local function MakeCleanNumber(dirtyNumber)
local isDecimalComma = true
if isDecimalComma then
local value = dirtyNumber:gsub("%.", "")
value = value:gsub(",", ".")
return tonumber(value)
else
local value = dirtyNumber:gsub(",", "")
return tonumber(value)
end
end
--------------------------------------------------------------------------------
local function AddReportData(json, component)
local id = tostring(component.id)
component.DayEnergy = 0
if json.reportersData[id] then
component.DayEnergy = json.reportersData[id].unscaledEnergy or 0
end
end
--------------------------------------------------------------------------------
local function AddReportInfo(json, component)
local id = tostring(component.id)
data = json.reportersInfo[id]
if data then
--component.LastMeasurement = data.lastMeasurement --Not updated by website, useless
component.Name = data.name
for k, v in pairs(data.localizedMeasurements or {}) do
if string.match(k, "%[W%]") then
component.CurrentPower = MakeCleanNumber(v)
end
end
end
end
--------------------------------------------------------------------------------
local function GetOptimizerData(json, component)
if component.data and component.data.type == "POWER_BOX" then
local optimizer = component.data
AddReportData(json, optimizer)
AddReportInfo(json, optimizer)
return optimizer
end
end
--------------------------------------------------------------------------------
local function GetStringData(json, info, component)
if component.data and component.data.type == "STRING" then
local powerstring = component.data
AddReportData(json, powerstring)
AddReportInfo(json, powerstring)
powerstring.Optimizers = {}
for _, child in pairs(component.children) do
local optimizer = GetOptimizerData(json, child)
if optimizer then
table.insert(powerstring.Optimizers, optimizer)
table.insert(info.Optimizers, optimizer)
end
end
return powerstring
end
end
--------------------------------------------------------------------------------
local function GetInverterData(json, info, component)
if component.data and component.data.type == "INVERTER" then
local inverter = component.data
AddReportData(json, inverter)
AddReportInfo(json, inverter)
inverter.Strings = {}
for _, child in pairs(component.children) do
local string = GetStringData(json, info, child)
if string then
table.insert(inverter.Strings, string)
table.insert(info.Strings, string)
end
end
return inverter
end
end
--------------------------------------------------------------------------------
local function RequestSolardEdge(domoticz)
local username = "your@email.com"
local password = "password"
local siteID = "0000000"
local authorization = string.format("%s:%s", username, password)
authorization = string.format("Basic %s", domoticz.utils.toBase64(authorization))
local url = string.format("https://monitoring.solaredge.com/solaredge-apigw/api/sites/%s/layout/logical", siteID)
local headers = { ['Authorization'] = authorization }
domoticz.openURL(
{
url = url,
method = 'GET',
headers = headers,
callback = 'SolarEdgeWebRespondse'
})
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
return
{
--------------------------------------------------------------------------------
active = true,
--------------------------------------------------------------------------------
on =
{
timer =
{
'every 1 minutes'
},
httpResponses =
{
'SolarEdgeWebRespondse'
}
},
--------------------------------------------------------------------------------
data =
{
},
--------------------------------------------------------------------------------
execute = function(domoticz, item)
--------------------------------------------------------------------------------
--print("dzRequestSolarInfo : -=[ Start ]===================================================")
if (item.isTimer) then
RequestSolardEdge(domoticz)
elseif (item.isHTTPResponse) then
if (item.ok) then
--print("dzRequestSolarInfo : Response OK")
local info = { Inverters = {}, Strings = {}, Optimizers = {} }
for _, child in pairs(item.json.logicalTree.children) do
local inverter = GetInverterData(item.json, info, child)
if inverter then
table.insert(info.Inverters, inverter)
end
end
local utils = domoticz.utils
for _, v in ipairs(info.Inverters) do
--print(string.format("dzRequestSolarInfo : Inverter %s = %0.2f Wh", v.Name, v.DayEnergy or 0))
if utils.deviceExists(v.Name) then
domoticz.devices(v.Name).updateElectricity(v.CurrentPower, v.DayEnergy)
end
end
for _, v in ipairs(info.Strings) do
--print(string.format("dzRequestSolarInfo : String %s = %0.2f Wh", v.Name, v.DayEnergy or 0))
if utils.deviceExists(v.Name) then
domoticz.devices(v.Name).updateElectricity(v.CurrentPower, v.DayEnergy)
end
end
for _, v in ipairs(info.Optimizers) do
--print(string.format("dzRequestSolarInfo : Optimizer %s = %0.2f Wh", v.Name, v.DayEnergy or 0))
if utils.deviceExists(v.Name) then
domoticz.devices(v.Name).updateElectricity(v.CurrentPower, v.DayEnergy)
end
end
end
end
--print("dzRequestSolarInfo : -=[ End ]=====================================================")
end
}