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

**URL:** <https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251>\
**Category:** Scripts & Rules\
**Tags:** python, openhab, temperature\
**Created:** [November 25, 2016, 2:19pm UTC](https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251 "2016-11-25T14:19:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe911](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Joe911](https://community.openhab.org/u/Joe911)\
**Post date:** [November 25, 2016, 2:19pm UTC](https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251/1 "2016-11-25T14:19:04Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![Joe911](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Joe911](https://community.openhab.org/u/Joe911)\
**Post date:** [November 25, 2016, 2:55pm UTC](https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251/2 "2016-11-25T14:55:15Z")

</div>

Ok…i solved by my own xD

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

Thx anyways cheers

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 27, 2016, 5:17am UTC](https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251/3 "2016-11-27T05:17:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joe911](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Joe911](https://community.openhab.org/u/Joe911)\
**Post date:** [November 28, 2016, 11:57am UTC](https://community.openhab.org/t/executing-python-script-reading-a-value-temperature-and-importing-the-value-to-openhab/17251/4 "2016-11-28T11:57:04Z")

</div>

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.

```csv
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
