多谢大佬指点。终于可以了。最后的代码如下,供有遇到同样问题的朋友参考。
sensor:
- platform: rest
resource: https://devapi.qweather.com/v7/weather/now?location=经度,纬度&key=YOUR_API_KEY
scan_interval: 1800 #半小时更新一次天气数据
name: weather_now #实时天气
value_template: "{{ value_json['now']['text'] }}"
json_attributes:
- updateTime
- now
- platform: rest
resource: https://devapi.qweather.com/v7/weather/3d?location=经度,纬度&key=YOUR_API_KEY
scan_interval: 7200 #二小时更新一次天气数据
name: weather_daily #3天预报
value_template: "{{ value_json['daily'][0]['textDay'] }}"
json_attributes:
- updateTime
- daily
- platform: template
sensors:
temp_hi_1d:
friendly_name: >- #明天最高温度
明天{% if states('sensor.temp_hi_1d')|int < states('sensor.temp_hi')|int %}↘{% else %}↗{% endif %}{{ (states('sensor.temp_hi_1d')|int - states('sensor.temp_hi')|int)|abs }}
value_template: "{{ states.sensor.weather_daily.attributes.daily[1]['tempMax'] }}"
unit_of_measurement: °C
temp_lo_1d:
friendly_name: >- #明天最低温度
明天{% if states('sensor.temp_lo_1d')|int < states('sensor.temp_lo')|int %}↘{% else %}↗{% endif %}{{ (states('sensor.temp_lo_1d')|int - states('sensor.temp_lo')|int)|abs }}
value_template: "{{ states.sensor.weather_daily.attributes.daily[1]['tempMin'] }}"
unit_of_measurement: °C
template:
- sensor:
- name: current_temperature #当前温度
icon: 'mdi:thermometer'
state: "{{ states.sensor.weather_now.attributes.now['temp'] }}"
unit_of_measurement: °C
- name: current_feelslike #当前体感温度
icon: 'mdi:temperature-celsius'
state: "{{ states.sensor.weather_now.attributes.now['feelsLike'] }}"
unit_of_measurement: °C
- name: temp_hi # 今天最高温度
state: "{{ states.sensor.weather_daily.attributes.daily[0]['tempMax'] }}"
unit_of_measurement: °C
- name: temp_lo # 今天最低温度
state: "{{ states.sensor.weather_daily.attributes.daily[0]['tempMin'] }}"
unit_of_measurement: °C
- name: wind_1d #明天风力
state: "{{ (( states.sensor.weather_daily.attributes.daily[1]['windSpeedDay'] |int *1000/3600/0.835) ** (1/1.5)) | round(1) }}"
unit_of_measurement: Bf
# 风速(米/秒)转蒲福风级:Bf = ( windSpeed(m/s) / 0.835 ) ^ (1/1.5)
# 参见:https://baike.baidu.com/item/%E8%92%B2%E7%A6%8F%E9%A3%8E%E7%BA%A7/2763752
|