What i want to do is add some sensors that are not officially supported to hass.
My sensor is DHT11, which is a temperature&humidity sensor. I used Dupont line to connect this sensor to the raspberry Pi, and used mqtt protocol to transfer this data from raspberry pi to hass in an another machine. I succeeded with binary sensor. I used HC-SR04, which is a ultrasonic sensor, to get the distance and if distance > 100 return true, else return false to hass. Then defined a binary sensor named"distance" in configuration.yaml, and defined a rule in automation.yaml, if the binary sensor is on then turn on the light. This scenario succeeded. So i want to extended this scenario, not only the binary sensor, but also more complex data types except boolean.
Here is my code:
This is my binary sensor part and it’s successful.
binary_sensor:
- platform: mqtt
state_topic: “/garage/csb”
name: “distance”
device_class: occupancy
payload_on: “true”
payload_off: “false”
value_template: “{{ value }}”
This is the sensor part which i want to ask how to define:
sensor:
- platform: mqtt
state_topic: “/garage/wsd”
name: “lj-wsd”
device_class: occupancy
And in raspberry pi, there is a python file to control DHT11 and publish the data to the topic.
#!/usr/bin/python
import RPi.GPIO as GPIO
import timechannel = 16
data =
j = 0MQTT_SERVER = “192.168.1.158”
MQTT_SERVER_PORT = 1883
MQTT_TOPIC = “/garage/wsd”
MQTT_USERNAME = “hcq”
MQTT_PASSWORD = “201393127”GPIO.setmode(GPIO.BCM)
time.sleep(1)
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)GPIO.setup(channel, GPIO.IN)
while GPIO.input(channel) == GPIO.LOW:
continuewhile GPIO.input(channel) == GPIO.HIGH:
continuewhile j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continuewhile GPIO.input(channel) == GPIO.HIGH: k += 1 if k > 100: break if k < 8: data.append(0) else: data.append(1) j += 1
print “sensor is working.”
print datahumidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0mqttc = mqtt.Mosquitto()
#Assign event callbacks
mqttc.on_connect = on_connect#Uncomment to enable debug messages
#mqttc.on_log = on_log#Connect
mqttc.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
mqttc.connect(MQTT_SERVER, MQTT_SERVER_PORT)for i in range(8):
humidity += humidity_bit[i] * 2 ** (7 - i)
humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
temperature += temperature_bit[i] * 2 ** (7 - i)
temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
check += check_bit[i] * 2 ** (7 - i)tmp = humidity + humidity_point + temperature + temperature_point
if check == tmp:
print "temperature : ", temperature, ", humidity : " , humidity
else:
print “wrong”
print "temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmpGPIO.cleanup()
What i want to know is: 1, how to define sensor in configuration.yaml? As sensor may return numerical data except boolean, the definition maybe different from binary sensor; 2, how to publish numerical data like temperature or humidity with mqtt in python?