Author Topic: Fronius Solar inverter integration  (Read 573 times)

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Fronius Solar inverter integration
« on: April 19, 2017, 01:51:26 am »
Hello

I have started to integrate my Fronius solar inverter to Vera edge (UI7).

I have changed the file from user reneboer (http://forum.micasaverde.com/index.php?topic=40611.0)
uploaded it, and created a new device as described in the post.

However, i do not get it to work properly.
 - The device says "it is not configured"
 - I can not see any log (ALTUI) for the device
 - Opening the uploaded xml file in the GUI (apps->develop app->luup files) says that there is a error on line 122 and that seems to be the "&" sign.
 - Testing the actual line of code in random lua test bench works fine.
 - Actual new code that i have changed (reading from the inverter and changed json conversion) working flawlessy.

Any suggestions on how to proceed to get it work?

The fronius API is described here:
http://www.fronius.com/cps/rde/xbcr/SID-A761BF1D-F8F8FEE6/fronius_international/42_0410_2012_318486_snapshot.pdf

The changed xml file is attached.

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #1 on: April 19, 2017, 02:16:30 pm »
Well... Changed the request to the server with this line instead excluding the "&" sign that seemed to be the issue in the file.  "GetInverterRealtimeData.cgi?Scope=System"

Now the module seems to start at least.

Offline reneboer

  • Hero Member
  • *****
  • Posts: 1104
  • Karma: +59/-30
Re: Fronius Solar inverter integration
« Reply #2 on: April 20, 2017, 05:16:55 am »
Hi,

The Vera does not like some HTML specific characters in the XML files even when they are within strings. You will have to replace them with the HTML equivalent.

Two typical ones are & and <
   & becomes &amp;
   < becomes &lt;

Give that a shot.

Note that is you would write the same code in a .lua file, you must use the original characters.


Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #3 on: April 22, 2017, 02:58:11 pm »
Thanks! Yes, changed the syntax and the file looks ok in the Vera viewer.  ;D

The files is uploaded, device created, but i do not see anything in the logs. I am very new for the LUA files but as i have understood the file shall depending on day/night update the values automatically (no need for external calls?). The log is not showing anything also when i changed the log level to 10.

The variables for the device are created according to the definition in the file.

Maybe i have misunderstood something fundamental with the vera plugin concept?


Offline reneboer

  • Hero Member
  • *****
  • Posts: 1104
  • Karma: +59/-30
Re: Fronius Solar inverter integration
« Reply #4 on: April 24, 2017, 05:05:09 am »
Hi,

Try reloading luup and at the same time keep an eye on the log file to see if anything on your device shows. It should at a minimum report that it is starting and the IP address it will be using.

If it still says not configured, make sure the IP attribute in the Advances tab is populated with the IP address of your solar device.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #5 on: April 29, 2017, 04:34:29 pm »
Hello

Yes it do seems to start... And now there is something with the decoding that i trying to understand.

03      04/29/17 22:27:49.433   luup_log:308: Fronius: Starting Fronius Device <0x77163520>
50      04/29/17 22:27:49.434   luup_log:308: Fronius: Using Fronius IP address 192.168.1.60 <0x77163520>
50      04/29/17 22:27:49.434   luup_log:308: Fronius: Fronius API URL http://192.168.1.60/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System <0x77163520>
50      04/29/17 22:27:49.434   luup_log:308: Fronius: Fronius: Fronius_Startup completed  <0x77163520>
50      04/29/17 22:27:59.790   luup_log:308: Fronius: Retrieve HTTPget Complete... <0x74363520>
50      04/29/17 22:27:59.791   luup_log:308: Fronius: {
01      04/29/17 22:27:59.795   LuaInterface::CallFunction_Timer-5 function Fronius_Retrieve failed [string "..."]:140: attempt to concatenate field '?' (a nil value) <0x74363520>

Should i expect that the raw string (containing the http call undecoded json) shall be visible as plain text in the ssh log?

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 752
  • Karma: +40/-7
Re: Fronius Solar inverter integration
« Reply #6 on: April 29, 2017, 05:40:44 pm »
Quote
attempt to concatenate field '?' (a nil value)
Happens all the time. To see more detail on the error, transform the variable into a string first. You can then see if you have a nil, number, table, boolean, etc. eg:

Code: [Select]
"WattsHour Today --> "..tostring(retData.DAY_ENERGY).."  --> Wh"
However after looking at the Fronius PDF, it looks like you need something like this, noting I haven't read all 52 pages of the manual: :D

Code: [Select]
"WattsHour Today --> "..tostring(retData.Data.DAY_ENERGY.Value).."  --> "..retData.Data.DAY_ENERGY.Unit
or perhaps:

Code: [Select]
"WattsHour Today --> "..tostring(retData.DAY_ENERGY.Value).."  --> "..retData.DAY_ENERGY.Unit




Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #7 on: April 30, 2017, 03:33:37 pm »
Thanks for all help!

Having trouble to access to integer part of the decoded json string. See the raw data below.

The string part work flawless. @a-lurker.. Tried to convert to string but i got this output "table: 0x21c7718" so to me it seems that there is some issue to decode the string.

I read that "For example tables that contain both string keys and an array part cannot be exactly represented by JSON. You can solve this by putting your array data in an explicit subtable."

Link: "http://dkolf.de/src/dkjson-lua.fsl/home"

The code i am trying:
  local retData = json.decode(Fronius_Raw,1,nil,nil)
  log("Current Power --> "..tostring(retData.Body.Data.PAC.Values) ..retData.Body.Data.PAC.Unit)

Undecoded response from the inverter:

{
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "",
         "Scope" : "System"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2017-04-30T20:49:09+02:00"
   },
   "Body" : {
      "Data" : {
         "PAC" : {
            "Unit" : "W",
            "Values" : {
               "1" : 0
            }
         },
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Values" : {
               "1" : 11068
            }
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Values" : {
               "1" : 668142
            }
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Values" : {
               "1" : 2321790
            }
         }
      }
   }
}

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 752
  • Karma: +40/-7
Re: Fronius Solar inverter integration
« Reply #8 on: April 30, 2017, 06:37:28 pm »
Try this:
Code: [Select]
log("Current Power --> "..tostring(retData.Body.Data.PAC.Values[1]) ..retData.Body.Data.PAC.Unit)
And to avoid these problems:
Code: [Select]
Two typical ones are & and <
   & becomes &amp;
   < becomes &lt;

It helps to split the code out into a separate  I_*.Lua file  Results in a lot more clarity. Refer to any plugin that does this.
« Last Edit: April 30, 2017, 06:40:34 pm by a-lurker »

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #9 on: May 01, 2017, 12:48:21 am »
Hi.

Really tying to get it to work. However I am now realizing that lua debugging is very time consuming  :o In fact i am stuck in with basic json deconding. .. 

HElloo
Quote
Try this:
Code: [Select]
log("Current Power --> "..tostring(retData.Body.Data.PAC.Values[1]) ..retData.Body.Data.PAC.Unit)
Yes, i fully agree. Result: Nil Value.

Quote
And to avoid these problems:
Code: [Select]
Two typical ones are & and <
   & becomes &amp;
   < becomes &lt;
Tried in the original code to implement without using the correct format and when i changed (as suggested) the filed loaded. To get more and better information from the device I changed the request string and this new one do not use these characters. This is the code i am using to request and store the data. 
Code: [Select]
log("Using Fronius IP address " .. ipAddress)
--EData.URL = "http://" .. ipAddress .. "/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&amp;DeviceId=1&amp;DataCollection=CommonInverterData"
EData.URL = "http://" .. ipAddress .. "/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System"
log("Fronius API URL " .. EData.URL)
-- Schedule to start polling in 10 sec
luup.call_delay("Fronius_Retrieve",10)
log("Fronius: Fronius_Startup completed ")
setluupfailure(0, EData.DEVICE)
return true
end

-- Retrieve data from Fronius
function Fronius_Retrieve()
local HTTP_Code, Fronius_Raw = luup.inet.wget(EData.URL,5)

if (HTTP_Code == 0) then
log("Retrieve HTTPget Complete...")
log(Fronius_Raw)
local retData = json.decode(Fronius_Raw,1,nil,nil)
log("Current Power --> "..tostring(retData.Body.Data.PAC.Values[1]) ..retData.Body.Data.PAC.Unit)
-- log("WattsHour Today --> "..tostring(retData.Data.DAY_ENERGY.Values[1]) .."  --> Wh")
-- log("WattsHour Year --> "..tostring(retData.Data.YEAR_ENERGY.Values[1]) .."  --> Wh")
-- log("WattsHour Lifetime --> "..retData.Data.TOTAL_ENERGY.Values[1] .."  --> Wh")
-- Set values in PowerMeter
varSet("Watts", retData.Body.Data.PAC.Value)
-- varSet("KWH", tostring(retData.Data.DAY_ENERGY.Values[1]/1000)/1000)
-- varSet("DayKWH", tostring(retData.Data.DAY_ENERGY.Values[1]/1000))
-- varSet("YearKWH", tostring(retData.Data.YEAR_ENERGY.Values[1]/1000))
-- varSet("LifeKWH", tostring(retData.Data.TOTAL_ENERGY.Values[1]/1000))
-- log("Finished Call "..retData.wattsNow.." --> "..retData.DAY_ENERGY.." --> "..retData.wattHoursSevenDays.." --> "..retData.wattHoursLifetime)
-- log("Finished Call "..retData.DAY_ENERGY.Value.." --> "..retData.Body.Data.YEAR_ENERGY.Value.." --> "..retData.wattHoursLifetime)
retData = nil
else
varSet("Watts", 0)
varSet("Today", 0)
log("HTTPget failed.",2)
end

The result i am getting is as below:
Code: [Select]
03      05/01/17 6:41:45.553    luup_log:308: Fronius: Starting Fronius Device <0x76b53520>
50      05/01/17 6:41:45.553    luup_log:308: Fronius: Using Fronius IP address 192.168.1.60 <0x76b53520>
50      05/01/17 6:41:45.554    luup_log:308: Fronius: Fronius API URL http://192.168.1.60/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System <0x76b53520>
50      05/01/17 6:41:45.554    luup_log:308: Fronius: Fronius: Fronius_Startup completed  <0x76b53520>
50      05/01/17 6:41:55.850    luup_log:308: Fronius: Retrieve HTTPget Complete... <0x73d53520>
50      05/01/17 6:41:55.851    luup_log:308: Fronius: {
50      05/01/17 6:41:55.855    luup_log:308: Fronius: Current Power --> nilW <0x73d53520>
50      05/01/17 6:41:55.856    luup_log:308: Fronius: Is Day use Day delay Interval Fronius_Retrieve --> 30 <0x73d53520>

To me it looks like that the issue is when i am trying to access and convert the integer and/or decoding the json.

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 752
  • Karma: +40/-7
Re: Fronius Solar inverter integration
« Reply #10 on: May 01, 2017, 02:28:11 am »
Using the original URL:

Code: [Select]
luup.log("Current Power --> "..tostring(retData.Body.Data.PAC.Values['1']) ..retData.Body.Data.PAC.Unit)
luup.log("Current Power --> "..tostring(retData.Body.Data.YEAR_ENERGY.Values['1']) ..retData.Body.Data.YEAR_ENERGY.Unit)

Offline tmorberg

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Fronius Solar inverter integration
« Reply #11 on: May 01, 2017, 03:22:17 am »
Yes!!! That was the issue!  ;D

The result in the logs looks like this..  :) Now I will go on and test the rest :)

Code: [Select]
23528 root      1792 S    grep Fronius
03      05/01/17 9:16:41.252    luup_log:308: Fronius: Starting Fronius Device <0x76deb520>
50      05/01/17 9:16:41.253    luup_log:308: Fronius: Using Fronius IP address 192.168.1.60 <0x76deb520>
50      05/01/17 9:16:41.253    luup_log:308: Fronius: Fronius API URL http://192.168.1.60/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System <0x76deb520>
50      05/01/17 9:16:41.254    luup_log:308: Fronius: Fronius: Fronius_Startup completed  <0x76deb520>
50      05/01/17 9:16:51.702    luup_log:308: Fronius: Retrieve HTTPget Complete... <0x73feb520>
50      05/01/17 9:16:51.703    luup_log:308: Fronius: {
50      05/01/17 9:16:51.707    luup_log:308: Fronius: Current Power --> 2157W <0x73feb520>
50      05/01/17 9:16:51.707    luup_log:308: Fronius: WattsHour Today --> 3818Wh <0x73feb520>
50      05/01/17 9:16:51.707    luup_log:308: Fronius: WattsHour Year --> 672074Wh <0x73feb520>
50      05/01/17 9:16:51.707    luup_log:308: Fronius: WattsHour Lifetime --> 2325729Wh <0x73feb520>
50      05/01/17 9:16:51.712    luup_log:308: Fronius: Is Day use Day delay Interval Fronius_Retrieve --> 30 <0x73feb520>
50      05/01/17 9:17:21.772    luup_log:308: Fronius: Retrieve HTTPget Complete... <0x73feb520>
50      05/01/17 9:17:21.772    luup_log:308: Fronius: {
50      05/01/17 9:17:21.775    luup_log:308: Fronius: Current Power --> 2158W <0x73feb520>
50      05/01/17 9:17:21.776    luup_log:308: Fronius: WattsHour Today --> 3833Wh <0x73feb520>
50      05/01/17 9:17:21.776    luup_log:308: Fronius: WattsHour Year --> 672093Wh <0x73feb520>
50      05/01/17 9:17:21.776    luup_log:308: Fronius: WattsHour Lifetime --> 2325750Wh <0x73feb520>
50      05/01/17 9:17:21.780    luup_log:308: Fronius: Is Day use Day delay Interval Fronius_Retrieve --> 30 <0x73feb520>

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 752
  • Karma: +40/-7
Re: Fronius Solar inverter integration
« Reply #12 on: May 01, 2017, 03:45:16 am »
Good news - have go at splitting the  I_FroniusPower1.xml  into a  I_FroniusPower1.xml  and  L_FroniusPower1.lua  file. Makes all a lot clearer. Make sure everything that is local is declared as local.