Clarification of javascript transformation: string vs number

I am on OH 2.5. And I would like to call a js transform out of a rule. The goal is to calculate the Air Quality Index from a PM2.5 and PM10 measurement. To this end I have two js functions that do the calculation. This is how one of the js functions looks like:

(function(c) {
        case (c > 425):
            var c_lo = 425;
            var c_hi = 604;
            var A_lo = 301;
            var A_hi = 500;
        case (c > 355):
            var c_lo = 355;
            var c_hi = 424;
            var A_lo = 201;
            var A_hi = 300;
        case (c > 255):
            var c_lo = 255;
            var c_hi = 354;
            var A_lo = 151;
            var A_hi = 200;
        case (c > 155):
            var c_lo = 155;
            var c_hi = 254;
            var A_lo = 101;
            var A_hi = 150;
        case (c > 55):
            var c_lo = 55;
            var c_hi = 154;
            var A_lo = 51;
            var A_hi = 100;
        case (c < 55):
            var c_lo = 0.0;
            var c_hi = 54;
            var A_lo = 0;
            var A_hi = 50;
    var AQI = (A_hi - A_lo) / (c_hi - c_lo) * (c - c_lo) + A_lo;
    return Math.round(AQI);

And this is my rule:

rule "calculate AQI"
        Item out_Airrohr_PM25 changed
        var AQI_10 = transform("JS", "pm10-aqi.js", out_Airrohr_PM10)
        var AQI_25 = transform("JS", "pm25-aqi.js", out_Airrohr_PM25)
        if(AQI_10 > AQI_25) out_Airrohr_AQI.postUpdate(AQI_10)
        else out_Airrohr_AQI.postUpdate(AQI_25)

I have used numerous variations of the rule, but I always get error messages, most often this one:
Could not invoke method: org.eclipse.smarthome.core.transform.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null
I have the feeling that this is a string/float issue. From the error message I read that the transform needs three strings as input, however in the (not very well documented) examples that I found, this did not appear to be required.

Could someone please clarify, what the correct input and output data type is for the transform? The out_Airrohr_AQI item that I want to set is a Number type.

Is this an Item? You can’t transform an Item. You can transform its .state. The state may need conversion .toString, depending.

Yes, everything gets passed in and out of transforms as strings.
You may need to parse an input numeric within the transform. You would certainly need to parse a returned string in a rule if you wanted to use it as numeric e.g.comparing.

Yes, it is an item. So I understand that for an item with a Number state, I have to pass out_Airrohr_PM10.state.toString to the transform. And inside the .js file for my transformation, I will have to first convert the string to a float, do my calculations and convert it back to string for returning it and in the rule I will again have to convert the string from the transformation to a Number before pushing it to my item?
I will try when I am back home.

Not for that reason, you can pass Strings to any Item.

But you want to do a numeric comparison, so you will have to parse out a number to do that.

ok, I am a bit further. However the comparison does not work. This is my current rule:

rule "calculate AQI"
        Time cron "0,30 * * ? * *"
        var AQI_10 = transform("JS", "pm10-aqi.js", out_Airrohr_PM10.state.toString)
        var AQI_25 = transform("JS", "pm25-aqi.js", out_Airrohr_PM25.state.toString)
        if (AQI_10 > AQI_25) out_Airrohr_AQI.postUpdate(AQI_10)
        else out_Airrohr_AQI.postUpdate(AQI_25)

However the if statement appears to always evaluate to True, even though it should not (I tried the values). When I add as Number after the transforms, i get the error “could not cast 81 to java.lang.Number”. My transform files now explicitly convert the calculation result to String before returning it. When removing the conversion to string, I get a similar behavior (though floats, even though the transform returns int).
The if statement should return false, since the AQI_10 number is smaller. Also in case it is a String, the String is shorter, so this does not make sense to me.

Yes, the transformation service always converts whatever your javascript returns into a string, no matter what it is. You need not do that part yourself, unless you want to format it first or something.

Okay, so your transform always returns strings. If you want to do a numerical comparison, x > y, you would need to parse your strings into some numerical form first. You can’t just cast a string as a number even if it looks like “81”.

ok, I got it to work. Thank you!