[SOLVED] OpenHAB 3 UI Rules ECMA "failed: TypeError: Math.log10"

  • Platform information:
    • RaspBerryPi 4 8GB
    • Openhabian
    • Java Runtime Environment: comes with openhabian
    • openHAB version: 3.0.1

I am making a rule for converting Hue Illuminance to Lux for IHC and wants to use Math.log10 but it gives me the following error in the openhab.log: “failed: TypeError: Math.log10 is not a function in <eval> at line number 2”

The rules is crated in the UI as an ECMAScript rule:

var tempno = parseFloat(itemRegistry.getItem("hueSensorIlluminanceOut").getState());
var luxnumber = Math.log10(tempno + 1) * 10000;
//Adjusting luxcalculation to be within IHC accept level of 0-60000 Lux
luxnumber = (luxnumber < 0) ? 0 : luxnumber;
luxnumber = (luxnumber > 60000) ? 60000 : luxnumber;
events.sendCommand("IHC_Lux_Ude_HUE", luxnumber);

If I try to use Math.log it woks fine på gives the wrong result not base-10
Autocompletion gives the values to use log, log10, log1p, log2 I have only tried log and log10 and log works log10 doesn’t.
My old rule from DSL works fine:

var tempno = (hueSensorIlluminanceOut.state as Number).doubleValue
var luxnumber = ((Math::log10(tempno) * 10000) + 1)
//Adjusting luxcalculation to be within IHC accept level of 0-60000 Lux
if(luxnumber < 0) {
luxnumber=0;
}
if(luxnumber > 60000) {
luxnumber=60000;
} 
IHC_Lux_Ude_HUE.postUpdate(luxnumber)

Any body got some experience or ideas on this?

Thanks for OH it is truly fantastic and a great forum.
Regards Jesper

Math.log10 was introduced in ES6. I believe OH3 uses ES5.

Hi Bruce
Ooohh.
Thanks. Do you then know what method to use in ES5 to do log10?

Thanks in advance
Regards
Jesper

I tried looking online for a babel transpiler to ES5 but ES5 has been deprecated so I did not find one.

Maths trick,

2 Likes

makes me remembering school days looong time ago.

Thanks guys

Oh yes the school days

This can replace the Math.log10 with a precision of aprox. 10E-15:
Math.log(tempno) / 2.302585092994046

Thanks a lot for showing the way.

Regards
Jesper

1 Like