HTTP Binding - JSON missing value

Dear all!

I’m receiving the data from my fronius solar inverter. Everything works fine until the sun is down. The device API doesn’t report the power value anymore, so I get errors in the log and sometimes the power value is stuck (last reported value of several watts). Is there anyway to avoid the errors or what’s in your opinion the best way for a workaround?

2017-08-14 01:02:44.019 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation 'JS(GetFroniusValuePAC.js)' threw an exception. [response={

   "Body" : {

      "Data" : {

         "DAY_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 0

         },

         "DeviceStatus" : {

            "ErrorCode" : 306,

            "LEDColor" : 1,

            "LEDState" : 0,

            "MgmtTimerRemainingTime" : 23,

            "StateToReset" : false,

            "StatusCode" : 3

         },

         "TOTAL_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 17429430

         },

         "YEAR_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 4737099

         }

      }

   },

   "Head" : {

      "RequestArguments" : {

         "DataCollection" : "CumulationInverterData",

         "DeviceClass" : "Inverter",

         "DeviceId" : "1",

         "Scope" : "Device"

      },

      "Status" : {

         "Code" : 0,

         "Reason" : "",

         "UserMessage" : ""

      },

      "Timestamp" : "2017-08-14T01:02:43+02:00"

   }

}

]

org.openhab.core.transform.TransformationException: An error occurred while executing script.

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[191:org.openhab.core.compat1x:2.1.0]

	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[188:org.openhab.binding.http:1.10.0]

	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[191:org.openhab.core.compat1x:2.1.0]

	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[191:org.openhab.core.compat1x:2.1.0]

==> /var/log/openhab2/events.log <==


Regards,
Herbert

Perhaps you should just fix the script that’s throwing the exception.

It’s not clear to me from this whether the JavaScript file itself is failing or the HttpBinding is. Need to see your configs and logs.

Hi Herbert,

could you be so kind and post your GetFroniusValuePAC.js file? I try to look into this later this day.

Thank you for your quick replies!

Configs:

Item

Number 	num_fronius_Power		"akt. Leistung [%.0f W]" 	<inverter>	(gFronius)	{http="<[http://192.168.1.135/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CumulationInverterData:5000:JS(GetFroniusValuePAC.js)]" }

GetFroniusValuePAC.js

JSON.parse(input).Body.Data.PAC.Value;

As said - it’s working perfectly as long as the pv-plant produces power. When it stops producing it doesn’t report PAC anymore, that’s why I’ve got the errors in the log. The problem is, that the last reported value which was acknowledegde from OpenHAB is >0 W, so I have PV-Production (at least according to OpenHAB) during night :wink:

If there’s no solution in the binding itself, I guess I have to design a rule (probably depending on constant energy values), to ensure num_fronius_Power is 0.

You should be able to just change the javascript to check if the PAC object exists.

If it exists, return its value. If not, then return a signal value (eg. -1).

Dear @namraccr - could you please point me in the right direction? I am afraid to say, but after one hour of searching and reading I must admit, I don’t get my JS-knowledge as fast as necessary. The check of the javascript can be done in the .js file directly - right?

You could replace your existing script with this.

var parsedData = JSON.parse(input);
if( parsedData.Body.Data.PAC == null) {
  return -1;
}
else {
  return parsedData.Body.Data.PAC.Value;
}

Dear @namraccr!

Thank you for your efforts and sorry for my late reply. I put the code in the transformation file, without luck:

2017-08-23 21:34:23.292 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation 'JS(GetFroniusValuePAC.js)' threw an exception. [response={

   "Body" : {

      "Data" : {

         "DAY_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 20406

         },

         "DeviceStatus" : {

            "ErrorCode" : 306,

            "LEDColor" : 1,

            "LEDState" : 0,

            "MgmtTimerRemainingTime" : 23,

            "StateToReset" : false,

            "StatusCode" : 3

         },

         "TOTAL_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 17676220

         },

         "YEAR_ENERGY" : {

            "Unit" : "Wh",

            "Value" : 4983892

         }

      }

   },

   "Head" : {

      "RequestArguments" : {

         "DataCollection" : "CumulationInverterData",

         "DeviceClass" : "Inverter",

         "DeviceId" : "1",

         "Scope" : "Device"

      },

      "Status" : {

         "Code" : 0,

         "Reason" : "",

         "UserMessage" : ""

      },

      "Timestamp" : "2017-08-23T21:34:22+02:00"

   }

}

]

org.openhab.core.transform.TransformationException: An error occurred while executing script.

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[191:org.openhab.core.compat1x:2.1.0]

	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[188:org.openhab.binding.http:1.10.0]

	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[191:org.openhab.core.compat1x:2.1.0]

	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[191:org.openhab.core.compat1x:2.1.0]

Any further ideas (I restarted openhab service)

I suggest 2 things.

  1. post the content of your JS file
  2. drop the binding into TRACE mode and capture the log; look at the messages from the binding surrounding the error

sorry for taking so much time to reply - I don’t have regular access to the system atm.

Here the Log and the file (changed from .js to .xml due to upload restrictions)GetFroniusValuePAC.xml (146 Bytes)

  • hope it helps!

Thx for your efforts!

    22:23:14.537 [DEBUG] [hab.binding.http.internal.HttpBinding] - item 'num_fronius_Power' is about to be refreshed now
22:23:17.014 [WARN ] [hab.binding.http.internal.HttpBinding] - Transformation 'JS(GetFroniusValuePAC.js)' threw an exception. [response={
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 14939
         },
         "DeviceStatus" : {
            "ErrorCode" : 306,
            "LEDColor" : 1,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : 13,
            "StateToReset" : false,
            "StatusCode" : 3
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 17967010
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 5274685
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CumulationInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2017-09-06T22:23:15+02:00"
   }
}
]
org.openhab.core.transform.TransformationException: An error occurred while executing script.
        at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [192:org.openhab.core.compat1x:2.2.0.201709032148]
        at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [189:org.openhab.binding.http:1.11.0.201709060110]
        at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157) [192:org.openhab.core.compat1x:2.2.0.201709032148]
        at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169) [192:org.openhab.core.compat1x:2.2.0.201709032148]
22:23:17.020 [DEBUG] [hab.binding.http.internal.HttpBinding] - transformed response is '{
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 14939
         },
         "DeviceStatus" : {
            "ErrorCode" : 306,
            "LEDColor" : 1,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : 13,
            "StateToReset" : false,
            "StatusCode" : 3
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 17967010
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 5274685
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CumulationInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2017-09-06T22:23:15+02:00"
   }
}
'
22:23:17.024 [DEBUG] [hab.binding.http.internal.HttpBinding] - Couldn't create state for item 'num_fronius_Power' from string '{
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 14939
         },
         "DeviceStatus" : {
            "ErrorCode" : 306,
            "LEDColor" : 1,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : 13,
            "StateToReset" : false,
            "StatusCode" : 3
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 17967010
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 5274685
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CumulationInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2017-09-06T22:23:15+02:00"
   }
}
'

Hi,
I don’t know if you solved your problem, cause of this post is old, however, try to change the GetFroniusValuePAC.js with this:

(function(myPAC) {
var parsedData = JSON.parse(myPAC);
if( parsedData.Body.Data.PAC != null) {
return parsedData.Body.Data.PAC.Value;
}
else {
return ‘’;
}
})(input)

Regards,

Dear Nicola!

Tried your changes - that’s the current log:
`018-01-04 19:40:19.046 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation ‘JS(GetFroniusValuePAC.js)’ threw an exception. [response=<?xml version="1.0" encoding="iso-8859-1"?>

404 - Not Found

404 - Not Found

] org.openhab.core.transform.TransformationException: An error occurred while executing script. at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [217:org.openhab.core.compat1x:2.2.0.201712081653] at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [214:org.openhab.binding.http:1.11.0.201712071817] at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [217:org.openhab.core.compat1x:2.2.0.201712081653] at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [217:org.openhab.core.compat1x:2.2.0.201712081653] `

Should it be different? I must admit I had no time to take a closer look, since I had issues collecting data since the latest FW-Update of the Fronius inverter.

Regards,
Herbert

Dear Nicola!

Indeed the firmware update caused some changes (instead of “solarplant” in the url I now have to use “inverter”

But it doesn’t change the problem, that the inverter does not send a value for current power production at night and therefor causes an error in the logs:

2018-01-04 20:08:39.426 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JS(GetFroniusValuePAC.js)' threw an exception. [response={
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 4277
         },
         "DeviceStatus" : {
            "ErrorCode" : 306,
            "LEDColor" : 1,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : 3,
            "StateToReset" : false,
            "StatusCode" : 3
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 19198910
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 19089
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CumulationInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2018-01-04T20:08:39+01:00"
   }
}
]
org.openhab.core.transform.TransformationException: An error occurred while executing script.
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [217:org.openhab.core.compat1x:2.2.0.201712081653]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [214:org.openhab.binding.http:1.11.0.201712071817]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [217:org.openhab.core.compat1x:2.2.0.201712081653]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [217:org.openhab.core.compat1x:2.2.0.201712081653]

Regards,
Herbert

For those who are interested: new URLs must look like below - sorry, do not have access the Firmware version of the inverter right now

Number 	num_fronius_TotEnergy	"Gesamterzeugung [%.0f Wh]" 	<solarplant>	(gFronius)	{http="<[http://192.168.1.135/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CumulationInverterData:60000:JS(GetFroniusValueTotal.js)]" }

Hi,
I have this in Fronius.rules:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.* 
import org.openhab.model.script.actions.*
import org.openhab.core.types.Command 
import org.openhab.core.types.*
import org.openhab.core.items.* 
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.lang.Math
import org.openhab.core.transform.*
import org.openhab.core.transform.TransformationService
import java.lang.String.*
import java.lang.Integer
import java.lang.Double
import org.openhab.core.persistence.*

rule Fronius_rule
when
	// Ogni minuto
	Time cron "1 * 5-22 ? * *"
then
	if (Elevation.state > 0) 
	{
		
	var String json = sendHttpGetRequest("http://192.168.2.104/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CumulationInverterData").toString
    	var String myVC= transform("JS", "GetFroniusValuePAC.js", json)
    	var String myDE= transform("JS", "GetFroniusValueDE.js", json)
    	var String myYEAR= transform("JS", "GetFroniusValueYEAR.js", json)
    	if  (myVC=='' || myVC==Uninitialized)
    	{ 
			postUpdate(num_fronius_Power,"0")
		}
    	else
		{ 
			postUpdate(num_fronius_Power,myVC)
		}
		if  (myDE=='' || myDE==Uninitialized)
		{
			postUpdate(num_fronius_DE,"0")
		}
		else
		{
			postUpdate(num_fronius_DE,myDE)
		}
		if (myYEAR=='' || myYEAR==Uninitialized)
		{
			postUpdate(num_fronius_YEAR_ENERGY,"0")
		}
		else
		{
			postUpdate(num_fronius_YEAR_ENERGY,myYEAR)	
			
		}
	}
	else
	
	{ 
			postUpdate(num_fronius_Power,"0")
	}
end

This in GetFroniusValueDe.js


(function(mystring) {
        var parsedData = JSON.parse(mystring);
        if( parsedData.Body.Data.DAY_ENERGY != null) {
        		var Temp=parsedData.Body.Data.DAY_ENERGY.Value;
                return Temp.toFixed(0);
             }
        else {
                return '0';
             }
})(input)

This in GetFroiusValuePAC.js

(function(myPAC) {
        var parsedData = JSON.parse(myPAC);
        if( parsedData.Body.Data.PAC != null) {
        		Temp=parsedData.Body.Data.PAC.Value;
                return Temp.toFixed(0);
             }
        else {
                return '0';
             }
})(input)

And this in GetFroniusValueYEAR

(function(myYEAR) {
        var parsedData = JSON.parse(myYEAR);
        if( parsedData.Body.Data.YEAR_ENERGY != null) {
        		var Temp = parsedData.Body.Data.YEAR_ENERGY.Value/1000
                return Temp.toFixed(0);
             }
        else {
                return '0';
             }
})(input)

And it works perfect!