How to add a sensor (not a binary sensor) in configuration.yaml?

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 time

channel = 16
data =
j = 0

MQTT_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:
continue

while GPIO.input(channel) == GPIO.HIGH:
continue

while j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continue

while 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 data

humidity_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 = 0

mqttc = 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 : ", tmp

GPIO.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?

Please format your code properly by using the < / > button. DHT11 is supported by HA:

As for the MQTT sensor, check the docs as well

You need to use the paho-mqtt module. In particular, to publish single readings intermittently it is probably easier to use single rather than maintain a connection to the broker permanently. The payload becomes the sensor state, so its easiest if you make this a string before you transmit the data.

Hello, I want to plug a Ultrasonic Distance Sensor to my raspberry and get the value in HA.
I try to add the same code as in esphome in the configuration.yaml but it say " Integration ‘ultrasonic’ not found"

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"