Exec Binding does not set number item

I try to read ADC values from SPI on raspberry PI 2 with OpenHAB.

I have the following item. This item should execute a python script, which should have one input parameter and one return value. The return value should set the moisture value from the item Moisture_Tomatoes. the input parameter is to select the SPI channel from the python script :
Number Moisture_Tomatoes “M: [%d]” (Moisture) {exec="[/usr/bin/python /opt/openhab/configurations/scripts/mcp3008_read_channel.py 0:60000:REGEX((.*?))]"}

and here is the script. Depending on the SPI channel it should return the ADC value:
#!/usr/bin/python
import spidev
import sys

spi = spidev.SpiDev()
spi.open(0,0)
channel = int(sys.argv[1])
adc = spi.xfer2([1,(8+channel)<< 4,0])
data = ((adc[1]&3) << 8) + adc[2]
spi.close()
sys.exit(data)

So the item does not update after 60 seconds. Its zero all the time.
Is there a problem with the python script (return value) or is the execution command wrong?

If i execute the python script with a terminal (insteed of sys.exit(data) i print the values) it works fine.

I’m stuck. Thanks for any help.

Hi Marc,
what is the output in the terminal if you execute

“/usr/bin/python /opt/openhab/configurations/scripts/mcp3008_read_channel.py 0”

?

Cheers
Björn

Thx for your answer.

If i execute the following command in terminal i don’t have any feedback:
"/usr/bin/python /opt/openhab/configurations/scripts/mcp3008_read_channel.py 0"

But when i change the “sys.exit(data)” with “print data” i have a feedback (the ADC value is printed on terminal), but nothing happen, if i execute the script with openHAB.

Is it wrong with print? Do i have to make a rule, which change the state of the Moisture value?

cheers
Marc

Hi Marc,
ok. Print is ok then, i.e. use print. You do see only the value, correct?
Next thing is the following:
In sitemaps file create a text item:
"Text item=Moisture_Tomatoes"
If you look in your UI, do you see anything now?
Cheers
Björn
PS: Check also your access permissions of your file.

Hey Björn

Yes, i already have the item Moistur_Tomatoes in my sitemap and its shown in the UI. In the rules i initialize the value at start up with zero, and the value does not change.

rule "Init Items"
when
System started
then
// Update moisture values
Moisture_Tomatoes.postUpdate(0)
end

Ah yes, i changed the permission to 0755. Thats was wrong. but it is still zero.

I also include the persistence in addons dir (org.openhab.persistence.exec-1.8.1.jar) Do i have to activate the exec persistence in the config file?

any other ideas?

thx

Do i need the exec.persist file in the persistence folder?

Hi Marc,
Normally you should not need a rule.
The text item should change every 60000ms acc. to your exec item.
Is your output eventually a string?
Cheers
Björn

I tried to change the value to a string -> [%s] but nothing happen.

Than i tried to set a fix return value.

“print 100” is now the last line, nothing happen.

When i start openhab with the terminal, i cant see any errors, so only infos are shown.

Cheers

Hi Mark,

Suggestion,try the following:

  1. Change python script only to “print 100”, no input parameters or anything else required.

  2. Grant permissions to your script.

  3. Check, that when executing the script via terminal only “100” is displayed.

  4. Change exec command in items file from
    "Number Moisture_Tomatoes “M: [%d]” (Moisture) {exec="[/usr/bin/python /opt/openhab/configurations/scripts/mcp3008_read_channel.py 0:60000:REGEX((.?))]"}“
    to
    Number Moisture_Tomatoes “M: [%.0f]” (Moisture) { exec=”<[/usr/bin/python /opt/openhab/configurations/scripts/mcp3008_read_channel.py:600000:REGEX((.
    ?))]" }

Commend: I changed
a) %d to %.0f
b) added an “<” after "exec="
c) removed the input “0” after “…py”

  1. Add item in sitemap file:
    “Text item=Moisture_Tomatoes”

Check whether it is working.
Hope this helps.
Cheers
Björn

Hey Björn

I tried your suggestion, but without success. The Item is still not changing.
If i perform the python script in the terminal, it gives my 100 back.

I implemented exactly like u said. but i m not sure about the permissions.
I set the permission to 0777 recursivly to the directory configurations and all the childs.
The group and owner is pi[1000]. Is it better to set it to root[0]?

Does the binding need root access to the system?

No my preferences in openhab are : USER_AND_GROUP=openhab:openhab
does i require root?

Cheers
Marc

Does the user that runs the openHAB server (usually the user openhab) have permission to access the hardware that your script uses, the same permissions you have at the command prompt? Also, are there environment variables set in your environment that would be needed by the different user under which the openHAB server is running?

Hi Marc,
I agree with watou, thus I proposed to write only “print 100” in you python file.
Further suggestion:
Move your python script to the openhab directory and execute it there.
Cheers
Björn

I changed now the permission from the openhab user identical like the pi (current user) :
pi ALL = NOPASSWD: ALL
openhab ALL = NOPASSWD: ALL

I tested the execution of the python script command as the openhab user with:
sudo -u openhab (and i get back 100 - (print 100))

After that i saw that i included the exec persistence insteed of the exec binding. With the exec binding it works perfectly.

I also try the python script with a parameter. It works too.

Thanks guys for your help.

Cheers
marc