JavaScript rule: ...padStart() is not a function in <eval>

  • openHAB version: 3.2

I’m struggling with JS standard, built-in String.prototype.padStart() method.

I’m using the scripting scratchpad to run this code:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.rule." + ctx.ruleUID);

var x = 18;
logger.info(x.toString());
logger.info(x.toString().padStart(3,'0'));

So I’m currently using this: :sweat_smile:

logger.info(("000" + x.toString()).substr(-3,3));

Anyone with a working code maybe? That would be great, thanks.

padStart is part of ECMA 2017 while bare OH uses ECMA 5.1.

See Is there a JavaScript function that can pad a string to get to a determined length? - Stack Overflow for ‘selfmade’ functions.

1 Like

Just one additional bit of info. If you want to use a more recent version of ECMAScript, install the JS Scripting add-on which offers ECMAScript 11.

Thanks guys!