Openhab 3 javascript Exec.executeCommandLine passing variables

Hi,
This is my first post here.
I am trying to use openhab 3 rules and I am using the javascript for that. Previously I have used openhab 2.5 (which is still running) and I am trying to “port” over some of the rules I have in 2.5.

I am have trouble getting the Exec.executeCommandLine to work with variables. I can make it work if I HARD code the values in but if I try to use a variable it doesn’t work.

What I am trying to do is sent a MQTT command to a sonoff device that has a small display unit on it. The executeCommandLine dosn’t seem to like having spaces in it.

Here is the code. (I have never used javascript unti to day by the way)

// 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”);

var stateof = “TEST”;

results = Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub”,"-h",“192.168.0.164”,"-t",“cmnd/coffee/displaytext”,"-m","[zOa2f0s1l1c1]Door-garage[s3l2c1]MOVE[s1l8c1]Updated[c9]at:[c13tS]");

java.lang.Thread.sleep(5000);

Exec.executeCommandLine(Duration.ofSeconds(5), “/usr/bin/mosquitto_pub”,"-h",“192.168.0.164”,"-t",“cmnd/coffee/displaytext”,"-m","[o]");

logger.info("results = " + results);

What I would like to achieve is where the the word MOVE is I want to put the variable stateof there instead.

Anyone know how to do this?

Thanks

So instead of
"[zOa2f0s1l1c1]Door-garage[s3l2c1]MOVE[s1l8c1]Updated[c9]at:[c13tS]"
use
"[zOa2f0s1l1c1]Door-garage[s3l2c1]" + stateof + "[s1l8c1]Updated[c9]at:[c13tS]"
it’s just a string concatenation.

Yes That was it.

I was so close. I had + stateof but I didn’t have the other + on the right hand side.

No to work out how to get the syntax for the triggeringItem so I can pass that into the command line.

Thanks again.

Depends on your rule’s trigger mode, this has changed in OH3.
With Member of, you get triggeringItem as in OH2
If it’s an Item xxx list, you get triggerngItemName instead.
Is it the Item name that you want, or state, or what about it?

I think I have worked it out.
I want to display the label name.
This is what I did:
var device = itemRegistry.getItem(‘CoffeeMachine_CoffeeMachine’).getLabel();
Then I added that to the execute command:
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]”);

So it displays the correct information on my small display screen attached to a sonoff.

Thanks for your help.

I just realised I didn’t have the triggering Item in the command.

var device = itemRegistry.getItem(‘CoffeeMachine_CoffeeMachine’).getLabel();

I suppose I change the CoffeeMachine_Coffee_Machine to triggerngItemName

Will try it later on. I haven’t created a group yet to test it.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.