07.02.2022, 16:29
(This post was last modified: 07.02.2022, 16:36 by Erwin van der Zwart.)
Code:
-- Load libs
require('socket.http')
require('json')
-- Set timeout
socket.http.TIMEOUT = 15
-- Get data from API
--data = socket.http.request('http://192.168.1.182/solar_api/v1/GetInverterRealtimeData.cgi?scope=System&collection=CommonInverterData')
--data = socket.http.request('http://192.168.1.182/solar_api/v1/GetInverterRealtimeData.cgi?scope=System&collection=CumulationInverterData')
--data = socket.http.request('http://192.168.1.182/solar_api/v1/GetInverterRealtimeData.cgi?scope=System&collection=3PInverterData')
data = socket.http.request('http://192.168.1.182/solar_api/v1/GetInverterRealtimeData.cgi?scope=System')
if data then
function sum(t)
local sum = 0
for k,v in pairs(t) do
sum = sum + v
end
sum = math.round(sum*0.001,2)
return sum
end
decdata = json.pdecode(data)
totaldata = decdata.Body.Data.TOTAL_ENERGY.Values
yeardata = decdata.Body.Data.YEAR_ENERGY.Values
daydata = decdata.Body.Data.DAY_ENERGY.Values
currentdata = decdata.Body.Data.PAC.Values
total = sum(totaldata)
year = sum(yeardata)
day = sum(daydata)
current = sum(currentdata)
previoustotal = storage.get('froniustotal')
if previoustotal then
if total > previoustotal then
grp.checkupdate('32/1/5',total)
previoustotal = storage.set('froniustotal', total)
end
else
storage.set('froniustotal', total)
end
grp.checkupdate('32/1/6',year)
grp.checkupdate('32/1/7',day)
grp.checkupdate('32/1/8',current)
end
I use the storage to check the previous total, as the inverter can go in sleep mode when there is no sun, the API is not sending a valid value at that moment and i dont want to write a invalid value to my trend. So this part is needed if you might see strange values when removing it (: