[Resolved] Issues with the Exec Binding (OpenHAB 2)

Hello

  • Platform information:
    • Hardware: RPI 3 B+
    • OS: OpenHAB Raspbian based provided image, openhabianpi-raspbian-201804031720-gitdba76f6-crc9e93c3eb
  • openHAB version: 2
  • Issue of the topic: I cannot get my exec binding to work

home.sitemap:

sitemap home label="My home controls" 
{
Frame label="Informations" {
	Text item=Date
}

Frame label="Controls" {
	Switch item=Main_Light icon="light"
}
}

Main_Light.item

Switch Main_Light "My Main Light"

Switch mainlight {channel="exec:command:mainlight:run"}
// Arguments to be placed for '%2$s' in command line
String mainlight_Args {channel="exec:command:mainlight:input"}
// Output of command line execution 
String mainlight_out {channel="exec:command:mainlight:output"}

Main_Light.things:

Thing exec:command:mainlight [ command="/usr/bin/lights %2$s", interval=0, autorun=false ]

Main_Light.rules
rule "Your Execution"
when
 Item Main_Light changed
then
    if(Main_Light.state == ON){
            mainlight_Args.sendCommand("on")
    }else{
            mainlight_Args.sendCommand("off")
    }

  // wait for the command to complete
  // State will be NULL if not used before or ON while command is executed
  while(mainlight.state != OFF){
     Thread::sleep(500)
  }
  
  // Trigger execution
  mainlight.sendCommand(ON)
  
  // Logging of command line result
  logInfo("Your command exec", "Result:" + mainlight_out.state )
  
 // If the returned string is just a number it can be parsed
 // If not a regex or another transformation can be used
 // YourNumber.postUpdate(
 //        (Integer::parseInt(mainlight_out.state.toString) as Number )
 // )
end

And let not forget the /usr/bin/lights script:

#!/bin/bash
if [ $1 == on ]; then
sudo send_cmd "B0 91 4D BC C1 01 CA" && sudo send_cmd "B0 91 4D BC C1 01 CA" && sudo send_cmd "B0 91 4D BC C1 01 CA"
echo Light ON
elif [ $1 == off ]; then
sudo send_cmd "B0 91 4D BC C1 02 CA" && sudo send_cmd "B0 91 4D BC C1 02 CA" && sudo send_cmd "B0 91 4D BC C1 02 CA"
echo Light OFF
else
    echo Please send a valid command
fi

As you can see, I used the exemple available on this page: https://www.openhab.org/addons/bindings/exec/ , but no matter what I do it doesn’t seem to execute anything, the command work in the linux CLI, but not via my switch on OpenHAB page.

I don’t know if the things definition is ok…I usually do everything via PaperUI.

Do you see anything in the log?

If you run your script manually like in the guide you posted does it work? does it ask for password?

Are you sure the user “openhab” can execute send_cmd with root privileges without password?

if it’s the case see my reply here to enable to sudo a command without password: executeCommandLine : ssh/account/permissions setup for remote reboot action

Your rule includes logging. May we see the openhab.log and events.log for an attempt to switch?

There are problems with that example script - it most likely gives you the result of a previous run, but that does not of itself stop it working.

Unfortunately, the logs don’t seem to contain anything useful :frowning:

/var/log/openhab2/openhab.log:

2019-05-31 06:16:01.605 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 12:33:56.268 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 12:33:56.276 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 12:33:56.283 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 12:33:56.289 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text

/var/log/openhab2/events.log :

2019-05-31 06:16:06.069 [vent.ItemStateChangedEvent] - Main_Light changed from ON to OFF
2019-05-31 12:33:58.374 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command ON
2019-05-31 12:33:58.400 [vent.ItemStateChangedEvent] - Main_Light changed from OFF to ON
2019-05-31 12:34:03.451 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command OFF
2019-05-31 12:34:03.468 [vent.ItemStateChangedEvent] - Main_Light changed from ON to OFF

As for permission, everyone in the sudo group and openhab is allowed to launch send_cmd as root, as my sudoers file contain

%sudo   ALL=(root) NOPASSWD: /usr/bin/send_cmd
openhab   ALL=(root) NOPASSWD: /usr/bin/send_cmd

that indeed allow me to run the command without password (tested in CLI using “su openhab -s /bin/bash”

I think you have a problem with the Item-Definition “Date” in your sitemap. This error

is caused, when no Item-Definition in a .items-File is defined.

Either make a Declaration of the Item “Date” in your .items-File or comment out the Declaration in the sitemap-File.

At the moment, you cannot tell if your rule triggers or not. Add a logInfo() to the beginning of the then body section to find out.

I added loginfo() at the beginning of the then statement

when
 Item Main_Light changed
then
    logInfo()
    if(Main_Light.state == ON){

And here the change in the logs:

2019-05-31 14:43:11.434 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'Main_Light.rules'
2019-05-31 14:43:11.880 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: null
2019-05-31 14:43:11.882 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: null
2019-05-31 14:43:11.884 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: null
2019-05-31 14:43:11.882 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: null
2019-05-31 14:43:11.885 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: null
2019-05-31 14:43:11.889 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.890 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.893 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.894 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.895 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.894 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.897 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.893 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.900 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.900 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.903 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.903 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.905 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.908 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.905 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.909 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.910 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.912 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.915 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.917 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.919 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.920 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.915 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.913 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.924 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:11.926 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': Script interpreter couldn't be obtain
2019-05-31 14:43:33.504 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 14:43:33.510 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 14:43:33.515 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 14:43:33.519 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2019-05-31 14:43:35.445 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0
2019-05-31 14:43:45.796 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0
2019-05-31 14:44:33.368 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0
2019-05-31 14:44:34.778 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0
2019-05-31 14:44:36.046 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0
2019-05-31 14:44:37.564 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Your Execution': An error occurred during the script execution: index=0, size=0

events.log:

2019-05-31 14:43:34.935 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command ON
2019-05-31 14:43:34.964 [vent.ItemStateChangedEvent] - Main_Light changed from OFF to ON
2019-05-31 14:43:45.767 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command OFF
2019-05-31 14:43:45.780 [vent.ItemStateChangedEvent] - Main_Light changed from ON to OFF
2019-05-31 14:44:33.326 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command ON
2019-05-31 14:44:33.361 [vent.ItemStateChangedEvent] - Main_Light changed from OFF to ON
2019-05-31 14:44:34.739 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command OFF
2019-05-31 14:44:34.766 [vent.ItemStateChangedEvent] - Main_Light changed from ON to OFF
2019-05-31 14:44:36.013 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command ON
2019-05-31 14:44:36.032 [vent.ItemStateChangedEvent] - Main_Light changed from OFF to ON
2019-05-31 14:44:37.531 [ome.event.ItemCommandEvent] - Item 'Main_Light' received command OFF
2019-05-31 14:44:37.558 [vent.ItemStateChangedEvent] - Main_Light changed from ON to OFF

So there definitively something wrong, but no idea what?

I should also mention that I did install the regex transformation.

PS: I know date is invalid, I haven’t had time to look to fix it yet and just kept the example, it don’t matter for now (Except if that what break my light switch?)

Okay, logInfo() was just a shorthand example. In reality, just like the other place in your rule that uses logInfo, you need to give it two parameters.
logInfo(“mytest”, “here I am”)

Anyway, the error of it blowing up because of missing parameters does tell you that the rule is triggering.

Now add more logInfo with different messages so that you can see how far through the rule it is getting.

!!

Thanks!

// wait for the command to complete
// State will be NULL if not used before or ON while command is executed
while(mainlight.state != OFF){
  Thread::sleep(500)
}

was the issue, once I commented it, it started working!

Yes, the example is poor. Like it says, state will be NULL on first run, it’ll never become OFF so it loops endlessly.