本帖最后由 nontrace 于 2020-11-1 10:33 编辑
今天天猫精灵接入设备数量权限终于开放了。
但是我的温湿度传感器天猫精灵无法读取,每次问天猫精灵都传感器无法获取温湿度数据。
我是通过python脚本获取的温湿度数据然后通过mqtt接入的,Python脚本如下
import paho.mqtt.client as mqtt
import time
import Adafruit_DHT
# 设置传感器类型
sensor=Adafruit_DHT.DHT11
# 设置GPIO引脚
gpio=17
# mqtt客户端
client = mqtt.Client()
# 链接上服务器后回调
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
# 设置链接成功后回调
client.on_connect = on_connect
# 链接mqtt服务器
client.connect("localhost", 1883, 60)
time.sleep(1)
# 注册温度传感器设备
client.publish('homeassistant/sensor/temperatureraspberrypi/config',
payload='{"name":"temperatureraspberrypi","unique_id":"temperatureraspberrypi","state_topic":"dht11/state/dht11raspberrypi","value_template":"{{value_json.temperature}}","device_class":"temperature","unit_of_measurement":"°C"}',
qos=0, retain=True)
# 注册湿度传感器设备
client.publish('homeassistant/sensor/humidityraspberrypi/config',
payload='{"name":"humidityraspberrypi","unique_id":"humidityraspberrypi","state_topic":"dht11/state/dht11raspberrypi","value_template":"{{value_json.humidity}}","device_class":"humidity","unit_of_measurement":"%"}',
qos=0, retain=True)
time.sleep(60)
while True:
# 读取温度与湿度
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
# 要发送的json字符串
payloadStr='{"temperature":"'+str(temperature)+'","humidity":"'+str(humidity)+'"}'
print(payloadStr)
client.loop(3)
# 发送到mqtt
client.publish('dht11/state/dht11raspberrypi', payload=payloadStr, qos=0, retain=True)
time.sleep(60)
已经可以正常接入ha,也可以正常显示,也接入了天猫精灵,但是就是天猫精灵无法读取。请指点
|