Problem Reading Temperature

To get the temperature, I connected a DHT-22 to my Raspi and created the following python script:

import board
import adafruit_dht
 
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
 
try:
    # Print the values to the serial port
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(
         "{:.1f} ".format(temperature_c)
    )
 
except RuntimeError as error:
    # Errors happen fairly often, DHT's are hard to read, just keep going
    print(error.args[0])
except Exception as error:
     dhtDevice.exit()
     raise error

The script works fine (returns something like 22.7). Afterwards, I tried to add getting the temperature to OpenHAB by adding the following item:

UID: exec:command:7ee1e9d524
label: TemperaturAbfragen
thingTypeUID: exec:command
configuration:
  transform: REGEX((.*))
  interval: 5
  autorun: true
  command: python3 /home/openhabian/temperature/temperature.py
  timeout: 15
location: Wohnzimmer

Additionally, I added an item TemperaturWohnzimmer as point which has a channel TemperaturAbfragen which takes the return value using the standard profile. Unfortunately, this always returns NULL.

Can anyone give me a hint how to configure this correctly? (Or how to export the item/channel code to post it here, I did not find a way to do so in the GUI).

Another way for me seems to wrap the temperature as JSON, like this:

import board
import adafruit_dht
 
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
 
try:
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(
         "{\"temperature\":" + str(temperature_c) + ",\"humidity\":" + str(humidity) + "}"
    )
 
except RuntimeError as error:
    print(error.args[0])
except Exception as error:
     dhtDevice.exit()
     raise error

and then get the results in the item via $.temperature. Unfortunately, this also does not get anything.

so you use the exec binding ? Did you configure exec.whitelist to allow the python3 command being executed/whitelisted ?

1 Like

Thank you for the hint, that was the problem! Now the temperature was read, but only three times. /var/log/openhab/events.log looks like this:

2022-09-22 22:00:45.725 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TemperaturAbfragen_Ruckgabewert' changed from NULL to 22.7 °C
2022-09-22 22:00:51.197 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TemperaturAbfragen_Ruckgabewert' changed from 22.7 °C to 22.6 °C
2022-09-22 22:00:56.691 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TemperaturAbfragen_Ruckgabewert' changed from 22.6 °C to 22.7 °C
2022-09-22 22:05:39.885 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'exec:command:7ee1e9d524' changed from ONLINE to UNINITIALIZED
2022-09-22 22:05:39.902 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'exec:command:7ee1e9d524' changed from UNINITIALIZED to UNINITIALIZED (DISABLED)
2022-09-22 22:05:41.392 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'exec:command:7ee1e9d524' changed from UNINITIALIZED (DISABLED) to INITIALIZING
2022-09-22 22:05:41.407 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'exec:command:7ee1e9d524' changed from INITIALIZING to ONLINE

Also, as the logs show, disabling and enabling the item did not solve the issue. The python script works as before. Do you maybe also have an idea how to solve this?

  • add something to your python script that e.g. adds a log entry in /tmp/ to see if the script really isn’t started
  • I assume that you would like to read the temperature every 5 seconds ?
  • autorun then needs to be set to false. true requires a trigger ( input parameter ) that needs to be changed every time the command shall be triggered; same input trigger will not trigger the command

Extract from the docs:

autorun - A boolean parameter to make the command execute immediately every time the input channel is sent a different openHAB command. If choosing autorun, you may wish to also set interval=0. Note that sending the same command a second time will not trigger execution.

1 Like

Thanks for the hints. Yes, I want the temperature to be read every 5 seconds for debugging (later, I’ll set it to 60 seconds). I’ve set autorun to false, this did not change the behaviour.

Additionally, I tried writing a temporary file:

import board
import adafruit_dht
from datetime import datetime

dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
 
try:
    dateTimeObj = datetime.now()
    f = open("/home/openhabian/demofile2.txt", "a")
    f.write("Now the file has more content!" + str(dateTimeObj.microsecond) + "\n")
    f.close()
 
    # Print the values to the serial port
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(
         "{:.1f} ".format(temperature_c)
    )

except RuntimeError as error:
    # Errors happen fairly often, DHT's are hard to read, just keep going
    print(error.args[0])
except Exception as error:
     dhtDevice.exit()
     raise error

This works when calling the script from the command line, but openhab does not seem to call it, so there is nothing added additionally.

has write permissions for user openhab ( OH runs with openhab user privileges ) ?
/home/openhabian implies that this might not be the case.

1 Like

Based on your hint, I changed the permissions with chmod 777 demofile2.txt. After adding some debug output, it turns out the temperature seems to stay constant at 22.7 degree celsius:

import board
import adafruit_dht
from datetime import datetime

dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
 
dateTimeObj = datetime.now()
date_time = dateTimeObj.strftime("%m/%d/%Y, %H:%M:%S")
try:
    # Print the values to the serial port
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(
         "{:.1f} ".format(temperature_c)
    )
    f = open("/home/openhabian/demofile2.txt", "a")
    f.write(date_time + " - Successfully read: " + str(temperature_c) + "\n")
    f.close()

 
except RuntimeError as error:
    # Errors happen fairly often, DHT's are hard to read, just keep going
    print(error.args[0])
    f = open("/home/openhabian/demofile2.txt", "a")
    f.write(date_time + " - A RuntimeError happened: " + error.args[0] + "\n")
    f.close()
except Exception as error:
    f = open("/home/openhabian/demofile2.txt", "a")
    f.write(date_time + " - An Exception happened: " + error.args[0] + "\n")
    f.close()
    dhtDevice.exit()
    raise error

While this seem suspicios to me, at least thats clearly not an OpenHAB issue. Thanks for the hints!

1 Like

Unfortunately, I was too fast. The log is fine for real:

09/22/2022, 23:15:22 - A RuntimeError happened: Checksum did not validate. Try again.
09/22/2022, 23:15:33 - Successfully read: 22.7
09/22/2022, 23:15:43 - Successfully read: 22.7
09/22/2022, 23:15:54 - A RuntimeError happened: A full buffer was not returned. Try again.
09/22/2022, 23:16:04 - Successfully read: 22.7
09/22/2022, 23:16:15 - Successfully read: 22.7
09/22/2022, 23:16:25 - Successfully read: 22.7
09/22/2022, 23:16:36 - A RuntimeError happened: Checksum did not validate. Try again.
09/22/2022, 23:16:46 - Successfully read: 22.7
09/22/2022, 23:16:57 - Successfully read: 22.6
09/22/2022, 23:17:07 - Successfully read: 22.9
09/22/2022, 23:17:18 - Successfully read: 23.4
09/22/2022, 23:17:28 - Successfully read: 24.1
09/22/2022, 23:17:39 - Successfully read: 24.5
09/22/2022, 23:17:49 - Successfully read: 24.8

But unfortunately, OpenHAB does not update the values.
This is how the channel looks like:


Which seems fine to me.

Looking to your code

I would try to remove the space between the closing brace and the double quote.