How to show DHT22 stats on HAB panel


(Normen) #1

Hello, what do I need to do to see the temperature and humidity displayed on the openHAB panel?

item:

Number Temp "Temperature [%.2f   C]" <temperature> {exec="<[/usr/bin/python2 /opt/openhab2/conf/scripts/temp.py:10000:REGEX((.*?))]"}

script:

    import sys
    import Adafruit_DHT
    sensor = Adafruit_DHT.DHT22
    pin = 18
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
         print('{0:0.2f}'.format(temperature))
    else:
         print('Failed to get reading. Try again!')
         sys.exit(1)

I used a dummy panel with server-provided format but the panel keeps empty.
Does anyone see an issue on this? The script is working.

greetings


(Lucky) #2

Can you please add code fences to your post so that it’s easier to read?

Also show your logs. Do the logs actually say the values are being set correctly to the item ?


(Josar) #3

@Mando your code looks as it is Openhab1 syntax, if you want to use that you need the legacy support enabled in paperUI.

You need at least to have exec binding and Regex transformation installed to get this to work.

Using the Openhab2 Binding, find some info here. But use paper UI to install all Binding and Transformation instead karaf as explained, because there is an issue with Karaf installed bindings get uninstalled in regular intervalls.

or


(Normen) #4

OK…how would it look if I use openhab2 syntax? :smiley:

Next try with things:

things:

Thing exec:command:hum [command="/usr/bin/python2  /opt/openhab2/conf/scripts/hum.py", interval=15, autorun=true]
Thing exec:command:temp [command="/usr/bin/python2  /opt/openhab2/conf/scripts/temp.py", interval=15, autorun=true]

Items:

Number Temp "Temperature [%.2f C]" <temperature> {channel="exec:command:temp:exit"}
Number Hum "Humidity [%d %%]" <humidity> {channel="exec:command:hum:exit"}

Aaaaand…nothing :frowning:
Humidity shows me 1% and temperature 1.00 °C

Log:

2018-02-12 14:27:52.820 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'Traceback (most recent call last):'

2018-02-12 14:27:52.830 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/opt/openhab2/conf/scripts/temp.py", line 11, in <module>'

2018-02-12 14:27:52.832 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)'

2018-02-12 14:27:52.833 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 94, in read_retry'

2018-02-12 14:27:52.835 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 80, in read'

2018-02-12 14:27:52.838 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 51, in get_platform'

2018-02-12 14:27:52.840 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 22, in <module>'

2018-02-12 14:27:52.841 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py", line 7, in <module>'

2018-02-12 14:27:52.842 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py", line 4, in __bootstrap__'

2018-02-12 14:27:52.844 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1198, in resource_filename'

2018-02-12 14:27:52.845 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    self, resource_name'

2018-02-12 14:27:52.847 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1710, in get_resource_filename'

2018-02-12 14:27:52.848 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    self._extract_resource(manager, self._eager_to_zip(name))'

2018-02-12 14:27:52.850 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1740, in _extract_resource'

2018-02-12 14:27:52.851 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    self.egg_name, self._parts(zip_path)'

2018-02-12 14:27:52.852 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1265, in get_cache_path'

2018-02-12 14:27:52.854 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    self.extraction_error()'

2018-02-12 14:27:52.855 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1245, in extraction_error'

2018-02-12 14:27:52.857 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    raise err'

2018-02-12 14:27:52.858 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'pkg_resources.ExtractionError: Can't extract file(s) to egg cache'

2018-02-12 14:27:52.860 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.861 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'The following error occurred while trying to extract file(s) to the Python egg'

2018-02-12 14:27:52.863 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'cache:'

2018-02-12 14:27:52.864 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.866 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  [Errno 13] Permission denied: '/home/openhab''

2018-02-12 14:27:52.868 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.869 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'The Python egg cache directory is currently set to:'

2018-02-12 14:27:52.871 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.872 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  /home/openhab/.cache/Python-Eggs'

2018-02-12 14:27:52.874 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.875 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'Perhaps your account does not have write access to this directory?  You can'

2018-02-12 14:27:52.877 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'change the cache directory by setting the PYTHON_EGG_CACHE environment'

2018-02-12 14:27:52.878 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'variable to point to an accessible directory.'

2018-02-12 14:27:52.880 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-02-12 14:27:52.886 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is 'Traceback (most recent call last):

(rbausdorf) #5

Seams be an permission problem. Please ensure that the user openhab is able to execute the script and is member of the GPIO group.


(Josar) #6

@Mando

Try to read both threads carefully! I see common mistakes adressed there.

  1. Can you execute the command as usr openhab/openhabian?
sudo -u openhab /usr/bin/python2  /opt/openhab2/conf/scripts/hum.py
sudo -u openhab /usr/bin/python2  /opt/openhab2/conf/scripts/temp.py

Post the result here then we can help you to get it to run.
If it does not, you need to give your user the permission he needs. I think at least

sudo adduser openhab gpio
  1. You want the output channel.
  2. You can not assigne the output channel which is a string to a Number. do it like this.
String Temp "Temperature [%.2f C]" <temperature> {channel="exec:command:temp:output"}
String Hum "Humidity [%d %%]" <humidity> {channel="exec:command:hum:output"}

(Normen) #7

Openhab is already in the group gpio. Here is my result:

Traceback (most recent call last):
  File "/opt/openhab2/conf/scripts/hum.py", line 11, in <module>

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 94, in read_retry

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 80, in read

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 51, in get_platform

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 22, in <module>

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py", line 7, in <module>

  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py", line 4, in __bootstrap__

  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1198, in resource_filename

    self, resource_name

  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1710, in get_resource_filename

    self._extract_resource(manager, self._eager_to_zip(name))

  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1740, in _extract_resource

    self.egg_name, self._parts(zip_path)

  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1265, in get_cache_path

    self.extraction_error()

  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1245, in extraction_error

    raise err

pkg_resources.ExtractionError: Can't extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg

cache:

  [Errno 13] Permission denied: '/home/openhab'

The Python egg cache directory is currently set to:

  /home/openhab/.cache/Python-Eggs

Perhaps your account does not have write access to this directory?  You can

change the cache directory by setting the PYTHON_EGG_CACHE environment

variable to point to an accessible directory.

Update: The folder didn’t exist. I create it and change the permission for openhab to 755. Now I can use the script as the user openhab. Unfortunately its still not working on the panel. Log says:

==> /opt/openhab2/userdata/logs/openhab.log <==

2018-02-12 19:06:28.637 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'Traceback (most recent call last):'

2018-02-12 19:06:28.648 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "/opt/openhab2/conf/scripts/temp.py", line 11, in <module>'

2018-02-12 19:06:28.649 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)'

2018-02-12 19:06:28.651 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 94, in read_retry'

2018-02-12 19:06:28.653 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 81, in read'

2018-02-12 19:06:28.654 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read'


2018-02-12 19:06:28.656 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: 'RuntimeError: Error accessing GPIO.'

2018-02-12 19:06:28.660 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is 'Traceback (most recent call last):
  File "/opt/openhab2/conf/scripts/temp.py", line 11, in <module>
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 94, in read_retry
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 81, in read
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
RuntimeError: Error accessing GPIO.
Traceback (most recent call last):
  File "/opt/openhab2/conf/scripts/temp.py", line 11, in <module>
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 94, in read_retry
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 81, in read
  File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
RuntimeError: Error accessing GPIO.'

(Josar) #8

@Mando what hardware do you use? Please post what is returned for those command, after you have rebooted the PI.

cat /etc/group
cat /etc/passwd
ls -la /sys/class/gpio/export
ls -la /sys/class/gpio
ls -l /dev/gpiomem

Maybe try to compare to

Also post the content of one of your scripts. Do you have the shebang? Then you would not need the /opt/bin/python2 i think. Maybe this thread helps! So did you install the files to the folder python2.7 as described there?


(Normen) #9
import sys
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 18
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
     print('{0:0.2f}'.format(temperature))
else:
     print('Failed to get reading. Try again!')
     sys.exit(1)

(Josar) #10

@Mando please fist make the code fence and then paste your code. If you pste the code and then mark it and add the code fence it will be splitt up in multiple lines. Feel free to correct your post.


(Josar) #11

@Mando

  1. have you copied the files to the python distribution?
    Please list
ls -l /usr/local/lib/python2.7/dist-packages/
  1. try to execute python from the environment.
/usr/bin/env python2

When this works add the shebang to your script, first line.

#!/usr/bin/env python2.7
import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 18

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
    print('{0:0.2f}'.format(temperature))
else:
    print('Failed to get reading. Try again!')
    sys.exit(1)

and execute this, please post the result.
EDIT:removed dot.

sudo -u openhab /opt/openhab2/conf/scripts/hum.py

Which user and rights for executeCommandLine?
Running a python script when Channel triggered
(Normen) #12
pi@raspberrypi:~ $ sudo -u openhab ./opt/openhab2/conf/scripts/hum.py
sudo: ./opt/openhab2/conf/scripts/hum.py: Befehl nicht gefunden

The files are in the dist-packages folder too.

root@raspberrypi:/home/pi# /usr/bin/env python2
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 


(Josar) #13

Sorry my fault. Remove the dot. And make sure this is the path to your skript.

sudo -u openhab /opt/openhab2/conf/scripts/hum.py

(Normen) #14
pi@raspberrypi:~ $ sudo -u openhab /opt/openhab2/conf/scripts/hum.py
30.30
pi@raspberrypi:~ $ 

Looks good…


(Josar) #15

Then just continue as described here

Or here

Does it work now?


(Normen) #16

It works now :smiley:
I forgot to change the “Number” to “String”.

Thank you so much for your help. So This is my solution now:

items:

String Temp "Temperature [%.2f C]" <temperature> {channel="exec:command:temp:output"}
String Hum "Humidity [%d %%]" <humidity> {channel="exec:command:hum:output"}

scripts:
temp.py


#!/usr/bin/env python2.7

import sys

import Adafruit_DHT



sensor = Adafruit_DHT.DHT22

pin = 18



humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)



if humidity is not None and temperature is not None:

    print('{0:0.2f}'.format(temperature))

else:

    print('Failed to get reading. Try again!')

    sys.exit(1)

hum.py

#!/usr/bin/env python2.7
import sys

import Adafruit_DHT



sensor = Adafruit_DHT.DHT22

pin = 18

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:

     print('{0:0.2f}'.format(humidity))

else:

     print('Failed to get reading. Try again!')

     sys.exit(1)

Things:

Thing exec:command:hum [command="/usr/bin/python2  /opt/openhab2/conf/scripts/hum.py", interval=15, autorun=true]

Thing exec:command:temp [command="/usr/bin/python2  /opt/openhab2/conf/scripts/temp.py", interval=15, autorun=true]


[WARN ] [eclipse.jetty.servlet.ServletHandler] after installing a DHT22
Problems using DHT22 on the same Openhab Pi
(Josar) #17

@Mando i read out the DS18B20 with oneWire directly without sipt, this should also work with the DHT22. I will test it as soon as the module i ordered arrives.