[SOLVED] How to use tts from jython with OH2.4?

  • Platform information:
    • Hardware: VM
    • OS: Ubuntu 18.04
    • Java Runtime Environment: 8
    • openHAB version: 2.4

Trying to write a rule that gives an audio alarm via tts when my basement floods. I hooked it up to the office light for now, but the “say” action is not working. How do I generate any action other than logging?

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")

from org.slf4j import LoggerFactory
log = LoggerFactory.getLogger("org.eclipse.smarthome.automation.flood_sensors");

class FloodSensorRule(SimpleRule):
    def __init__(self, name):
        self.name = name
        self.triggers = [
            TriggerBuilder.create()
                    .withId("FloodSensorTrigger")
                    .withTypeUID("core.ItemStateUpdateTrigger")
                    .withConfiguration(
                        Configuration({
                            "itemName": self.name
                        })).build()
        ]

    def execute(self, module, input):
        log.info("Got event!")
#        say('THIS LINE CROAKS!')                                                                                                                                                                                  

When I uncomment the last line and trigger the rule I get something like this:

13:34:05.968 [INFO ] [se.smarthome.automation.flood_sensors] - Got event!                                                                                                                                          
13:34:05.969 [ERROR] [tomation.core.internal.RuleEngineImpl] - Failed to execute rule '2a4b0be6-3cd0-4384-b57d-b02903bd0e69': Fail to execute action: 1                                                            
13:34:05.971 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - 2a4b0be6-3cd0-4384-b57d-b02903bd0e69 updated: RUNNING                                                                                               
13:34:05.971 [DEBUG] [tomation.core.internal.RuleEngineImpl] -                                                                                                                                                     
java.lang.RuntimeException: Fail to execute action: 1                                                                                                                                                              
        at org.eclipse.smarthome.automation.core.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1207) ~[216:org.eclipse.smarthome.automation.core:0.10.0.oh240]                                        
        at org.eclipse.smarthome.automation.core.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:1004) [216:org.eclipse.smarthome.automation.core:0.10.0.oh240]                                                

There are many example scripts out there that have import statements like this:

 from openhab import triggers

Alas, they all fail for me with import error of this kind:

13:37:04.612 [ERROR] [ript.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/flood_sensors.py': ImportError: No module named openhab in <script> at line number 4

I think I have everything installed correctly. Any suggestions what to do?
Thanks!

I’m not sure but I saw this in another example. Looks like you’re missing an import for the Voice action.

from core.actions import Voice
Voice.say("This will be said")

Examples

Edit: Just tried this with MaryTTS and was able to make it work. At first I got a warning about voice being null but it worked after I restarted Openhab. Not sure if the restart is required after installing a TTS service or if I just screwed something up.

Works!
Thanks much