Control lirc from openhab

Hi,

I’m trying to control lirc from openHab which sits on a separate raspberry pi. Remote access is configured and working. So I can either use a TCP binding (cannot get this to work) or a executecommandline.

As I had no success with the TCP binding, I’m trying to control it though a rule.

In my rule I have the following.

var result = executeCommandLine(“echo SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765”)
logInfo(“TVStandby”, result)

The log shows result as “null”

If i run that same command from ssh logged into the openhab raspberry pi, it works and lirc returns…

BEGIN
SEND_ONCE technika32 KEY_POWER
SUCCESS
END

Can anyone tell me what I’m doing wrong in openhab which means this isn’t working. I have another rule which executescommandline just fine.

Many Thanks,
Chris

You must provide a timeout in executeCommandLine in order for the call to wait for the command to finish executing and return the results as a String.

var result = executeCommandLine("echo SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765", 5000)

The above will wait for up to five seconds before timing out and returning null.

Thanks for the reply,

I’ve added that into the rule, and i now see the following.

2016-06-29 14:46:24.131 [INFO ] [openhab.model.script.TVStandby] - TV Standby: Begin
2016-06-29 14:46:24.140 [DEBUG] [g.openhab.io.net.exec.ExecUtil] - executed commandLine 'echo SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765’
2016-06-29 14:46:24.194 [DEBUG] [g.openhab.io.net.exec.ExecUtil] - exit code ‘0’, result 'SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765’
2016-06-29 14:46:24.201 [INFO ] [openhab.model.script.TVStandby] - SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765
2016-06-29 14:46:24.206 [INFO ] [openhab.model.script.TVStandby] - TV Standby: End

But nothing actually fires on the other pi running lirc.

Also its not the expected output of lirc as defined in the first post.

Any other ideas?

Many Thanks
Chris

No ideas. The problem lies in the command.

Can you get these commands to run from the command line yourself? If so the problem may be a permission problem as OH runs as the openhab user instead of your user. The openhab user as limited permissions and frequently this is the source of problems.

Yes I can get the commands to run fine under my user.

im not sure how i would go about troubleshooting if its permissions. It doesn’t require sudo, so I don’t think that’s it and even if it was I already have the openhab allowed to sudo without a password, which is working fine in my other rule.

I’m at a loose end.

I have two options, netcat as per this post, or get the tcp binding to work.

Or write a script that works and call that script from OH.

I see no reason why this wouldn’t work from executeCommandLine and there is no error message in the log to indicate a permission problem.

You can try running it from the command line as the openhab user and see what it does.

sudo runuser -l openhab -c 'echo SEND_ONCE technika32 KEY_POWER | nc 192.168.1.11 8765'

That would at least confirm or eliminate permission or some other issue with the openhab user being the problem.

That command doesn’t work, however this line does.

sudo -u openhab echo ‘SEND_ONCE technika32 KEY_POWER’ | nc 192.168.1.11 8765

Which i’m no expert but i think it does a similar thing.

I’ve just realised something. It’s ignoring the pipe and anything after it. The result is that of the echo only.

Interesting. I can’t say that I’ve tried to use a pipe from within openhab. It should work though.

At think point I would either just write the command as a shell script and call that from OH and move on to more interesting problems. Short of that I would just flail around trying random stuff like escaping the pipe or quoting the string sna commands in different ways hoping something works.

Thanks,

I found another thread you were talking on. Exec binding and echo, netcat, a pipe, carriage return and quotation marks

I’ve created a script which works just like the one towards the bottom.

var result = executeCommandLine("/home/pi/scripts/LircScripts/Lirc.sh technika32 KEY_POWER", 5000)

and it works with the expected results.

Do you know if its possiable to have multiple scripts kick off from an exec binding?

basically i want, script1.sh; sleep 3; script2.sh

Sure.

executeCommandLine("script1.sh", 5000)
Thread::sleep(3000) // msec
executeCommandLine("script3.sh", 5000)

Thank you so much for your help today. I’ve finally got it cracked. This rule will turn the power on to the tv and then send an ir code to take it out of standby. Turning off, just kills the power.

rule “h_GF_Living_TV ON/OFF”
when
Item h_GF_Living_TV changed
then
lock.lock()

try {
if(h_GF_Living_TV.state == OFF )
{
logInfo(“TV OFF”, “TV OFF: Begin”)
logInfo(“TV OFF”, “TV OFF: Power off Sockets”)
var result = executeCommandLine(“sudo /home/pi/scripts/433Scripts/StatusRCT08.sh 401419 1 OFF”, 5000)
logInfo(“TV OFF”, "TV OFF: " + result)
logInfo(“TV OFF”, “TV OFF: End”)
}

  if(h_GF_Living_TV.state == ON )
  {
  	logInfo("TV ON", "TV ON: Begin")
  	logInfo("TV ON", "TV ON: Power on Sockets")
  	var result1 = executeCommandLine("sudo /home/pi/scripts/433Scripts/StatusRCT08.sh 401419 1 ON", 5000)
  	logInfo("TV ON", "TV ON: " + result1)
  	Thread::sleep(5000)
  	logInfo("TV ON", "TV ON: Send KEY_POWER")
  	var result2 = executeCommandLine("/home/pi/scripts/LircScripts/Lirc.sh technika32 KEY_POWER", 5000)
  	logInfo("TV ON", "TV ON: " + result2)	
  	logInfo("TV ON", "TV ON: End")
  }

}
finally
{
lock.unlock()
}
end

i am new to openhab i want to do similar thing.

i want to send command on command line of my rpi < my command >

can you give a little deep idea about it explaining what addons are to be included ??
what is to be written in items and sitemap

You need the Exec binding addon. See the wiki page for details on how to set up and configure everything.

It might be worth while starting with the demo and play with that to learn how everything works.

At a high level you need:

  • install the exec addon (see the Exec Binding wiki page)
  • I recommend at least starting by creating a rule and using executeCommandLine rather than Items directly bound to the command until you get it to work. To do this create an unbound Item that triggers a rule and call executeCommandLine within that rule.
  • Put the Item as a switch on your sitemap.

thanks that was a great tutorial

is your lirc working after installing openhab.i first intalld openhab and now when i am trying to install lirc again its not working properly although it was working properly before

Hey, my lirc is on another pi. Its not running on the same pi as openhab. I’ve not tried to run them both together. Sorry

Chris,

Can you share your lirc script? I’m trying to do something very similar with a remote pi running lirc in order to control my benq projector.

Also is there anything you need to do on the remote lirc pi to enable remote commands?

Hi Matt,

Sure, I’m not near my computer at the moment but I’ll dig out and share tomorrow if that’s ok.

Chris

Hi Matt,

Sorry for the late reply.

Yes LIRC needs to be started in listening mode (–listen) http://www.lirc.org/html/lircd.html

You can then use my script.

#!/bin/bash
echo "SEND_ONCE $1 $2" | nc <lirc_host_name> 8765

This script basically sends a command via telnet to lirc. You can call it from an openhab rule like so…

executeCommandLine("/home/pi/scripts/LircScripts/Lirc.sh <remote> <key>", 5000)

eg

executeCommandLine("/home/pi/scripts/LircScripts/Lirc.sh technika32 KEY_POWER", 5000)

There is an example further up of how i use this in a rule.

Hope this helps.
Chris