[SOLVED] OpenHAB2 exec Binding

Tags: #<Tag:0x00007fc8f840e778> #<Tag:0x00007fc8f840e610> #<Tag:0x00007fc8f840e480>

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?

the openhab-transformation-regex because when i try

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

It doesn’t work with %2$s

Okay, exec works for other people so it probably is something about your setup rather than a fault in openHAB regex. Let’s find out.

Which rule, using exec Things, are we concentrating on for now? I would suggest the simpler one to begin with. I already pointed out a fault in it that would definitely stop that working, but we didn’t find out if it then worked for you.

What does openhab.log have to say, when you expect a command to be run?

Ok i understand let’s go simpler when i look ar the log i see no update when i use the switch
Can you put the config you wxant i use please i have deleted the old one thx

I’m not writing it for you, you gave us your setup in your post #20 and following