I hope this helps you.
Questions or tips to improve the script, I'd love to hear them
Code: Select all
--- Created by Raymond Wiertz
--- DateTime: 2019-04-21
-- Note: The GoodWe data is refreshed every 60 seconds.
-- Note: Enter your own data at [LOGINNAME] and [PASSWORD]
-- Tracking your token
-- To find out your token, you must execute the CURL command below from the command line.
--- curl -d 'account=[LOGINNAME]&pwd=[PASSWORD]&code=' https://www.semsportal.com/Home/Login;
-- Place here your token
token = "[YOUR TOKEN]"
-- The curl command below retrieves the get session ID named "ASP.NET_SessionId", the session ID is used to request the final data
command = "curl --silent --output /dev/null --cookie-jar - -d 'account=[LOGINNAME]&pwd=[PASSWORD]&code=' https://www.semsportal.com/Home/Login"
commandArray = {}
i=1
function UpdateDevice(device, data)
idx = otherdevices_idx[device]
if (idx == nil) then
print('** Unknown device'..device)
else
commandArray[i] = {['UpdateDevice'] = idx..'|0|'..data}
i = i+1
end
end
function stripchars(str, chrs)
local s = str:gsub("["..chrs:gsub("%W","%%%1").."]", '')
return s
end
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function getValue(s)
firstPos, lastPos = string.find(s, ":")
value = string.sub(s, lastPos+1, string.len(s))
return value
end
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes,sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
-- Sensor declaration
sensorSolarPanels = "solar panels"
difference = timedifference(otherdevices_lastupdate[sensorSolarPanels])
if (difference > 60) then
local f = assert(io.popen(command, 'r'))
s = assert(f:read('*a'))
f:close()
-- extract the ASP.NET_SessionId from the curl response
sessionId = string.sub(s, -26)
sessionId = string.gsub(sessionId,"[\r\n]", "" )
sessionId = string.gsub(sessionId, "%s+", "")
-- update curl command with the session id.
getData = string.format("%s%s%s%s" , "curl -v --cookie 'ASP.NET_SessionId=", sessionId, "' https://www.semsportal.com/PowerStation/PowerStatusSnMin/",token )
local f = assert(io.popen(getData, 'r'))
s = assert(f:read('*a'))
f:close()
-- extract the JSON-data from the response
dummy, startPos = string.find(s, "var pw_info = ")
dummy, endPos = string.find(s, "}};")
s = string.sub(s, startPos, endPos-1)
-- remove excess brackets
valueString = stripchars(s, "[]{}\"")
-- and split the records
splitString = split(valueString, ",")
-- for debugging only
-- shows all values and the corresponding record id
--- for key, value in pairs(splitString) do
--- print(key..'='..value)
--- end
output_power = getValue(splitString[182])
last_refresh_time = getValue(splitString[188])
eDay = getValue(splitString[199])
-- daily_generation = getValue(splitString[357])
-- work_mode = getValue(splitString[189])
-- warning = getValue(splitString[194])
print("last_refresh_time :" .. last_refresh_time)
print("output_power :" .. output_power)
print("eDay :" .. eDay)
--- print("daily_generation :" .. daily_generation )
--- print("work_mode :" .. work_mode)
--- print("warning :" .. warning)
current = tonumber(stripchars(output_power, "kwhKWH"))
dayTotal = tonumber(eDay)*1000
UpdateDevice(sensorSolarPanels, current..";"..dayTotal)
end
return commandArray
Have fun.