Skip to content

Instantly share code, notes, and snippets.

@vlad-ivanov-name
Last active October 12, 2016 21:08
Show Gist options
  • Save vlad-ivanov-name/0dc8c7bbf9b31d0a3225 to your computer and use it in GitHub Desktop.
Save vlad-ivanov-name/0dc8c7bbf9b31d0a3225 to your computer and use it in GitHub Desktop.
Openhab LIRC binding

Openhab LIRC binding

As for now, Openhab doesn't have a separate binding for LIRC. However, LIRC and Openhab can be connected together through TCP binding and some scripting.

Step 1: setup lircd

To make lircd accept connections, modify REMOTE_LIRCD_ARGS parameter in lirc/hadrware.conf as follows:

REMOTE_LIRCD_ARGS="--listen=127.0.0.1:8700"

Restart lircd. If you have already configured your remote, you should be able to connect to LIRC via netcat. Try pressing buttons on you remote and check the output of nc.

user@server:~$ nc 127.0.0.1 8700
0000000000000001 00 KEY_POWER REMOTE_LED
0000000000000001 00 KEY_POWER REMOTE_LED
0000000000000003 00 KEY_DOWN REMOTE_LED
0000000000000002 00 KEY_UP REMOTE_LED
0000000000000003 00 KEY_DOWN REMOTE_LED
0000000000000001 00 KEY_POWER REMOTE_LED
^C

Step 2: add item definition

Add the following definition to your items file:

String IR_command "IR Command" {
	tcp=">[127.0.0.1:8700:JS(lirc-key.js)]"
}

Make sure TCP binding is installed (check addons folder or refer to your package manager).

Step 3: setup a rule and transform script

The following script may be used to extract the key name from lircd output:

(function(i) {
	var
		array = i
			.replace(new RegExp('\\s+', 'gi'), ' ')
			.replace(new RegExp('[\\n\\r]', 'gi'), '')
			.trim()
			.split(' '),
		index = array.length - 1;
	return array[index - 1];
})(input)

Put it in transforms folder under the name lirc-key.js.

Now you need to create a rule triggered on every update of IR_command item. Example:

rule "Handle IR Keys"
	when
		Item IR_command received update
	then
		var command = IR_command.state as StringType
		
		switch command {
			case "KEY_POWER": {
				// do
			}
			case "KEY_DOWN": {
				// something
			}
			case "KEY_UP": {
				// here
			}
		}
end

Restart Openhab if needed. You should be able now to receive IR commands. You may want to modify the transformation stript if you have more than one RC, e. g.:

	return array[index] + '.' + array[index - 1];

In this case, IR_command.state will look like REMOTE_NAME.KEY_NAME.

@EdwinKM
Copy link

EdwinKM commented Aug 7, 2016

If you get this error use: "var command = IR_command.state.toString()" instead.
[ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Handle IR Keys': Cannot cast org.openhab.core.library.types.StringType to void

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment