Python script with executecommandline

I have a python script that works fine from the command line but doesnt when calling from executecommandline.

Hoping someone can help

command:

strCommand = "/usr/bin/python@@/opt/openhab/configurations/scripts/lifxstate.py"
executeCommandLine(strCommand, 5000)

the script::

    #!/usr/bin/env python
import requests
import time

token = ''mytokenhere''

headers = {
    "Authorization": "Bearer %s" % token,
}

def restore_state(state):
    selector = 'id:%s' % state['id']

    # Color is sent as one string, so we have to convert it
    color_items = state['color'].items()
    color_strings = map(lambda x:'%s:%s' % x, color_items)

    data = {
        'power': state['power'],
        'brightness': state['brightness'],
        'color': ' '.join(color_strings),
    }

    response = requests.put('https://api.lifx.com/v1/lights/%s/state' % selector, data=data, headers=headers)

def fetch_states(selector='all'):
    response = requests.get('https://api.lifx.com/v1/lights/' + selector, headers=headers)
    
    return response.json()

print 'Storing the current state of the bulbs'
orig_states = fetch_states()

print 'Waiting 10 seconds, change the color of the bulbs now'
time.sleep(10)

print 'Restoring lights to the state they were 10 seconds ago'
for state in orig_states:
    restore_state(state)

Change your rule code to log out what the script is returning:

val results = executeCommandLine(strCommand, 5000)
logInfo("exec", results)

If I had to guess the problem may be with the #!/usr/bin/env python. The user openHAB runs under doesn’t have a shell so evn may not be able to run.

1 Like

I did try that and the log shows a loginfo entry with no return value

Any idea what I should be putting in the top of the script?

Things to try:

  • I am pretty sure you don’t need the “@@” for spaces when using executeCommandLine. And even if you did, I don’t think it is needed in this case.
  • make sure python is in /usr/bin
  • replace the line at the top with #!/usr/bin/python

I had this same issue.

I read a suggestion somewhere to write a shell script which just contains the command to run the python script. So something like:

#!/bin/sh
/usr/bin/python /opt/openhab/configurations/scripts/lifxstate.py

Save that lifxstate.sh.

Then chmod +x lifxstate.sh to make it executable.

Now try

executeCommandLine("/opt/openhab/configurations/scripts/lifxstate.sh")

No idea why this works, but it does for me.

So I dropped it back to a 1 liner:

executeCommandLine("/usr/bin/python@@/opt/openhab/configurations/scripts/lifxstate.py")

is now working for me.

Pretty sure the @@ are needed for spaces in executeCommandLine syntax

I have a number of executeCommandLine calls, all of which have spaces, none of which use @@. So I can say it is not always required and in my experience not required as often as it is when using the Exec binding.

executeCommandLine("chmod a+x /etc/openhab/configurations/scripts/resetGarageSensorReporter.sh", 5000)
executeCommandLine("chmod go-r /var/lib/openhab/.ssh/authorized_keys2 /var/lib/openhab/.ssh/id_rsa /var/lib/openhab/.ssh/id_rsa.pub /var/lib/openhab/.ssh/known_hosts /var/lib/openhab/.ssh/known_hosts.old", 5000)
executeCommandLine("/etc/openhab/configurations/scripts/resetGarageSensorReporter.sh", 5000)
executeCommandLine("/etc/openhab/configurations/scripts/searchRokus.py", 20000)

OK now I haev it all working - I will pull them out and see if it still does - would certainly make the code tidier. Perhaps Im getting confused with curl commands.