Control Raspberry I/O pin with EXEC

I want to control an I/O pin on my raspberry with the exec 2.x binding. I have a python script , which works:

import RPi.GPIO as GPIO
import sys
if len(sys.argv) > 1:
    MOTOR_GPIO = 4
    if sys.argv[1] == "ON":
        GPIO.output(MOTOR_GPIO, GPIO.HIGH)
    elif sys.argv[1] == "OFF":
        GPIO.output(MOTOR_GPIO, GPIO.LOW)

My home.sidemap is:

sitemap home label="Motoren"
	Frame label="Motor" {
		Switch  item=MOTOR_1_SWITCH

My home.item is :

String MOTOR_1_SWITCH "Motor 1"  ["Switchable"] { channel="exec:command:motor_1_control:input", channel="exec:command:motor_1_status:output", autoupdate="false" }

My home.things is:

Thing exec:command:motor_1_control [ command="/etc/openhab2/scripts/ %2$s", interval=0, autorun=true ]
Thing exec:command:motor_1_status [ command="/etc/openhab2/scripts/ status", interval=60, timeout=5 ]

And my home.rules are:

rule "Motor Power"
	Item Motor1 changed

	while(Motor1.state != OFF){

When openhab starts, I get this event.log:

2018-03-12 16:04:25.193 [.ItemChannelLinkAddedEvent] - Link ‘MOTOR_1_SWITCH-exec:command:motor_1_status:output’ has been added.
2018-03-12 16:04:25.198 [.ItemChannelLinkAddedEvent] - Link ‘MOTOR_1_SWITCH-exec:command:motor_1_control:input’ has been added.
2018-03-12 16:04:25.351 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_control’ changed from UNINITIALIZED to INITIALIZING
2018-03-12 16:04:25.367 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_control’ changed from INITIALIZING to ONLINE
2018-03-12 16:04:25.370 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_status’ changed from UNINITIALIZED to INITIALIZING
2018-03-12 16:04:25.379 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_status’ changed from INITIALIZING to ONLINE
2018-03-12 16:04:25.509 [vent.ItemStateChangedEvent] - MOTOR_1_SWITCH changed from NULL to /etc/openhab2/scripts/ 1: /etc/openhab2/scripts/ import: not found
/etc/openhab2/scripts/ 2: /etc/openhab2/scripts/ import: not found
/etc/openhab2/scripts/ 3: /etc/openhab2/scripts/ Syntax error: word unexpected (expecting “)”)
/etc/openhab2/scripts/ 1: /etc/openhab2/scripts/ import: not found
/etc/openhab2/scripts/ 2: /etc/openhab2/scripts/ import: not found
/etc/openhab2/scripts/ 3: /etc/openhab2/scripts/ Syntax error: word unexpected (expecting “)”)

Does this mean, the python script isn’t ok? As I wrote already, it works from the commandline.


See How to solve Exec binding problems

The problem likely has to do with the fact that openHAB runs as the openHAB user and you probably installed the python libraries you are using only as your user rather than the system and they are therefore not available to the openhab user. Another explanation could be that the script is expecting to be executed from a specific folder but openHAB will be running the script from its own folder.

Thank you very much for your reply.
The problem is, I’m a linux noob. I copy what I read. So what can I do to check the first part, the python libraries.
I use the openhabian image and install python as user openhabian.
The second I don’t understand. I thought, when I use the full path to the script, nothing can be wrong.

You need to ad the shebang to your Skript.
Place the library files to the right folder of your python installation. Some claims also the Skripts had to be at the same place, but I never had this issue.

But to only control rpi pins you could use the gpio binding.

Fantastic. Shebang it is!. Thank you very very much.
I tried the gpio binding before for testing, but I need a script for a more complex situation.
Again, thank you very much.