Posts: 176
Threads: 42
Joined: Jul 2015
Reputation:
2
Hi
Has anyone made script for connecting to Solis inverter via API?
Attached the api manual.
I know the inverter can be accessed via modbus rtu but cannot do that and the solis cloud at the same time so I would like to only get this via api.
Attached Files
Posts: 7764
Threads: 42
Joined: Jun 2015
Reputation:
447
Try this, change key_id/key_secret as needed. request function accepts two parameters - request URI (starting with /v1/api/...) and request body (either a table which is converted to JSON or a string which is sent as is).
The calculated signature differs from documented example even though it is calculated the same way. Might be an error in the documentation.
Code:
require('json')
require('encdec')
require('socket.http')
require('ltn12')
host = 'https://www.soliscloud.com:13333'
key_id = '2424'
key_secret = '6680182547'
function request(uri, body)
local method = 'POST'
if type(body) ~= 'string' then
body = json.encode(body)
end
local content_md5 = encdec.md5(body, true)
content_md5 = encdec.base64enc(content_md5)
local content_type = 'application/json'
local date = os.date('!%a, %d %b %Y %X GMT')
local sign = method .. '\n' ..
content_md5 .. '\n' ..
content_type .. '\n' ..
date .. '\n' ..
uri
sign = encdec.hmacsha1(sign, key_secret, true)
local url = host .. uri
local auth = 'API ' .. key_id .. ':' .. encdec.base64enc(sign)
local headers = {
['Content-MD5'] = content_md5,
['Content-Type'] = content_type,
['Date'] = date,
['Authorization'] = auth,
['Content-Length'] = #body,
}
local resp = {}
local res, code = socket.http.request({
url = url,
method = method,
source = ltn12.source.string(body),
sink = ltn12.sink.table(resp),
headers = headers
})
if res and code == 200 then
return table.concat(resp)
else
return nil, code
end
end
res, err = request('/v1/api/userStationList', { userId = '1145611319416590338' })
log(res, err)
Posts: 176
Threads: 42
Joined: Jul 2015
Reputation:
2
Posts: 111
Threads: 14
Joined: Nov 2019
Reputation:
6
20.12.2022, 08:28
(This post was last modified: 04.01.2023, 16:33 by Joep .)
I tried this script but got the result below. From line 58 there is a userId set but i don't know how to get it as it didn't come with the API credentials i got. Attached the latest version of the API.
* arg: 1
* string: {"success":true,"code":"1","msg":"数据异常 请联系管理员","data":null}
* arg: 2
* nil
This part translated: 数据异常 请联系管理员
Means: The data is abnormal, please contact the administrator
Anyone who can help me out please? Many thanks in advance.
Posts: 111
Threads: 14
Joined: Nov 2019
Reputation:
6
UPDATE
After a lot of trail and error i finally wrote an article that it's related to a Soliscloud issue when you reveive this error message.
The only thing you'll need to do is to first disable the API from your soliscloud.com account and then enable it again. After that it will work.
Posts: 212
Threads: 61
Joined: May 2018
Reputation:
4
05.12.2023, 12:04
(This post was last modified: 05.12.2023, 12:45 by tomnord .)
(20.05.2022, 14:31) admin Wrote: Try this, change key_id/key_secret as needed. request function accepts two parameters - request URI (starting with /v1/api/...) and request body (either a table which is converted to JSON or a string which is sent as is).
The calculated signature differs from documented example even though it is calculated the same way. Might be an error in the documentation.
Code:
require('json')
require('encdec')
require('socket.http')
require('ltn12')
host = 'https://www.soliscloud.com:13333'
key_id = '2424'
key_secret = '6680182547'
function request(uri, body)
local method = 'POST'
if type(body) ~= 'string' then
body = json.encode(body)
end
local content_md5 = encdec.md5(body, true)
content_md5 = encdec.base64enc(content_md5)
local content_type = 'application/json'
local date = os.date('!%a, %d %b %Y %X GMT')
local sign = method .. '\n' ..
content_md5 .. '\n' ..
content_type .. '\n' ..
date .. '\n' ..
uri
sign = encdec.hmacsha1(sign, key_secret, true)
local url = host .. uri
local auth = 'API ' .. key_id .. ':' .. encdec.base64enc(sign)
local headers = {
['Content-MD5'] = content_md5,
['Content-Type'] = content_type,
['Date'] = date,
['Authorization'] = auth,
['Content-Length'] = #body,
}
local resp = {}
local res, code = socket.http.request({
url = url,
method = method,
source = ltn12.source.string(body),
sink = ltn12.sink.table(resp),
headers = headers
})
if res and code == 200 then
return table.concat(resp)
else
return nil, code
end
end
res, err = request('/v1/api/userStationList', { userId = '1145611319416590338' })
log(res, err)
Ref the above code. I've tried this and got a respons, but not in a usable table.
I get this as a responce, how do I get this as a Table? The script does not identify this as a table:
Code:
Solis_Test 05.12.2023 12:59:09
* arg: 1
* string: {"success":true,"code":"0","msg":"success","data":{"inverterStatusVo":{"all":2,"normal":2,"fault":0,"offline":0,"mppt":0},"page":{"records":[{"id":"**************","sn":"****","model":"1213","collectorSn":"****","userId":"******","productModel":"1213","nationalStandards":"16","inverterSoftwareVersion":"f0cee6","inverterSoftwareVersion2":"000000","dcInputType":7,"acOutputType":1,"stationType":1,"stationId":"*******","tag":"YingZhen","rs485ComAddr":"101","simFlowState":-5,"power":30.000,"powerStr":"kW","pac":1.250,"pac1":0.001,"pacStr":"kW","state":1,"stateExceptionFlag":0,"ivSupport":0,"inverterConfig":"0","fullHour":0.06,"totalFullHour":417.57,"maxDcBus":0.0,"maxDcBusTime":"1701777325000","maxUac":6548.1,"maxUacTime":"1690352237551","maxUpv":4938.2,"maxUpvTime":"1690900161804","timeZone":1.00,"timeZoneStr":"(UTC+01:00)","timeZoneName":"(UTC+01:00) 阿姆斯特丹,柏林,伯尔尼,罗马,斯德哥尔摩,维也纳","dataTimestamp":"1701777325000","dataTimestampStr":"2023-12-05 12:55:25 (UTC+01:00)","fisTime":"1688723533485","fisTimeStr":"2023-07-07 11:52:13 (UTC+01:00)","fisGenerateTime":1688723533000,"fisGenerateTimeStr":"2023-07-07 11:52:13 (UTC+01:00)","inverterMeterModel":1,"updateShelfBeginTime":1685203200000,"updateShelfEndTime":1843056000000,"updateShelfEndTimeStr":"2028-05-28","updateShelfTime":"5","collectorId":"************","dispersionRate":16.66666667,"currentState":"0","pow1":170.67,"pow2":227.56,"pow3":113.78,"pow4":284.45,"pow5":0.0,"pow6":402.2,"pow7":0.0,"pow8":405.65,"pow9":0.0,"pow10":0.0,"pow11":0.0,"pow12":0.0,"pow13":0.0,"pow14":0.0,"pow15":0.0,"pow16":0.0,"pow17":0.0,"pow18":0.0,"pow19":0.0,"pow20":0.0,"pow21":0.0,"pow22":0.0,"pow23":0.0,"pow24":0.0,"pow25":0.0,"pow26":0.0,"pow27":0.0,"pow28":0.0,"pow29":0.0,"pow30":0.0,"pow31":0.0,"pow32":0.0,"gridPurchasedTodayEnergy":0.000,"gridPurchasedTodayEnergyStr":"kWh","gridSellTodayEnergy":0.000,"gridSellTodayEnergyStr":"kWh","psumCalPec":"1","batteryPower":0.000,"batteryPowerStr":"kW","batteryPowerPec":"1","batteryCapacitySoc":0.000,"parallelStatus":0,"parallelAddr":0,"parallelPhase":0,"parallelBattery":0,"batteryTodayChargeEnergy":0.000,"batteryTodayChargeEnergyStr":"kWh","batteryTotalChargeEnergy":0.000,"batteryTotalChargeEnergyStr":"kWh","batteryTodayDischargeEnergy":0.000,"batteryTodayDischargeEnergyStr":"kWh","batteryTotalDischargeEnergy":0.000,"batteryTotalDischargeEnergyStr":"kWh","bypassLoadPower":0.000,"bypassLoadPowerStr":"kW","backupTodayEnergy":0.000,"backupTodayEnergyStr":"kWh","backupTotalEnergy":0.000,"backupTotalEnergyStr":"kWh","familyLoadPower":0.000,"familyLoadPowerStr":"kW","totalLoadPower":0.000,"totalLoadPowerStr":"kW","homeLoadTodayEnergy":0.000,"homeLoadTodayEnergyStr":"kWh","isS5":0,"batteryModel":1,"bypassAcOnoffSet":0.0,"parallelOnoff01":0.000,"parallelOnoff02":0.000,"rfState":1,"etoday":1.800,"etotal":15.254,"psum":0.000,"psumCal":1.250,"etoday1":1.800,"etotal1":15254.000,"offlineLongStr":"--","psumStr":"kW","psumCalStr":"kW","etotalStr":"MWh","etodayStr":"kWh"},{"id":"************","sn":"110192222230019","model":"19","collectorSn":"************","userId":"***************","productModel":"19","nationalStandards":"16","inverterSoftwareVersion":"ff0f20","inverterSoftwareVersion2":"000000","dcInputType":11,"acOutputType":1,"stationType":1,"stationId":"1298491919449164769","tag":"YingZhen","rs485ComAddr":"101","simFlowState":-5,"power":50.000,"powerStr":"kW","pac":1.420,"pac1":0.001,"pacStr":"kW","state":1,"stateExceptionFlag":0,"ivSupport":0,"inverterConfig":"0","fullHour":0.05,"totalFullHour":292.7,"maxDcBus":0.0,"maxDcBusTime":"1701777481000","maxUac":5486.3,"maxUacTime":"1691492235614","maxUpv":5402.3,"maxUpvTime":"1690705689852","timeZone":1.00,"timeZoneStr":"(UTC+01:00)","timeZoneName":"(UTC+01:00) 阿姆斯特丹,柏林,伯尔尼,罗马,斯德哥尔摩,维也纳","dataTimestamp":"1701777481000","dataTimestampStr":"2023-12-05 12:58:01 (UTC+01:00)","fisTime":"1688723762868","fisTimeStr":"2023-07-07 11:56:02 (UTC+01:00)","fisGenerateTime":1688723763000,"fisGenerateTimeStr":"2023-07-07 11:56:03 (UTC+01:00)","inverterMeterModel":1,"updateShelfBeginTime":1662825600000,"updateShelfEndTime":1820592000000,"updateShelfEndTimeStr":"2027-09-11","updateShelfTime":"5","collectorId":"********","dispersionRate":38.29708438,"currentState":"0","pow1":93.14,"pow2":0.0,"pow3":90.86,"pow4":136.29,"pow5":0.0,"pow6":139.5,"pow7":88.76,"pow8":177.52,"pow9":183.36,"pow10":244.48,"pow11":185.04,"pow12":246.72,"pow13":0.0,"pow14":0.0,"pow15":0.0,"pow16":0.0,"pow17":0.0,"pow18":0.0,"pow19":0.0,"pow20":0.0,"pow21":0.0,"pow22":0.0,"pow23":0.0,"pow24":0.0,"pow25":0.0,"pow26":0.0,"pow27":0.0,"pow28":0.0,"pow29":0.0,"pow30":0.0,"pow31":0.0,"pow32":0.0,"gridPurchasedTodayEnergy":0.000,"gridPurchasedTodayEnergyStr":"kWh","gridSellTodayEnergy":0.000,"gridSellTodayEnergyStr":"kWh","psumCalPec":"1","batteryPower":0.000,"batteryPowerStr":"kW","batteryPowerPec":"1","batteryCapacitySoc":0.000,"parallelStatus":0,"parallelAddr":0,"parallelPhase":0,"parallelBattery":0,"batteryTodayChargeEnergy":0.000,"batteryTodayChargeEnergyStr":"kWh","batteryTotalChargeEnergy":0.000,"batteryTotalChargeEnergyStr":"kWh","batteryTodayDischargeEnergy":0.000,"batteryTodayDischargeEnergyStr":"kWh","batteryTotalDischargeEnergy":0.000,"batteryTotalDischargeEnergyStr":"kWh","bypassLoadPower":0.000,"bypassLoadPowerStr":"kW","backupTodayEnergy":0.000,"backupTodayEnergyStr":"kWh","backupTotalEnergy":0.000,"backupTotalEnergyStr":"kWh","familyLoadPower":0.000,"familyLoadPowerStr":"kW","totalLoadPower":0.000,"totalLoadPowerStr":"kW","homeLoadTodayEnergy":0.000,"homeLoadTodayEnergyStr":"kWh","isS5":0,"batteryModel":1,"bypassAcOnoffSet":0.0,"parallelOnoff01":0.000,"parallelOnoff02":0.000,"rfState":1,"etoday":2.300,"etotal":15.467,"psum":0.000,"psumCal":1.420,"etoday1":2.300,"etotal1":15467.000,"offlineLongStr":"--","psumStr":"kW","psumCalStr":"kW","etotalStr":"MWh","etodayStr":"kWh"}],"total":2,"size":10,"current":1,"orders":[],"optimizeCountSql":false,"searchCount":true,"pages":1},"mpptSwitch":0}}
Never mind. added following:
resp = table.concat(resp)
resp = json.pdecode(resp)