PercentType in JavaScript (ECMAScript 2021+) doesn't work

I’m using a ECMAScript 2021+) an I’ve a problem with the say function and the PercentType

actions.Voice.say(MessageToSay, voiceForThisRule , 'sonos:Move:RINCON_mySecret', PercentType(75));	

this leads to an error. Somehow the PercentType is not defined

org.graalvm.polyglot.PolyglotException: ReferenceError: "PercentType" is not defined

Correct, You have to import it. There was some discussion on adding the types to the library but that hasn’t happened yet so you’ll need to add to the top of your script.

var { PercentType } = require(@runtime);

which will import the full JSR223 presets into your script. However, that will override items which may break things (I can’t tell, screenshots are useless on the forum, in the future please post the text).

So you probably will need to do something like:

var runtime = require('@runtime');

new runtime.PercentType(50)
2 Likes

thanks a lot. it works.
could’t you please explain this:

However, that will override items which may break things

currently I see no negativ side effects. Here the complete code, I made also some other modifications.

'use strict';
{
  const verbose          = true;
  const voiceForThisRule = "voicerss:deDE_Hanna";
 
  let runtime = require('@runtime');
  
  
  //array of all speakers
  let   sonosSpeakers    = [["SchlafzimmerSonosBeam",  "sonos:Beam:RINCON_347E5C81570501400", "Sonos Beam im Schlafzimmer"],
                            ["ArbeitszimmerSonosMove", "sonos:Move:RINCON_542A1B70351C01400", "Sonos Move"                ], 
                            ["FlurSonosRoam",          "sonos:Roam:RINCON_542A1B466E5201400", "Sonos Roam"                ],
                            ["WohnzimmerSonosArc",      "sonos:Arc:RINCON_48A6B80093C301400", "Sonos Arc im Wohnzimmer"   ],
                            ["BadSonosOne",             "sonos:One:RINCON_7828CAE2AA5001400", "Sonos One im Badezimmer"   ],
                            ["SonosOneKuche",           "sonos:One:RINCON_48A6B8F55C0001400", "Sonos One in der Küche"    ]];
  
  const idxItemName    = 0;
  const idxAudioSink   = 1;
  const idxTextToSpeak = 2;
  
  const itemMicrophoneExtensionName = "_Microphone";

  //java.lang.Thread.sleep(5000);

  for(let i in sonosSpeakers){
    let MicrophoneState = items.getItem(sonosSpeakers[i][idxItemName] + itemMicrophoneExtensionName).state.toString();
    let MicrophoneOn    = ("ON" == MicrophoneState);  
   
    let MessageToSay = "Jemand hat beim Putzen das Mikrofon beim " + sonosSpeakers[i][idxTextToSpeak] + (MicrophoneOn ? " eingeschaltet." : " ausgeschaltet!") ;
    let iconToSend   = MicrophoneOn ? "info" : "warn";

    if(verbose){
    console.info(sonosSpeakers[i][idxTextToSpeak] + " " + sonosSpeakers[i][idxItemName] + " " + sonosSpeakers[i][idxAudioSink]  + " Microphon state: " + MicrophoneState );
    console.info(MessageToSay)
    }
    
    if (!MicrophoneOn){       
      actions.Voice.say(MessageToSay, voiceForThisRule , sonosSpeakers[i][idxAudioSink], new runtime.PercentType(50));
      actions.NotificationAction.sendBroadcastNotification(MessageToSay, "recorder", iconToSend ); 
    }
    else
    {
      ; //nothing todo here  
    }  
  } //for(let i in sonosSpeakers)
}

That’s because you assigned @runtime to a variable.

By default there is a bunch of stuff that gets imported from openhab-js (unless you turn that off). Some of that stuff uses the same name as the JSR223 default context which gets imported by @runtime. So if you just import all of @runtime to the current Object, items from runtime, which is a java.util.Map of Item names and their current state will override items from openhab-js which is a JavaScript Object which centralizes all your interactions with Items.

1 Like