To apply some of the lessons on that other thread here.
Since we know that calling split doesn’t actually change the original String, there really is no need to split the String over and over. Instead split the String only once and store that in a variable (I call it parts) are reuse the variable.
Next, to remove white space from the beginning and end of your String, there is a handy method called trim.
Finally, the String::format really isn’t doing anything for you in the Float::parseFloat calls. Since you are using %s this is the same as calling Float::parseFloat(LOAD.toString)
and since LOAD is already a String this is basically a noop.
There are some other minor concerns about using parseFloat here as well. parseFloat returns a primitive float
. Unfortunately, especially on an RPi, the use of primitives can greatly increase the amount of time it takes to load and parse .rules files. Because you are not actually doing anything with these variables that requires them to be a primitive float, it would be better to let it be a Number. By default the Rules DSL uses BigDecimal to represent numbers so let’s just use that.
So the above code would become:
logInfo("APC Info", "APC UPS Query update = "+APCUPSQuery_Output.state) // Change this Item to your "apcaccess -u" exec query output // Comment out this line to stop it populating your openhab.log file
var APCQUERY = APCUPSQuery_Output.state.toString.split(':') // Change this Item to your "apcaccess -u" exec query output
var APCname = APCQUERY.get(14).trim
var APCOnline = APCQUERY.get(15).trim
var linev = APCQUERY.get(16).trim
var LOAD = APCQUERY.get(17).trim
var loadNo = new BigDecimal(LOAD) // turns the string into a usable number
var batterych = APCQUERY.get(18).trim
var batterychNo = new BigDecimal(batterych) // turns the string into a usable number
var timeleft = APCQUERY.get(19).trim
var timeleftNo = new BigDecimal(timeleft) // turns the string into a usable number
var outputv = APCQUERY.get(23).trim
var outputvNo = new BigDecimal(outputv) // turns the string into a usable number
var itemp = APCQUERY.get(30).trim
var itempNo = new BigDecimal(itemp) // turns the string into a usable number
var battv = APCQUERY.get(32).trim
var battvNo = new BigDecimal(battv) // turns the string into a usable number
Not only is the above more efficient, which isn’t really that much of a concern here, but it avoids duplicated code which is a bigger deal. When ever you see a bunch of lines that look the same or groups of lines that look the same with only minor difference, it is usually worth while to try to centralize the duplicated code into one place. In this case, we centralized the split.
Theoretically you could also centralize the trims with something like
var APCQUERY = APCUPSQuery_Output.state.toString.replace("\n", "").split(':') // Change this Item to your "apcaccess -u" exec query output
But I don’t know the String that is being parsed well enough to know if that would cause problems.
The part referencing parseFloat is not something I would expect the vast majority of OH users to know and understand before now. The only reason I know it is because of my extensive experience helping other users.