我将JSON数据从python脚本发送到Home助手。HA接收数据,但我无法获得我的模板来正确读取它。我看不到我缺少的东西-我想访问JSON中的温湿度特性。
Python客户端发布脚本(Python2.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/climateHA configuration.yaml
- platform: mqtt
state_topic: 'rpi3/sensors/climate'
name: 'RPi3 Climate'
value_template: '{{ value_json.climate }}'HA开发模板
Data: {{ states('sensor.rpi3_climate') }}
-> OUTPUTS: Data: {'temperature': '9', 'humidity': '7'}
Temperature: {{ state_attr('sensor.rpi3_climate', "temperature") }}
-> OUTPUTS: Temperature: NoneHA开发模板替代
{% 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: 发布于 2021-10-28 19:41:36
好吧,为了回答我自己的问题.我想出了一些我认为的工作,但我确信我遗漏了一些东西,应该能够访问以原始格式发送的数据。
我更改了正在发送的JSON消息的格式:
msg_json = {
"temperature": str(temperature_f),
"humidity": str(humidity_f)
}并编辑了我的configuration.yaml,这样湿度和温度就成了自己的实体:
- platform: mqtt
state_topic: 'rpi3/sensors/climate'
name: 'RPi3 Climate'
- platform: mqtt
state_topic: 'rpi3/sensors/climate'
name: 'RPi3 Temperature'
value_template: '{{ value_json.temperature }}'
- platform: mqtt
state_topic: 'rpi3/sensors/climate'
name: 'RPi3 Humidity'
value_template: '{{ value_json.humidity }}'然后我就能用传感器的数据制作出一张卡片。
https://stackoverflow.com/questions/69709845
复制相似问题