[SOLVED] OpenHAB2 exec Binding

Hello All,
I started OH2 experience 2 days ago and the simplest thing I could imagine takes just enormous amount of time.
My objective is to simulate alarms.
F.e. I declare something as an alarm trigger and something as a siren (both based on files).
My configuration is exactly what was discussed above and it simply half-working!
The siren.sh was never triggered. loglevel TRACE for the EXEC handler never shows “Executing”.
Moreover after several tries the BASIC UI just stops refreshing the data.
If somebody knows the reason why, please advise the solution…

Thanks.

sitemap:
//sm
sitemap home label=“P12” {
Frame label=“ALARM” {
Text item=SENSOR1DATA label="Out: "
Text item=Siren label="SIREN: "
}
}

//rule
rule "Sensor1 On"
when
Item SENSOR1DATA changed to 150
then
logInfo(“Sensors”, “Sensor 1 was triggered”)
sendCommand(Siren, “ON”)
end

rule "Sensor1 Off"
when
Item SENSOR1DATA changed to 110
then
logInfo(“Sensors”, “Sensor 1 unset”)
sendCommand(Siren, “OFF”)
end

////things
Thing exec:command:sensor1 [command="/home/oh2/sensor1.sh", interval=5, timeout=5]
Thing exec:command:sirn [command="/home/oh2/siren.sh %2$s", interval=0, timeout=5, autorun=true]

//Items
String SENSOR1DATA “Value: [%s]” {channel=“exec:command:sensor1:output”}
String Siren “[%s]” {channel=“exec:command:sirn:input”, autoupdate=“true”}

2017-08-15 22:35:03.220 [INFO ] [lipse.smarthome.model.script.Sensors] - Sensor 1 unset
2017-08-15 22:35:13.011 [INFO ] [lipse.smarthome.model.script.Sensors] - Sensor 1 was triggered
2017-08-15 22:35:23.053 [INFO ] [lipse.smarthome.model.script.Sensors] - Sensor 1 unset

There is a bug right now that makes changed to rule triggers only work for enum types (e.g. ON, CLOSED, etc.).

Try just using changed and if statements to handle your cases and see if that makes a difference.

Thank you for the idea Rich,
I’ve just solved the problem but the source is different.

Apparently, it has nothing to do with the “changed” syntax, it works just fine.
The problem was in the access rights.
Today, I saw your earlier post how to tackle the Exec Bind problems and one of actions was to check the user permissions.
First off, activate the log LEVEL

log:set TRACE org.openhab.binding.exec

[DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ‘/home/oh2/siren.sh: line 2: /var/lib/openhab2/oh2/state.txt: No such file or directory’

In my siren.sh I used $HOME to do the output what’s wrong for another user unless one adapts permissions!
Solved.
One small hint:
The exec binding will never write the same string (the same data of the command) that has already been written:
https://github.com/openhab/openhab2-addons/blob/master/addons/binding/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/handler/ExecHandler.java:

                String previousInput = lastInput;
                lastInput = command.toString();
                if (lastInput != null && !lastInput.equals(previousInput)) {

Thank you again.

Hey and thank you for this detailed tutorial.

I copied your sources, so I have exactly the same. Unfortunately, the code is not sent when switching the Power Plug B in the app. The exec-binding is installed and moved to the adding file. Do I have to change the rights there ?Is there something else, I have to consider ?

Would be great if somebody could help me. Thank you !

I try to switch a powe plug with the sources from Josar.
But it does not work.
I use the openhabian.
When is use sudo su in terminal the command sudo ~/raspberry-remote/send 00001 2 1
works.
But only with this root rights. The openhabian user and the openhab can access to the command.

What should I do to fix ??

As openhab has no root you have to Grand it the access to the send command of the raspberry-remote by changing the sudoers. As it is stated in the description which my example is based on

http://www.forwardme.de/2016/11/24/433mhz-funksteckdosen-mit-raspberry-pi-und-openhab-schalten/

sudo mcedit /etc/sudoers

# User privilege specification
 root ALL=(ALL:ALL) ALL
 openhab ALL=NOPASSWD: /opt/raspberry-remote/send*

NOOOOOOOOOOO! Always use visudo to edit sudoers.

If you make one little syntax mistake you will all but brick your machine. The only way to recover is to boot or move your disk/sd card to another machine, mount that file system, and then edit out the errors you introduced. Even with visudo you can run into problems (e.g. I once lost my ssh connection while visudo was writing out to the sudoers file leaving it corrupted).

See the following for how to change the editor if you don’t like vi:

You want to use visudo because it makes sure there are no syntax errors in a temp file before it copies your changes over to the active file. So if you have an error you are not locked out of your machine.

1 Like

Hello Josar,

thanks for your answer

I did’nt have success with the powerplug access yet.

I added my config and log files.
There are errors.

By changing the sudoers file the system crashed two times.
So I want to fix the errors in config first and then get to the wrong access rights.

  • I dont’t know if the path to the send command is right.
    I cant find it in dir opt at my system

  • which user I have to edit in sudoers?
    the user on my pi is openhabian.
    Should I edit openhab or openhabian?

looking forward hearing from you

Thorsten

meine.items

Switch Power_Plug_Socket_B

Switch Remote_Send { channel=“exec:command:remote-send:run” }
String Remote_Send_Args { channel=“exec:command:remote-send:input”}
String Remote_Send_Out { channel=“exec:command:remote-send:output” }

powerplug.rules

rule "Poweroutlet B"
when
Item Power_Plug_Socket_B received command
then
if(receivedCommand == ON){
Remote_Send_Args.sendCommand(“00001 2 1”)
}else{
Remote_Send_Args.sendCommand(“00001 2 0”)
}

  // wait for the command to complete
  while(Remote_Send.state != ON){
     Thread::sleep(500)
  }

logInfo(“Power_Plug”, “Results are: \n” + Remote_Send_Out.state)
end

meine.sitemap

sitemap powerplugs label=“Wireless Poweroutlets”
{
Frame label=“Poweroutlets”
{
Switch item=Power_Plug_Socket_B label=“Power Plug B”
}
}

meine.things

Thing exec:command:remote-send [
command=“sudo /opt/raspberry-remote/send %2$s”,
interval=0,
autorun=true,
]

openhab.log

2017-10-05 19:03:46.910 [INFO ] [panel.internal.HABPanelDashboardTile] - Stopped HABPanel
2017-10-05 19:03:46.936 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Stopped Paper UI
2017-10-05 19:03:47.025 [INFO ] [basic.internal.servlet.WebAppServlet] - Stopped Basic UI
2017-10-05 19:03:47.086 [INFO ] [.dashboard.internal.DashboardService] - Stopped dashboard
2017-10-05 19:04:42.698 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'meine.items’
2017-10-05 19:04:42.812 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'powerplugs.items’
2017-10-05 19:04:46.860 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘meine.rules’ has errors, therefore ignoring it: [15,70]: missing ‘)’ at ‘")\r\nend\r\n’

2017-10-05 19:04:48.785 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'powerplugs.rules’
2017-10-05 19:04:51.019 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'powerplugs.sitemap’
2017-10-05 19:04:51.060 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'meine.sitemap’
2017-10-05 19:04:52.052 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'exec.things’
2017-10-05 19:04:52.085 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘meine.things’ has errors, therefore ignoring it: [5,1]: mismatched input ‘]’ expecting RULE_ID

2017-10-05 19:04:53.786 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at http://192.168.1.81:8080
2017-10-05 19:04:54.006 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at https://192.168.1.81:8443
2017-10-05 19:04:54.532 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app
2017-10-05 19:04:54.606 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui
2017-10-05 19:04:54.671 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2017-10-05 19:05:30.611 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename meine.sitemap does not match the name powerplugs of the sitemap - please fix this as you might see unexpected behavior otherwise.
2017-10-05 19:05:31.248 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename meine.sitemap does not match the name powerplugs of the sitemap - please fix this as you might see unexpected behavior otherwise.
2017-10-05 19:05:34.117 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate command ‘00001 2 1’ for item ‘Remote_Send_Args’ to handler for channel ‘exec:command:remote-send:input’, because no thing with the UID ‘exec:command:remote-send’ could be found.
2017-10-05 19:05:34.121 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate update ‘00001 2 1’ for item ‘Remote_Send_Args’ to handler for channel ‘exec:command:remote-send:input’, because no thing with the UID ‘exec:command:remote-send’ could be found.
2017-10-05 19:05:34.554 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate command ‘00001 2 0’ for item ‘Remote_Send_Args’ to handler for channel ‘exec:command:remote-send:input’, because no thing with the UID ‘exec:command:remote-send’ could be found.
2017-10-05 19:05:34.559 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate update ‘00001 2 0’ for item ‘Remote_Send_Args’ to handler for channel ‘exec:command:remote-send:input’, because no thing with the UID ‘exec:command:remote-send’ could be found.

events.log

2017-10-05 19:03:47.369 [temChannelLinkRemovedEvent] - Link ‘Remote_Send_Out => exec:command:remote-send:output’ has been removed.
2017-10-05 19:03:47.371 [temChannelLinkRemovedEvent] - Link ‘Remote_Send_Args => exec:command:remote-send:input’ has been removed.
2017-10-05 19:03:47.373 [temChannelLinkRemovedEvent] - Link ‘Remote_Send => exec:command:remote-send:run’ has been removed.
2017-10-05 19:04:51.732 [ItemChannelLinkAddedEvent ] - Link ‘Remote_Send_Out-exec:command:remote-send:output’ has been added.
2017-10-05 19:04:51.734 [ItemChannelLinkAddedEvent ] - Link ‘Remote_Send_Args-exec:command:remote-send:input’ has been added.
2017-10-05 19:04:51.736 [ItemChannelLinkAddedEvent ] - Link ‘Remote_Send-exec:command:remote-send:run’ has been added.
2017-10-05 19:05:33.792 [ItemCommandEvent ] - Item ‘Power_Plug_Socket_B’ received command ON
2017-10-05 19:05:33.838 [ItemStateChangedEvent ] - Power_Plug_Socket_B changed from NULL to ON
2017-10-05 19:05:34.120 [ItemCommandEvent ] - Item ‘Remote_Send_Args’ received command 00001 2 1
2017-10-05 19:05:34.126 [ItemStateChangedEvent ] - Remote_Send_Args changed from NULL to 00001 2 1
2017-10-05 19:05:34.529 [ItemCommandEvent ] - Item ‘Power_Plug_Socket_B’ received command OFF
2017-10-05 19:05:34.539 [ItemStateChangedEvent ] - Power_Plug_Socket_B changed from ON to OFF
2017-10-05 19:05:34.558 [ItemCommandEvent ] - Item ‘Remote_Send_Args’ received command 00001 2 0
2017-10-05 19:05:34.565 [ItemStateChangedEvent ] - Remote_Send_Args changed from 00001 2 1 to 00001 2 0

I updated my post and added some more information. I will try to set this up as explained in the next days. But i think it should work like that.

The thing had an additional comma after autorun=true. It did not provoke errors when i used it like that but now it does. I just removed the comma and all formating so it is one line. And the error was solved.

The sitemap file has to have the same name as the sitemap in the file.

To find out which user you have to add to the gpio group just search who owns openhab by executing following ps command. I think no additional editing of sudoers should be required.

ps -ef | grep open
openhab    424     1  0 18:31 ?        00:00:00 /bin/bash /usr/share/openhab2/runtime/bin/karaf server
...

Is the user openhab or openhabian?

@rlkoshak Could you please hint to where this documented.

https://docs.openhab.org/addons/bindings/exec/readme.html#thing-configuration

the current State of the input channel (see below, example: %2$s)

Hello i cannot use exec commade nothing happen :confused:

2019-09-21 00:01:49.890 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_B’ received command ON
2019-09-21 00:01:50.007 [vent.ItemStateChangedEvent] - Power_Plug_Socket_B changed from OFF to ON
2019-09-21 00:12:22.641 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_B’ received command OFF
2019-09-21 00:12:22.737 [vent.ItemStateChangedEvent] - Power_Plug_Socket_B changed from ON to OFF
2019-09-21 00:13:41.252 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_B’ received command ON
2019-09-21 00:13:41.357 [vent.ItemStateChangedEvent] - Power_Plug_Socket_B changed from OFF to ON
2019-09-21 00:15:06.904 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_B’ received command OFF
2019-09-21 00:15:07.010 [vent.ItemStateChangedEvent] - Power_Plug_Socket_B changed from ON to OFF
2019-09-21 00:15:08.948 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_B’ received command ON
2019-09-21 00:15:09.022 [vent.ItemStateChangedEvent] - Power_Plug_Socket_B changed from OFF to ON

When i type in terminal with user openhap the command works .

items

Switch Power_Plug_Socket_B
Switch Remote_Send { channel=“exec:command:remote-send:run” }
String Remote_Send_Args { channel=“exec:command:remote-send:input”}
String Remote_Send_Out { channel=“exec:command:remote-send:output” }

things
Thing exec:command:remote-send [
command=“/home/pi/src/433Utils/RPi_utils/send %2$s”,
interval=0,
autorun=true]

The working command in terminal :
/home/pi/src/433Utils/RPi_utils/send 00001 1 1

You’d probably want a rule to do something with your exec linked Items, when you send Power_Plug_Socket_B a command.

thks for the reply this is my rules

rule “Poweroutlet B”
when
Item Power_Plug_Socket_B received command
then
if(receivedCommand == ON) Remote_Send_Args.postUpdate(“00001 3 1”)
else Remote_Send_Args.postUpdate(“00001 3 0”)

Remote_Send.sendCommand(ON)

// wait for the command to complete
while(Remote_Send.state != ON) Thread::sleep(100)

logInfo(“Power_Plug”, "Resuts are: " + Remote_Send_Out.state)
end

That won’t work.
To send parameter data to Exec binding, you must send as command to the ‘input’ channel Item.

The docs weren’t very clear on this, and have been updated recently.

If you are going to trigger execution with the ‘run’ channel, you do not want autorun to be true in your Thing config. That is about triggering from the input channel.

A better way to do this without waiting is to have a separate rule

rule “Poweroutlet B responds”
when
   Item Remote_Send_Out received update
then
   logInfo(“Power_Plug”, "Results are: " + Remote_Send_Out.state.toString)
end

Thanks for your reply i find an other posts with rf433 and exec command i have updated my files with your post for 2s$

powerplugs.rules
import java.util.concurrent.locks.ReentrantLock

val ReentrantLock transmitter = new ReentrantLock

rule “Single Plug”
when
Member of Gplugs received command
// Item Power_Plug_Socket_1 received command or
// Item Power_Plug_Socket_2 received command or
// Item Power_Plug_Socket_3 received command or
// Item Power_Plug_Socket_4 received command
then

logInfo("Power_Plug", "Member " + triggeringItem.name + " to " + receivedCommand)

try {
  // Lock transmitter so other executed rules dont't use it at the same time.
  // Concurrent calls of the rule will wait till the resource is free again.
  transmitter.lock()

  // Get the item which triggered the rule
  // Split the name and get the second part, the number to set as command.
  val num = triggeringItem.name.toString.split("Power_Plug_Socket_").get(1)

  // Set the command which should be executed to the output channel
  // Auto trigger will then execute the Thing.
  if(receivedCommand == ON){
    Remote_Send_Args.sendCommand("00001 " + num +" 1")
  }else{
    Remote_Send_Args.sendCommand("00001 " + num +" 0")
  }

  // Wait for the command to complete
  while(Remote_Send.state != OFF){
    Thread::sleep(100)
  }

  // Mulltiple trigger do not work if there is no break here
  // maybe external skript needs some time to properly free resources.
  Thread::sleep(400)
  logInfo("Power_Plug", Remote_Send_Out.state.toString.replaceAll("\r|\n"," ") )
}catch(Throwable t) {}
finally {
    // Free the resource for the next call.
    transmitter.unlock()
}

end

powerplugs.sitemap
sitemap powerplugs label=“Wireless Poweroutlets”
{
Frame label=“Poweroutlets”
{
Switch item=Gplugs label=“All Power Plugs”
Text item=PlaceHolder label=“” icon=“none”
Switch item=Power_Plug_Socket_1 label=“Power Plug A”
Switch item=Power_Plug_Socket_2 label=“Power Plug B”
Switch item=Power_Plug_Socket_3 label=“Power Plug C”
Switch item=Power_Plug_Socket_4 label=“Power Plug D”
}
}

powerplugs.items
// RF Socket Items
Group:Switch:OR(OFF, ON) Gplugs

Switch Power_Plug_Socket_1 (Gplugs)
Switch Power_Plug_Socket_2 (Gplugs)
Switch Power_Plug_Socket_3 (Gplugs)
Switch Power_Plug_Socket_4 (Gplugs)
String PlaceHolder “”

Switch Remote_Send { channel=“exec:command:remote-send:run” }
String Remote_Send_Args { channel=“exec:command:remote-send:input” }
String Remote_Send_Out { channel=“exec:command:remote-send:output” }

My log
==> /var/log/openhab2/events.log <==
2019-09-22 10:56:53.291 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_1’ received command ON
2019-09-22 10:56:53.336 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_2’ received command ON
2019-09-22 10:56:53.390 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_3’ received command ON
2019-09-22 10:56:53.485 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_4’ received command ON
2019-09-22 10:56:53.935 [GroupItemStateChangedEvent] - Gplugs changed from ON to OFF through Power_Plug_Socket_1
2019-09-22 10:56:53.978 [vent.ItemStateChangedEvent] - Power_Plug_Socket_1 changed from OFF to ON
2019-09-22 10:56:54.030 [vent.ItemStateChangedEvent] - Power_Plug_Socket_2 changed from OFF to ON
2019-09-22 10:56:54.063 [vent.ItemStateChangedEvent] - Power_Plug_Socket_3 changed from OFF to ON
2019-09-22 10:56:54.087 [vent.ItemStateChangedEvent] - Power_Plug_Socket_4 changed from OFF to ON
2019-09-22 10:56:54.113 [GroupItemStateChangedEvent] - Gplugs changed from OFF to ON through Power_Plug_Socket_4
2019-09-22 11:04:46.486 [ome.event.ItemCommandEvent] - Item ‘Gplugs’ received command ON
2019-09-22 11:04:46.583 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_1’ received command ON
2019-09-22 11:04:46.647 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_2’ received command ON
2019-09-22 11:04:46.745 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_3’ received command ON
2019-09-22 11:04:46.836 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_4’ received command ON
2019-09-22 11:04:48.112 [ome.event.ItemCommandEvent] - Item ‘Gplugs’ received command OFF
2019-09-22 11:04:48.159 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_1’ received command OFF
2019-09-22 11:04:48.229 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_2’ received command OFF
2019-09-22 11:04:48.280 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_3’ received command OFF
2019-09-22 11:04:48.385 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_4’ received command OFF
2019-09-22 11:04:48.853 [vent.ItemStateChangedEvent] - Power_Plug_Socket_1 changed from ON to OFF
2019-09-22 11:04:48.909 [vent.ItemStateChangedEvent] - Power_Plug_Socket_2 changed from ON to OFF
2019-09-22 11:04:49.017 [vent.ItemStateChangedEvent] - Power_Plug_Socket_3 changed from ON to OFF
2019-09-22 11:04:49.063 [vent.ItemStateChangedEvent] - Power_Plug_Socket_4 changed from ON to OFF
2019-09-22 11:04:50.585 [ome.event.ItemCommandEvent] - Item ‘Power_Plug_Socket_3’ received command ON
2019-09-22 11:04:50.743 [vent.ItemStateChangedEvent] - Power_Plug_Socket_3 changed from OFF to ON

This is going to become difficult if you change the question every time someone offers an answer.

What are you trying to do now, and what is the problem that you see?

Add some temporary logInfo() to your rule so that you can see progress.

Hi sorry and thanks for your time
i have four power outlet with rf
I can switch them with send command and wiring pi in the terminal with user openhab
When i put this the rule i can switch off or on power outlets maybe a problem with regex ?

rule “Poweroutlet B”
when
Item Power_Plug_Socket_B received command
then
var String results = “”
if (receivedCommand==ON ) {
results = executeCommandLine(“sudo /home/pi/src/433Utils/RPi_utils/send 00001 1 1”, 5000)
}
else if (receivedCommand==OFF ) {
results = executeCommandLine(“sudo /home/pi/src/433Utils/RPi_utils/send 00001 1 0”, 5000)
}
logInfo(“Power_Plug”, "Results are: " + results)
end

Well, that is yet another way to do it. What do you want to happen next?

What regex?