How to get SAJ Solar xml values? Topic is solved

Moderator: leecollings

Post Reply
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

How to get SAJ Solar xml values?

Post by BarryT »

Hi all!

I try to use this script to get data out of my (ethernet module of the) SAJ Solar inverter..
Forget to add the version:

Version: 4.11487
Build Hash: ac3d2290d
Compile Date: 2019-11-14 16:41:43
dzVents Version: 2.5.0
Python Version: 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516]

Code: Select all

commandArray = {}
lineNo = 0
now    = 0
temp   = 0
today  = 0
total  = 0
v1     = 0
v2     = 0
i1     = 0
i2     = 0
ip     = '192.168.1.18'
i = 1

-- Only run during day
-- if ( uservariables["Het is Donker"] == 0 ) then
    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

	--Get the XML data from the inverter
    local url = 'curl http://'..ip..'/real_time_data.xml'
    local data = assert(io.popen(url))

    for line in data:lines() do

        if (lineNo == 0 and string.find(line,'Normal') ~= nil) then
            lineNo = 1
        elseif (lineNo > 0) then
            if     (lineNo == 4) then now   = string.match(line, "%d+")
            elseif (lineNo == 5) then temp  = string.match(line, "%d+%.%d+")
            elseif (lineNo == 6) then today = string.match(line, "%d+%.%d+")
            elseif (lineNo == 8) then total = string.match(line, "%d+%.%d+")
            end
            lineNo = lineNo + 1
        end
    end

    data:close()

    if (lineNo >= 14) then
        -- Update the Domoticz GUI
        ep = v1*i1
        wp = v2*i2
        UpdateDevice('PV SAJ', now..";".. total*1000)
        UpdateDevice('Temperatuur SAJ', temp)
   end
return commandArray
The script seems to doesnt work at all, no errors but also no executing in the log.
I use the EventSystem, and also made a script by hand with script_time_saj.lua, but both seems to do nothing.

Someone know what is wrong?
Ps, i can only test the dummy updating (when its hopefully going to executed once) when it is daylight because the SAJ goes offline when it is dark, and so the ethernet module goes to..
saj.jpg
saj.jpg (222.46 KiB) Viewed 2159 times
thanks!
Last edited by BarryT on Thursday 14 November 2019 19:33, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by waaren »

BarryT wrote: Thursday 14 November 2019 18:05 I try to use this script to get data out of my (ethernet module of the) SAJ Solar inverter..
The script seems to doesnt work at all, no errors but also no executing in the log.
I use the EventSystem, and also made a script by hand with script_time_saj.lua, but both seems to do nothing.
assuming that the eventSystem is active and the script is saved as enabled with a time trigger, a start to debug the script would be to change

Code: Select all

for line in data:lines() do
to

Code: Select all

print(data) -- should show something like 'file (0x42e0f8)' 
for line in data:lines() do
print(line) -- should print all lines of the return of the url
could help
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by BarryT »

waaren wrote: Thursday 14 November 2019 19:29
BarryT wrote: Thursday 14 November 2019 18:05 I try to use this script to get data out of my (ethernet module of the) SAJ Solar inverter..
The script seems to doesnt work at all, no errors but also no executing in the log.
I use the EventSystem, and also made a script by hand with script_time_saj.lua, but both seems to do nothing.
assuming that the eventSystem is active and the script is saved as enabled with a time trigger, a start to debug the script would be to change

Code: Select all

for line in data:lines() do
to

Code: Select all

print(data) -- should show something like 'file (0x42e0f8)' 
for line in data:lines() do
print(line) -- should print all lines of the return of the url
could help
Thanks waaren, but the script isnt function at all. :(
Eventsystem is active and ofcourse a time script used.
there is no logging who said lua..Status or triggered like all other scripts.

Example of other scripts wich working correctly:
2019-11-14 18:47:28.728 Status: RFLink: 2 level: 7
2019-11-14 18:48:29.246 Status: LUA: Lampen in de badkamer AAN
2019-11-14 18:48:29.350 Status: RFLink: 2 level: 7
2019-11-14 18:49:04.253 Status: LUA: Lamp Overloop AAN
2019-11-14 18:51:03.455 Status: LUA: Lampen Garage UIT, 2 minuten geen beweging
2019-11-14 18:51:03.456 Status: EventSystem: Script event triggered: GarageLampenUIT

Also did a reboot and change the lua to device but that didnt do anything.
Also made a fault, then the lua gives an error, so it looks like it IS reading the script but not executing?
Is it possible that Domoticz had changed the eventsystem / lua script readings?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by waaren »

BarryT wrote: Thursday 14 November 2019 19:37 Is it possible that Domoticz had changed the eventsystem / lua script readings?
Not that I am aware.

If I test your script (after changing part of it)

from

Code: Select all

	--Get the XML data from the inverter
    local url = 'curl http://'..ip..'/real_time_data.xml'
    local data = assert(io.popen(url))

    for line in data:lines() do

to

Code: Select all

	--Get the XML data from the inverter
    local url = 'curl http://192.168.192.109:8084/json.htm?type=command&param=getversion'
    local data = assert(io.popen(url))
    
    print(tostring(data))
    for line in data:lines() do
        print(line)
the result is

Code: Select all

2019-11-14 20:35:18.298  Status: LUA: file (0x20c93b0)
2019-11-14 20:35:18.324  Status: LUA: {
2019-11-14 20:35:18.324  Status: LUA:    "status" : "ERR"
2019-11-14 20:35:18.324  Status: LUA: }
When changing to

Code: Select all

	--Get the XML data from the inverter
    local url = 'curl "http://192.168.192.109:8084/json.htm?type=command&param=getversion" '
    local data = assert(io.popen(url))
    
    print(tostring(data))
    for line in data:lines() do
        print(line)
the result is

Code: Select all

2019-11-14 20:37:28.376  Status: LUA: file (0xf0994898)
2019-11-14 20:37:28.402  Status: LUA: {
2019-11-14 20:37:28.402  Status: LUA:    "DomoticzUpdateURL" : "https://www.domoticz.com/download.php?channel=stable&type=release&system=linux&machine=aarch64",
2019-11-14 20:37:28.402  Status: LUA:    "HaveUpdate" : false,
2019-11-14 20:37:28.402  Status: LUA:    "Revision" : 10717,
2019-11-14 20:37:28.402  Status: LUA:    "SystemName" : "linux",
2019-11-14 20:37:28.402  Status: LUA:    "UseUpdate" : true,
2019-11-14 20:37:28.402  Status: LUA:    "build_time" : "2019-11-08 17:59:05",
2019-11-14 20:37:28.402  Status: LUA:    "dzvents_version" : "2.5.0",
2019-11-14 20:37:28.402  Status: LUA:    "hash" : "5c5f579c3",
2019-11-14 20:37:28.402  Status: LUA:    "python_version" : "3.7.3 (default, Apr  3 2019, 05:39:12) \n[GCC 8.2.0]",
2019-11-14 20:37:28.402  Status: LUA:    "status" : "OK",
2019-11-14 20:37:28.402  Status: LUA:    "title" : "GetVersion",
2019-11-14 20:37:28.402  Status: LUA:    "version" : "4.11476"
2019-11-14 20:37:28.402  Status: LUA: }
So one conclusion: the double quotes around the url do matter..
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by BarryT »

waaren wrote: Thursday 14 November 2019 20:39
BarryT wrote: Thursday 14 November 2019 19:37 Is it possible that Domoticz had changed the eventsystem / lua script readings?
Not that I am aware.

If I test your script (after changing part of it)

from
to...
So one conclusion: the double quotes around the url do matter..
Wauw, hero! :-)
Well, there is something going on indeed:
2019-11-14 21:19:00.135 Status: LUA: file (0x72b3bbe0)

Code: Select all

commandArray = {}

        lineNo = 0
        now    = 0
        temp   = 0
        today  = 0
        total  = 0
        v1     = 0
        v2     = 0
        i1     = 0
        i2     = 0
        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

	--Get the XML data from the inverter
    local url = 'curl "http://192.168.1.18/real_time_data.xml" '
    local data = assert(io.popen(url))
    
    print(tostring(data))
    for line in data:lines() do
        print(line)

            if (lineNo == 0 and string.find(line,'Normal') ~= nil) then
                lineNo = 1
            elseif (lineNo > 0) then
                if     (lineNo == 4) then now   = string.match(line, "%d+")
                elseif (lineNo == 5) then temp  = string.match(line, "%d+%.%d+")
                elseif (lineNo == 6) then today = string.match(line, "%d+%.%d+")
                elseif (lineNo == 8) then total = string.match(line, "%d+%.%d+")
                end
                lineNo = lineNo + 1
            end
        end

        data:close()

        if (lineNo >= 14) then
            -- Update the Domoticz GUI
            ep = v1*i1
            wp = v2*i2
            UpdateDevice('PV SAJ', now..";".. total*1000)
            UpdateDevice('Temperatuur SAJ', temp)
        end
        
return commandArray
I guess i have to wait till tommorrow when its light?
I cannot fully check now because the ip is offline :(
Or.. is there a way (debug) to see if it try to open the curl "url" ?

Thanks so far :-)
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by BarryT »

Getting this now:

Code: Select all

2019-11-15 09:23:00.058 Status: LUA: file (0x6f502b68)
2019-11-15 09:23:00.136 Status: LUA: <?xml version="1.0" ?>
2019-11-15 09:23:00.136 Status: LUA: <real_time_data>
2019-11-15 09:23:00.136 Status: LUA: <state>Normal</state>
2019-11-15 09:23:00.136 Status: LUA: <Vac_l1>230.9</Vac_l1>
2019-11-15 09:23:00.136 Status: LUA: <Vac_l2>-</Vac_l2>
2019-11-15 09:23:00.136 Status: LUA: <Vac_l3>-</Vac_l3>
2019-11-15 09:23:00.136 Status: LUA: <Iac_l1>0.17</Iac_l1>
2019-11-15 09:23:00.136 Status: LUA: <Iac_l2>-</Iac_l2>
2019-11-15 09:23:00.137 Status: LUA: <Iac_l3>-</Iac_l3>
2019-11-15 09:23:00.137 Status: LUA: <Freq1>49.99</Freq1>
2019-11-15 09:23:00.137 Status: LUA: <Freq2>-</Freq2>
2019-11-15 09:23:00.137 Status: LUA: <Freq3>-</Freq3>
2019-11-15 09:23:00.137 Status: LUA: <pac1>38</pac1>
2019-11-15 09:23:00.137 Status: LUA: <pac2>-</pac2>
2019-11-15 09:23:00.137 Status: LUA: <pac3>-</pac3>
2019-11-15 09:23:00.137 Status: LUA: <p-ac>38</p-ac>
2019-11-15 09:23:00.137 Status: LUA: <temp>7.0</temp>
2019-11-15 09:23:00.137 Status: LUA: <e-today>0.01</e-today>
2019-11-15 09:23:00.137 Status: LUA: <t-today>1.0</t-today>
2019-11-15 09:23:00.137 Status: LUA: <e-total>2293.08</e-total>
2019-11-15 09:23:00.137 Status: LUA: <CO2>2286.20</CO2>
2019-11-15 09:23:00.137 Status: LUA: <t-total>6559.9</t-total>
2019-11-15 09:23:00.137 Status: LUA: <v-pv1>146.6</v-pv1>
2019-11-15 09:23:00.137 Status: LUA: <v-pv2>0.0</v-pv2>
2019-11-15 09:23:00.137 Status: LUA: <v-pv3>-</v-pv3>
2019-11-15 09:23:00.137 Status: LUA: <v-bus>359.8</v-bus>
2019-11-15 09:23:00.138 Status: LUA: <maxPower>43</maxPower>
2019-11-15 09:23:00.138 Status: LUA: <i-pv11>0.27</i-pv11>
2019-11-15 09:23:00.138 Status: LUA: <i-pv12>-</i-pv12>
2019-11-15 09:23:00.138 Status: LUA: <i-pv13>-</i-pv13>
2019-11-15 09:23:00.138 Status: LUA: <i-pv14>-</i-pv14>
2019-11-15 09:23:00.138 Status: LUA: <i-pv21>-</i-pv21>
2019-11-15 09:23:00.138 Status: LUA: <i-pv22>-</i-pv22>
2019-11-15 09:23:00.138 Status: LUA: <i-pv23>-</i-pv23>
2019-11-15 09:23:00.138 Status: LUA: <i-pv24>-</i-pv24>
2019-11-15 09:23:00.138 Status: LUA: <i-pv31>-</i-pv31>
2019-11-15 09:23:00.138 Status: LUA: <i-pv32>-</i-pv32>
2019-11-15 09:23:00.138 Status: LUA: <i-pv33>-</i-pv33>
2019-11-15 09:23:00.138 Status: LUA: <i-pv34>-</i-pv34>
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by waaren »

BarryT wrote: Friday 15 November 2019 9:24 Getting this now:

Code: Select all

2019-11-15 09:23:00.058 Status: LUA: file (0x6f502b68)
2019-11-15 09:23:00.136 Status: LUA: <?xml version="1.0" ?>
2019-11-15 09:23:00.136 Status: LUA: <real_time_data>
2019-11-15 09:23:00.136 Status: LUA: <state>Normal</state>
...
This part looks about right.
I see two other (potential) problems in your script.
- The script assumes certain values on fixed line numbers. Even it would do the job now (which is doesn't) it will fail whenever something changes in the return.
- The script uses match on number to extract the values from the output but the XML tags also contain numbers. It will fail on some of the fields.

See this example:

Code: Select all

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> a = ' <Freq1>49.99</Freq1>'
> string.match(a, "%d+")
1 -- Not what was expected ?

line = '<Freq1>49.99</Freq1>'
> string.match(line, "%d+%.%d+")
49.99 -- Here it does the job
Maybe better to change to something that takes the tags into account.

This function takes a line and a tag as input and return the value between the tags.

Code: Select all

local function getValueFromTag(tagLine, tag)
    local _, beginValueString = tagLine:find(tag)
    local _, endValueString = tagLine:find('<', beginValueString)
    return tagLine:sub(beginValueString + 1, endValueString - 1)
end	
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by BarryT »

waaren wrote: Friday 15 November 2019 10:04
BarryT wrote: Friday 15 November 2019 9:24 Getting this now:

Code: Select all

2019-11-15 09:23:00.058 Status: LUA: file (0x6f502b68)
2019-11-15 09:23:00.136 Status: LUA: <?xml version="1.0" ?>
2019-11-15 09:23:00.136 Status: LUA: <real_time_data>
2019-11-15 09:23:00.136 Status: LUA: <state>Normal</state>
...
This part looks about right.
I see two other (potential) problems in your script.
- The script assumes certain values on fixed line numbers. Even it would do the job now (which is doesn't) it will fail whenever something changes in the return.
- The script uses match on number to extract the values from the output but the XML tags also contain numbers. It will fail on some of the fields.

See this example:

Code: Select all

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> a = ' <Freq1>49.99</Freq1>'
> string.match(a, "%d+")
1 -- Not what was expected ?

line = '<Freq1>49.99</Freq1>'
> string.match(line, "%d+%.%d+")
49.99 -- Here it does the job
Maybe better to change to something that takes the tags into account.

This function takes a line and a tag as input and return the value between the tags.

Code: Select all

local function getValueFromTag(tagLine, tag)
    local _, beginValueString = tagLine:find(tag)
    local _, endValueString = tagLine:find('<', beginValueString)
    return tagLine:sub(beginValueString + 1, endValueString - 1)
end	
Thanks man!
But what now?

Do i need a new script or...?
I wish lua was a bit easier to understand.. haha
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to get SAJ Solar xml values?

Post by BarryT »

I used another script, its working now!

Thanks for your help Waaren :-)

Code: Select all

commandArray = {}

local now    = 0
local bNowUpdated = false
local temp   = 0
local bTempUpdated = false
local today  = 0
local bTodayUpdated = false
local total  = 0
local bTotalUpdated = false
local ip     = '192.168.1.18'    --customize this to your invertor IP
local 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

	--Get the XML data from the inverter
    local url = 'curl http://'..ip..'/real_time_data.xml'
    local data = assert(io.popen(url))
    
    --print(string.format("http request from SAJ: %s", data))

    local LineNr = 0
--    print(tostring(data)) -- debugging
--    print(data) -- should show something like 'file (0x42e0f8)' 
        for line in data:lines() do
--    print(line) -- should print all lines of the return of the url
        LineNr = LineNr + 1

        if( string.find(line, 'temp') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            temp  = string.match(line, "%d+%.%d+")
            bTempUpdated = true

        elseif( string.find(line, 'e%-total') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            total  = string.match(line, "%d+%.%d+")
            bTotalUpdated = true

        elseif( string.find(line, 'e%-today') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            today  = string.match(line, "%d+%.%d+")
            bTodayUpdated = true

        elseif( string.find(line, 'p%-ac') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            now  = string.match(line, "%d+")
            bNowUpdated = true

        else
            --print(string.format("line %d: %s", LineNr, line))
        end

    end

    data:close()

    -- Update the Domoticz GUI
    if( bTempUpdated==true)
    then
        --print(string.format("updating SAJ temp:%4.1f",temp))
        UpdateDevice('Temperatuur SAJ', temp)                          --customize the temperature sensor name
    end
    
    if( bNowUpdated==true and bTotalUpdated==true)
    then
        --print(string.format("updating SAJ energy:%d %d",now, total*1000))
        UpdateDevice('PV SAJ', now..";".. total*1000)       --customize the energy sensor name
    end
    

--        local ep = v1*i1
--        local wp = v2*i2
		-- Upload the data to PVoutput every 10 mins
		-- date = os.date("*t")
		-- if (date.min % 10 == 0) then
		--     baseURL="http://pvoutput.org/service/r2/addstatus.jsp?"
		--     API="yourAPIkey"
		--     PVO_URL= baseURL .. "sid=36263&key=" .. API .. "&d=" .. os.date("%Y%m%d") .. "&t=" .. os.date("%H:%M")
		--     PVO_URL = PVO_URL .. "&v1=" .. (today*1000) .. "&v2=" .. now
		--     commandArray['OpenURL'] = PVO_URL
		-- end
return commandArray
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest