You could create two python scripts. One for temperature and one for the humidity, and then use the Command Line Sensor. With the command line sensor you will be able to run a command and get the “result” in hass.
That examble gets you the CPU temperature in hass. Instead of the cat /sys… you could run your python script python ./humidity.py and it would show the humidity.
If the script requires sudo, you will have to add the home assistant user to sudo by following this guide. Remember to explicit select what sudo command home assistant is allowed to run.
Start with the DHT example you already found here.
I know it’s pretty hacky, and I wish I was more skilled so I would be able to help you better.
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi.GPIO as GPIO
import dht22
import time
import datetime
# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()
# read data using pin 14
instance = dht22.DHT22(pin=PIN2)
while True:
result = instance.read()
if result.is_valid():
print("%.2f" % result.temperature)
time.sleep(1)
which outputs (continually)
19.60
19.70
19.60
19.60
19.60
…
Even though the yaml is correct (passed the check), my hass webui isn’t coming up anymore.
In the hass log it’s only written:
2018-01-20 11:58:13 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform command_line is taking over 10 seconds.
but the script is running (when I check with htop - with root privileges)
any ideas?
Your command should return a value that will be used by hass, not continually print it to console output.
Since your python script doesn’t return any value and keep running, hass might be blocked on the script.
Try to just get the value 1 time, and exit the script.
I went with this code now (using sys.exit(1) in the end):
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys
# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()
# read data using pin 14
instance = dht22.DHT22(pin=PIN2)
while True:
result = instance.read()
if result.is_valid():
print("%.2f" % result.temperature)
sys.exit(1)
At it returns on line:
20.20
before it closes
I tried to use your cleaner code:
while result.is_valid() is False:
result = instance.read()
print("%.2f" % result.temperature)
but I get this error as output
Traceback (most recent call last):
File “dht22_temp.py”, line 20, in
while result.is_valid() is False:
NameError: name ‘result’ is not defined
I have the problem now that the value in hass stays empty (-)
my yaml:
# DHT Sensor via command_line
- platform: command_line
name: DHT Temperature
command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
unit_of_measurement: "°C"
value_template: '{{ value|multiply(0.001)|round }}'
coming up without problems.
My /DHT22-Python-library-Orange-PI/dht22_temp.py is like this:
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys
# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()
# read data using pin 14
instance = dht22.DHT22(pin=PIN2)
while True:
result = instance.read()
if result.is_valid():
print("%.2f" % result.temperature)
sys.exit(1)
an outputs a proper value when called (python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py)
Yes, using sys.exit(1) might be wrong, try with sys.exit(0) instead.
The code 1 signal an execution error, 0 is the default code. Sorry for giving the wrong code ^^
So here is my sum up (important is that I’m running hass as root - if you don’t you maybe add some sudo commands and add your hass user to the sudo group!)
Last valid input: 2018-01-18 18:30:45.093912
Temperature: 20.10 C
Humidity: 59.00 %
In this folder you are (DHT22-Python-library-Orange-PI) you create a new file called dht22_temp.py adding this code:
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys
# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()
# read data using pin 14
instance = dht22.DHT22(pin=PIN2)
result = instance.read()
while result.is_valid() is False:
result = instance.read()
print("%.2f" % result.temperature)
And another file called dht22_hum.py adding this code:
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys
# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()
# read data using pin 14
instance = dht22.DHT22(pin=PIN2)
result = instance.read()
while result.is_valid() is False:
result = instance.read()
print("%.2f" % result.humidity)
Last but not least you need to touch your configuriatons.yaml adding this:
# DHT Sensor via command_line: Temperature
- platform: command_line
name: DHT Temperature
command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
unit_of_measurement: "°C"
value_template: '{{ value }}'
# DHT Sensor via command_line: Humidity
- platform: command_line
name: DHT Humidity
command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_hum.py"
unit_of_measurement: "%"
value_template: '{{ value }}'