Strange casting

Hi,
I have a strange cast problem:

Items:

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" }

Rules:

rule "wu pws submit"
    when
        Time cron "0 */1 * * * ?"
    then
        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);
end

(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 ?
Thanks

Luca

Wouldn’t you just use ‘as Number’ ?

rule "wu pws submit"
when
    Time cron "0 */1 * * * ?"
then
    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)
end

Hi,
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:

Thanks.

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.

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html