How to use the exec binding with Windows 10

  • Platform information:
    • Hardware: Beelink BT3Pro
    • OS:Windows 10 Pro
    • Java Runtime Environment: Java 11
    • openHAB version: 4.0
      I would like to start programs on my PC through the Exec binding.
      For a tryout I tried to start google:
      Thing:

      Item:

      I added the command to the whitelist.
      Problems are:
      I cann’t start the program using the Item switch.
      So I used a rule and a test switch to send an ON command to the Google Start Item.
      Anyway Google is not started.
      Is there any tutorial how to use the exec binding or can someone help me an tell me how to use it?
      Thank you

Note sure this would be the issue - but OH 4.0 is not supported on Java 11 - requires Java 17. Surprised it runs at all?

1 Like

The return value should be visible in the log file. What does it say?

Sorry, I am using Java 17

In the evenst.log file I found:
2023-08-24 14:02:47.672 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Test’ received command OFF
2023-08-24 14:02:47.674 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item ‘Test’ updated to OFF
2023-08-24 14:02:47.675 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Test’ changed from ON to OFF
2023-08-24 14:02:49.351 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Test’ received command ON
2023-08-24 14:02:49.355 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item ‘Test’ updated to ON
2023-08-24 14:02:49.355 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Test’ changed from OFF to ON
2023-08-24 14:02:49.360 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Google_Start’ received command ON
2023-08-24 14:02:49.361 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘Google_Start’ predicted to become ON
2023-08-24 14:02:49.366 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item ‘Google_Start’ updated to ON
2023-08-24 14:02:49.366 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Google_Start’ changed from OFF to ON
2023-08-24 14:02:49.388 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item ‘Google_Start’ updated to ON
2023-08-24 14:02:50.510 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item ‘Google_Start’ updated to OFF
2023-08-24 14:02:50.511 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Google_Start’ changed from ON to OFF

nothind about the exec

Do you have the output channel linked to an item? What‘s the value?

I have linked the chanel and the return is NULL

If you execute the command chrome.exe through a rule (execute command line), does this work? Could you also check/correct the permission of the whitelist file through openhabian-config?

I am running openhab on Windows 10!
The command is in the whitelist. Otherwise I get an error in the openhab.log
I tried:
executeCommandLine(“C:\Program Files\Google\Chrome\Application\chrome.exe”)
That doesn’t work either.

Is the syntax of the executecommandline correct? In the forum I have found many different forms like:
executeCommandLine(Duration.ofSeconds(5),“C:\Program Files\Google\Chrome\Application\chrome.exe”)
Can I get a return value?
Google_Return = executeCommandLine(“C:\Program Files\Google\Chrome\Application\chrome.exe”)
doesn’t work.

I am just pursuing if the command maybe is wrong. Maybe you need to escape the backslashes.
Yes, you can get a returnvalue

logInfo(executeCommandLine(Duration.ofSeconds(5),“C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe”)

I have tried it this way:

response = executeCommandLine(Duration.ofSeconds(20),"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")

In the log file I get a timeout warning for the command and the respone variable has the value null.

I made the test simpler:

    response = executeCommandLine("echo hi there")
    logInfo("Test ON", response)

this lead to the following warning in the openhab.log:

2023-08-24 18:25:58.685 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[echo hi there]'
java.io.IOException: Cannot run program "echo hi there": CreateProcess error=2, Das System kann die angegebene Datei nicht finden

response value = null

echo hi there works fine entered manually in cmd.

Is there any way to make a bomb proof test?

I am running openHAB as a service.

Neither executeCommandLine nor the Exec binding are designed to kick off long running processes. both expect what ever program you run starts, does its thing, and then exits with some meaning output printed to the output.

Chrome doesn’t do that. It starts and stays running until you close it. Eventually OH will kill the process when it times out.

There’s also a potential issue with starting a program with a GUI from openHAB. There might be security controls in place to prevent that. I know that’s the case for Linux.

I think the whitespaces in the command could still make some problems.
Let‘s start with a bullet proof test.
Create a .bat file e.g. in your scripts folder with this command

dir > c:\path to your script file\output.txt

and call this command through executecommand line.
Eventually you have to start command.com and pass the batch file as an argument
If that works then replace dir with google command.

There is a long discussion in this thread:

I created a test.bat. If I start it manually it writes the output.txt.
Started with executecommandline it does nothing.
I dont know excatlly how to start it with command.com.

Did you try

logInfo(executeCommandLine(Duration.ofSeconds(5), "C:\\path to bat\\test.bat")

Just like to know if it works.

logInfo(executeCommandLine(Duration.ofSeconds(5), "command.com" "/c" "start" "\"\"" "C:\\path to bat\\test.bat")

This is how you start a new command.com instance which runs the batch file.

logInfo(executeCommandLine(Duration.ofSeconds(5),"C:\\test.bat")) 

leads to:

2023-08-24 21:59:00.869 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Test-1' failed: An error occurred during the script execution: index=1, size=1 in Test
logInfo(executeCommandLine(Duration.ofSeconds(5),"command.com" "/c" "start" "\"\"" "C:\\test.bat"))

seams to have wrong syntax:

2023-08-24 22:04:33.871 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Test.rules' has errors, therefore ignoring it: [13,68]: mismatched input '"/c"' expecting ')'[13,102]: mismatched input ')' expecting 'end'

I had the same problem. Look here: