Hello,
I am running the following python script on a raspi and try to integrate it in HA without success:
import time
import Adafruit_DHT as dht
import board
import adafruit_sht4x
import paho.mqtt.client as mqtt
Sensor1 = dht.DHT22
Pin = 4
Sensor2 = adafruit_sht4x.SHT4x(board.I2C())
broker_address = "192.168.178.60"
mqtt_topic = "home/pi_zero"
QOS = 1
username = "mqtt-user"
password = "xxxx"
#Client Instanz erstellen
client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
print("Verbunden mit MQTT Broker mit Code: " + str(rc))
client.subscribe(topic)
def publish_message(Temp1, Phi1, Temp2, Phi2):
client.publish(mqtt_topic + "/temp1", str(Temp1), QOS)
client.publish(mqtt_topic + "/phi1", str(Phi1), QOS)
client.publish(mqtt_topic + "/temp2", str(Temp2)), QOS
client.publish(mqtt_topic + "/phi2", str(Phi2), QOS)
client.on_connect = on_connect
client.username_pw_set(username, password)
client.connect(broker_address, 1883, 60)
while True:
try:
Phi1, Temp1 = dht.read_retry(Sensor1, Pin)
except Exception as e:
print('Fehler Sensor1: ', e)
try:
Temp2, Phi2 = Sensor2.measurements
except Exception as e:
print('Fehler Sensor2: ', e)
publish_message(Temp1, Phi1, Temp2, Phi2)
time.sleep(900)
The yaml integration looks like:
mqtt:
sensor:
- name: "Temperatur Wohnzimmer"
state_topic: "home/pi_zero/temp1"
unit_of_measurement: "°C"
- name: "rel. Feuchte Wohnzimmer"
state_topic: "home/pi_zero/phi1"
unit_of_measurement: "%"
- name: "Temperatur Keller"
state_topic: "home/pi_zero/temp2"
unit_of_measurement: "°C"
- name: "rel. Feuchte Keller"
state_topic: "home/pi_zero/phi2"
unit_of_measurement: "%"
In the mosquitto protocoll I see the following:
2023-12-30 22:23:23: New connection from 192.168.178.58:47579 on port 1883.
2023-12-30 22:23:23: New client connected from 192.168.178.58:47579 as auto-C9BEFD6D-38C3-3A73-704D-D46300696119 (p2, c1, k60, u’mqtt-user’).
Can anybody identify my mistake?
Thanks a lot!
J