Hi,
I am trying to “port” my OH2.5 DSL rules into ECMAscript and I am really finding it a challenge.
First question is do I really need to use ECMAscript?
I just wanted to do a simple get the triggering item name or label to put into a variable. I cannot do it in ECMAscript. Everything I have read says to use:
ir.getItem(event.itemName)
I got that from here: But How Do I…? — openHAB Helper Libraries documentation
When I type event and then press ctrl-space my only option is events. There is NO event.
I give up on this ECMAscript. I can get simple things to work.
Here is the code I am using and I have spent 3 days trying to crack this:
// logger.info(“About to test executeCommandLine”);
var logger = Java.type(‘org.slf4j.LoggerFactory’).getLogger(‘org.openhab.rule.’ + ctx.ruleUID);
var Exec = Java.type(“org.openhab.core.model.script.actions.Exec”);
var Duration = Java.type(“java.time.Duration”);
//The statement below doesn’t work
var device = event.itemName();
//I want to get the item that triggered the event and put it in below where it says CoffeeMachine_CoffeeMachine
//as the statement below works!
//var device = itemRegistry.getItem(‘CoffeeMachine_CoffeeMachine’).getLabel();
// var device = ItemRegistry.getItem(triggeringItem.name.toString();
var stateof = “MOVING”;
// Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub -h 192.168.0.164 -t cmnd/coffee/displaytext -m [zOa2f0s1l1c1]Door-garage[s3l2c1]MOVING[s1l8c1]Updated[c9]at:[c13tS]”);
// results = Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub”,"-h",“192.168.0.164”,"-t",“cmnd/coffee/displaytext”,"-m",mqtt,stateof,mqtt1);
results = Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub”,"-h",“192.168.0.164”,"-t",“cmnd/coffee/displaytext”,"-m","[zOa2f0s1l1c1]" + device + “[s3l2c1]” +stateof + “[s1l8c1]Updated[c9]at:[c13tS]”);
java.lang.Thread.sleep(10000);
Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub”,"-h",“192.168.0.164”,"-t",“cmnd/coffee/displaytext”,"-m","[o]");
logger.info("results = " + results);
Here is the error I get:
2021-02-05 15:42:00.159 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘5d74ea1203’ failed: ReferenceError: “event” is not defined in at line number 8
I am finding it difficult to find working examples. I just get things like use this:
ir.getItem(event.itemName)
Great but how do I use the above.
If anyone out there has managed to get something similar to work let me know.
Thanks