我将 JSON 数据从 python 脚本发送到 Home Assistant。HA 收到数据,但我无法让我的模板正确读取它。我看不到我缺少什么——我想访问 JSON 中的温度和湿度属性。
Python 客户端发布脚本 (Python 2.7.16)
msg_json = {
"climate": {
"temperature": str(temperature_f),
"humidity": str(humidity_f)
}
}
result = client.publish(topic, payload=json.dumps(msg_json))
status = result[0]
print("Send {0} to topic {1}").format(msg_json, topic)
// OUTPUT -> Send {'climate': {'temperature': '9', 'humidity': '7'}} to topic rpi3/sensors/climate
HA 配置.yaml
- platform: mqtt
state_topic: 'rpi3/sensors/climate'
name: 'RPi3 Climate'
value_template: '{{ value_json.climate }}'
高可用性开发模板
Data: {{ states('sensor.rpi3_climate') }}
-> OUTPUTS: Data: {'temperature': '9', 'humidity': '7'}
Temperature: {{ state_attr('sensor.rpi3_climate', "temperature") }}
-> OUTPUTS: Temperature: None
HA 开发模板替代方案
{% set temp = states("sensor.rpi3_climate") %}
{% set climate_json = temp|to_json %}
The temperature is: {{ climate_json }}
-> OUTPUTS: The temperature is: "{'temperature': '9', 'humidity': '7'}"
The temperature is: {{ climate_json.temperature }}
-> OUTPUTS: The temperature is: