Can't calculate with var Number .... provided from script

Hello,
a shell script set a variable in oH, but oH will not use this value for calculations.

The value seems correct (i.e.): 5.3

Example Code (the calculation *3 is not the real calculation I willl use later):

   var Number RainForecastNextDay = executeCommandLine("/usr/local/mbin/get_rain_forcast_nextday.sh", 2000)
   logInfo("irrigation.rules", "Value **AAA** RainForecastNextDay: {}ENDE",RainForecastNextDay )
   RainForecastNextDay = RainForecastNextDay * 3
   logInfo("irrigation.rules", "Value BBB RainForecastNextDay: {}ENDE",RainForecastNextDay )

Logfile show, that the RainForecastNextDay is set to “5.3”, but the simple calculation in next line (* 3) went wrong:


2019-07-11 14:00:08.993 [INFO ] [rthome.model.script.irrigation.rules] - Value AAA RainForecastNextDay: 5.3ENDE
2019-07-11 14:00:08.998 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘automatic irrigation, calculate irrigation time …’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_multiply(java.lang.Number,java.lang.Number) on instance: null

If I set the RainForecastNextDay manually:
RainForecastNextDay = 5.3
it works (* 3 etc …) , no error.

How can I use the “value” from the script as a number?

The script returns a string. You’ll need to parse it.

var Number RainForecastNextDay = Float::parseFloat( executeCommandLine("/usr/local/mbin/get_rain_forcast_nextday.sh", 2000) ) as Number
1 Like

Thx, this is working.
Thought if I define the variable as a Number, uf will be a number. And if a string is coming (from the Shell-script), an error will be shown.

Not so easy in oH, … var Number float parse as-number …

Rules variables are loosely typed and easily bent into other shapes.
executeCommandLine() always returns a string (if anythng).
You usually have to be quite expilcit when parsing a string to a number because of different string formats and number types.