GoodWe: Get data from Semportal

Moderator: leecollings

Post Reply
CrazyFrog
Posts: 21
Joined: Sunday 21 April 2019 13:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

GoodWe: Get data from Semportal

Post by CrazyFrog »

I have update my lua script to extract the data from the new "Goodwe semsportal" and pass it on to Domoticz.

Questions or tips to improve the script, I'd love to hear them

Code: Select all

-- Created by Raymond Wiertz
-- DateTime: 2020-02-13

-- 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;

local UserName = ""
local Password = ""
local token = ""

local refresh_time = 180 -- secondens
local sensorSolarPanels = "solar panels" -- name of your 

-- get login token
local loginCommand = string.format("curl --silent --output /dev/null --cookie-jar - -c cookie.txt -d 'account=%s&pwd=%s&code=' https://www.semsportal.com/Home/Login",UserName,Password)
-- get data from site
local getDataCommand = string.format("curl -b cookie.txt -d 'str={\"api\":\"v1/PowerStation/GetMonitorDetailByPowerstationId\",\"param\":{\"powerStationId\":\"%s\"}}&s=v1/PowerStation/GetMonitorDetailByPowerstationId' 'https://www.semsportal.com//GopsApi/Post?s=v1/PowerStation/GetMonitorDetailByPowerstationId'",token)

commandArray = {}
i=1

function isempty(s)
  return s == nil or s == ''
end

function getExecuteCommand(command)
  local f = assert(io.popen(command, 'r'))
  s = assert(f:read('*a'))
  f:close()
  return s
end

function stripchars(str, chrs)
    local s = str:gsub("["..chrs:gsub("%W","%%%1").."]", '')
    return s
end

function getValue(value, searchString)
   local response = ""

   searchString = string.format("%q", searchString)
   dummy, startPos = string.find(value, searchString )
   if not(startPos == nil) then
      dummy, startPos = string.find(value, searchString )
      dummy, endPos = string.find(value, ",", startPos)
      response = string.sub(value, startPos, endPos-1)
      response = string.gsub(response, ":", "")
      response = string.gsub(response, "\"", "")
      response = string.gsub(response, " ", "")
   end

   return response
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


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

difference = timedifference(otherdevices_lastupdate[sensorSolarPanels])

if (difference > refresh_time) then

    getExecuteCommand(loginCommand)
    local data = getExecuteCommand(getDataCommand)

--print(data)

    if (isempty(data) == false) then
        output_power = getValue(data, "output_power")
        last_refresh_time = getValue(data, "last_refresh_time")
        eDay = getValue(data, "eDay")

        print("last_refresh_time :" .. last_refresh_time)
        print("output_power :" .. output_power)
        print("eDay :" .. eDay)

        current = tonumber(stripchars(output_power, "kwhKWH"))
        dayTotal = tonumber(eDay)*1000

        UpdateDevice(sensorSolarPanels, current..";"..dayTotal)
    end
end

return commandArray

hopefully the script will continue to work in the near future.

hopefully the script will continue to work in the near future.

Have fun with it.
Sjuul
Posts: 4
Joined: Tuesday 22 October 2019 19:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: GoodWe: Get data from Semportal

Post by Sjuul »

Hello,

This script isn't work on my Raspberry PI 4.

The error message is:
2021-09-13 20:29:00.536 Error: EventSystem: in Goodwe solar SEMS 2020: [string "-- Created by Raymond Wiertz ..."]:58: bad argument #1 to 'sub' (string expected, got nil)

Is the someone how have this working in a LUA script?

Regards Sjuul.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests