[ECMA] correct usage of 'say' command

I tried to use the say command as follows:

...
var VoiceManager = Java.type("org.openhab.core.voice.VoiceManager");
....
 VoiceManager.say(sprechText, "voicerss:deDE", AudioSink);
.....

I’m getting this error:
Script execution of rule with UID 'SagEtwas' failed: TypeError: VoiceManager.say is not a function in <eval> at line number 31

Since say isn’t a static method I tried also to create an instance of VoiceManager with

var VoiceManager = Java.type("org.openhab.core.voice.VoiceManager");
var myvoice = new VoiceManager();
...
myvoice.say(sprechText, "voicerss:deDE", AudioSink);

Script execution of rule with UID 'SagEtwas' failed: TypeError: Can not create new object with constructor org.openhab.core.voice.VoiceManager with the passed arguments; they do not match any of its method signatures. in <eval> at line number 3
Where would I find the needed arguments to create a VoiceManager? I looked here: https://openhab.org/javadoc/latest/org/openhab/core/voice/voicemanager

The class to import is org.openhab.core.model.script.actions.Voice.

It’s always going to be something like “actions” or “extensions” that you need to import into your rule.

1 Like

Thanks for that one. Now my rule-suit is completly converted to JavaScript!
Learned a lot again!

[Edit:]
That statement was a bit early. The JavaScript rule runs through without an error, however no TTS-soundfile is played. Using the DSL-rule it works, searching…
[Edit2]
The last problem was caused by a switch case problem, the sound was played somewhere else (multi-room problem).

1 Like

Hello @opus,

I try to convert my dsl rules to ECMA rules in oh3 and one of my last problems is TTS and the say command. Did you solve your mentioned problems and can I use your jaca code also for a oh3 ui ecma rule?

Thank you for your help!

The below works for me, obviously change to your needs:

//-------------------PLAY Voice in JavaScript

var Voice = Java.type(“org.openhab.core.model.script.actions.Voice”);

var message = “Hello”;

var volume = new PercentType(80);

var sink = “sink”;

var tts = “tts”;

Voice.say(message, tts, sink, volume);

My working rule looks like that:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var Voice = Java.type("org.openhab.core.model.script.actions.Voice");
var selectedSpeaker = items['Lautsprecher']
var sprechText = items['SayCommand'];
var AudioSink;
  logger.info("Lautsprecher ={}", selectedSpeaker);
  logger.info("AudioSink ={}", AudioSink);
  switch  (selectedSpeaker.toString()) {
    case "Kueche":
      AudioSink = "sonos:PLAY1:RINCON_Bxx";
      break;
    case "Wohnzimmer":
      AudioSink = "sonos:PLAY1:RINCON_Byy";
      break;
    case "Buero":
      AudioSink = "sonos:PLAY1:RINCON_9zz";
      break;
    case "Gaestezimmer":
      AudioSink = "sonos:PLAY1:RINCON_Bqq";
      break;
    default:
      AudioSink = "sonos:PLAY1:RINCON_Bxx";
    }
  logger.info("Lautsprecher ={}", selectedSpeaker);
  logger.info("AudioSink ={}", AudioSink);
  Voice.say(sprechText, "voicerss:deDE", AudioSink);
  logger.info("Es ist alles gesagt!");

@christmas
I wonder which Audiosink does play your sound.

2 Likes

Thank you guys! Its working!