[SOLVED] Transform and Split

  • Platform information: Vmware
    • Hardware: VmWare on Itel Nuc/Ram:4GB/HD: 100GB
    • OS: Centos 6.0
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_121-b13)
    • openHAB version: 1.8.3

Hi All,
I’m having a problem with Transform using the split function.
I’m receiving data from my fronius inverter using this rule:

rule Fronius_rule
when

	 Time cron "1 * 5-19 ? * *"
then
	if (Elevation.state > 0) 
	{
		
		var String json = sendHttpGetRequest("http://192.168.1.10/solar_api /v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CumulationInverterData").toString
    	var String myDE= transform("JS", "GetFroniusValueDE.js", json)
    	if  (myDE=='' || myDE==Uninitialized)
		{
			postUpdate(num_fronius_DE,"0")
		}
		else
		{
			postUpdate(num_fronius_DE,myDE)
		}
			
		
	}
	else
	
	{ 
			postUpdate(num_fronius_DE,"0")
	}
end

and this transformation script:

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

and it works well. But I need to take the INT value of the DAY_ENERGY value, so I modified the js in this manner:

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

But it does not work, returning an error in the log that simply reports the string received from fronius unparsed.
Someone can help me? Thanks in advance,

Post a sample raw data. Also, JSON.parse does not return null if it’s unable to. It just throws an exception. So if your raw data is incorrect (invalid JSON format), or any of the children of parsedData is null/undefined or not a complex object, you’ll get the same exception.

If you just wanna convert the string to a number, just add a plus sign:

return +arrayOfStrings[0];

Hi,
Thanks for the reply!
This is what is logged:

2018-02-08 16:14:01.811 [INFO ] [runtime.busevents             ] - num_fronius_DE state updated to {
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 14001
         },
         "DeviceStatus" : {
            "ErrorCode" : 0,
            "LEDColor" : 2,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : -1,
            "StateToReset" : false,
            "StatusCode" : 7
         },
         "PAC" : {
            "Unit" : "W",
            "Value" : 205
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 7977840
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 431171.40999999997
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CumulationInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2018-02-08T16:13:58+01:00"
   }
}

That is exactly the raw data obtained from the http command in a browser.
The RAW data is correct because, if I use the first function on it, as in the example, it works perfect. The only difference is in the split function that I think I’m using in a not correct mode.
If I use return +arrayOfStrings[0]; there is no difference.

I beleive that the JS transform returns the original data if there’s an error. When I tested your script in a browser i got an error:

Uncaught TypeError: Temp.split is not a function
    at test (test.html:10)
    at test.html:62

The reason is that temp is a number, and split is meant to be used on strings. But since it already is a number, tou can just use Temp.toFixed(0) to round it off if that’s what you’re after.

@pacive
Thank you very much for you reply!
Now the inverter is off cause it’s night, so I will try it tomorrow morning.

It works perfect! Thank you very much!!