Strange casting

I have a strange cast problem:


Number:Temperature    Netatmo_Outside_Temperature "Temperature [%.1f %unit%]" <temperature>    { channel = "netatmo:NAModule1:home:<hex>:Temperature" }
Number:Dimensionless  Netatmo_Outside_Humidity    "Humidity [%d %unit%]" <humidity>            { channel = "netatmo:NAModule1:home:<hex>:Humidity" }


rule "wu pws submit"
        Time cron "0 */1 * * * ?"
        var string logstring = "weatherunderground.rules";

        logInfo(logstring,"Triggered rule");

        var double tempval = (Netatmo_Outside_Temperature.state as QuantityType<Number>)

        logInfo(logstring,"Netatmo_Outside_temperature: {}", tempval);

(FYI: I’m hacking with the WeatherUnderground PWS Send rules that i found in this forum)

What I get is strange:

2018-08-21 12:58:00.070 [INFO ] [odel.script.weatherunderground.rules] - Netatmo_Outside_temperature: 28.299999237060546875 ℃

Obviously that number doesn’t have this resolution and it gets the " ℃" unit, which I suspect is my casting error. This problem greatly impact my rules as it seems that dumb comparison doesn’t work either, such as “27.5 > 28.299999237060546875 ℃” which clearly it is not.

How do I “strip” out the unit ? Should I create a new, different item ?


Wouldn’t you just use ‘as Number’ ?

rule "wu pws submit"
    Time cron "0 */1 * * * ?"
    var string logstring = "weatherunderground.rules"
    logInfo(logstring,"Triggered rule")
    var Number tempval = (Netatmo_Outside_Temperature.state as QuantityType<Number>).doubleValue
    logInfo(logstring,"Netatmo_Outside_temperature: {}", tempval.toString)

thanks for your answer.
the doubleValue correctly removes the °C unit but it still has a strange number:

2018-08-21 13:44:00.027 [INFO ] [odel.script.weatherunderground.rules] - Netatmo_Outside_temperature: 28.299999237060547

such resolution is way beyond the capability of the sensor thus I think is a strange conversion artifact.
It solves the comparison error, btw, so I’m quite happy :slight_smile:


You can also do the comparison this way:

  if (Netatmo_Cucina_Temperature.state > 27.5|"°C") {                                                                                                                                                                                                                                                                             

That works

It is a side effect with how most programming languages represent floating point numbers. It can not represent every possible number between the max value (somewhere around 1.710^308) and the minimum value (somewhere around -1.710^308). Instead it gets as close as it can but that often results in decimal places that seem to go on forever.

For the most part it is a quirk that developers just need to be aware of and deal with. The usual solution is to round the value when presenting it to the user but keep all the decimal places for calculations.