Executing python script, reading a value(temperature) and importing the value to openHAB

Hello guys,

I’m pretty new to openHab and I’m working for a study project on a raspberry pi. I have a problem and I’m pretty sure it’s a little tiny problem what drives me nuts. I looked out for it on the world wide web. I followed there suggestions and tips, still not working.
As you can see in the topic I want to get a certain value out of my python script and make it visible on the openHab interface.

Here is my home.sitemap:
sitemap home label=“Home”
{
Frame label=“Home”
{
Switch item=TestLED
Text item=TestTemp
Text item=TestText
}
}

and my home.item:
Switch TestLED {gpio=“pin:21”}
Number TestTemp “TestTemperature [%.0f °C] (home) {exec=”<[/usr/bin/python /opt/openhab/configurations/scripts/lm75_TempSensr.py temperature:1000:REGEX((.?))]"}
Text TestText “TestText [%s]” (home) {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/testtext.py:1000:REGEX((.
?))]"}

As you can see in TestText I just wanted display a simple string from a python script. Nothing works. It has to be something very simple I guess.

I hope you can help me. Thanks in advance


Python Script “testtext.py”
#!/usr/bin/python

print “Hello”


Python Script “lm75_TempSensr.py”
#!/usr/bin/python

from subprocess import Popen , PIPE
from time import sleep

SLAVE_ADDR = 0x48
DELAY = 30 #in seconds

def main():
try:
while True:
p = Popen([‘i2cget’ , ‘-y’ , ‘1’ , str(SLAVE_ADDR) , ‘0x00’ , ‘w’] ,
stdout=PIPE)
output = p.stdout.read()
temperature = getTemperature(int(output , 16))

		print "Current temperature is %s degree celsius" % (str(temperature))
		print "Waiting " + str(DELAY) + " seconds..."

		sleep(DELAY)
except KeyboardInterrupt:
	print "Execution canceled by user."

def getTemperature(rawData):
""“
Note: You’ll get data in reverse order from sensor:
|low byte|high byte|
0x80|0E
”""
degrees = rawData & 0xFF #cut high byte off, get low byte000
degreesAfterDecimal = rawData >> 15 #shift msb to lsb place

if (degrees & 0x80) != 0x80:	  #msb in low byte is 0 -> positive temperature
	return degrees + degreesAfterDecimal * 0.5
else:	#msb in low byte is 1 -> negative temperature
	#calc two's complement
	degrees = -((~degrees & 0xFF) + 1)
	return degrees + degreesAfterDecimal * 0.5

if name == “main”:
main()

Ok…i solved by my own xD

I’m trying to delete my post, but no can do.

Thx anyways cheers

Instead of deleting, tell us how you fixed it on your own so others may benefit. Was it a permissions problem? A path problem?

It was that simple, so I guessed it’s not worth mentioning it. But yeah, you are right.

I just changed the keyword to STRING, since my python script uses print as an output.

String TestTemp "TestTemperature [%s °C] <temperature> (home) {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/lm75_TempSensr.py:1000:REGEX((.*?))]"}

That was pretty much it

1 Like