本帖最后由 book123 于 2023-2-25 21:42 编辑  
 
我想用esp32,独立于HA系统运行,用http_request:代码读取openweathermap的当日天气信息,但是怎么也读取不了。 
浏览器能够看到json格式的信息,如下: 
{
        "coord": {
                "lon": 117.12,
                "lat": 36.65
        },
        "weather": [{
                "id": 804,
                "main": "Clouds",
                "description": "overcast clouds",
                "icon": "04d"
        }],
        "base": "stations",
        "main": {
                "temp": 2.66,
                "feels_like": 0.68,
                "temp_min": 2.66,
                "temp_max": 2.66,
                "pressure": 1040,
                "humidity": 23,
                "sea_level": 1040,
                "grnd_level": 1020
        },
        "visibility": 10000,
        "wind": {
                "speed": 1.97,
                "deg": 114,
                "gust": 2.42
        },
        "clouds": {
                "all": 97
        },
        "dt": 1676946193,
        "sys": {
                "country": "CN",
                "sunrise": 1676933624,
                "sunset": 1676973423
        },
        "timezone": 28800,
        "id": 1805753,
        "name": "Jinan",
        "cod": 200
} 
  
esphome代码如下:
esp32:
  board: wemos_d1_mini32 #esp32dev
  framework:
    type: arduino
sensor:
  - platform: template
    name: "Air temperature"
    id: air_temp
http_request:
  useragent: esphome/esp32dev
  id: http_request_data
time:
  - platform: sntp
    id: sntp_time
    on_time:
      # Every 5 minutes 获取openweathermap的当日天气信息
      - seconds: 0
        minutes: /5
        then:
          - http_request.get:
              url: http://api.openweathermap.org/data/2.5/weather?lat=36.65&lon=117.12&appid=***************&units=metric
              headers:
                Content-Type: application/json
              verify_ssl: false
              on_response:
                then:
                  - lambda: |-
                      json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
                        id(air_temp).publish_state(root["main"]["temp"]);
                      }); 
  
编译和上传都正常。联网正常,显示时间正常。但是就是air_temp没有数值。 
请各位大佬指教,哪里出了问题。 
==================================================================== 
后经多方查阅资料,已经解决,代码如下: 
http_request: 
  ### OPENWEATHERMAP 
  useragent: esphome/device 
  timeout: 10s 
  id: http_request_data 
 
sensor: 
  ### OPENWEATHERMAP 
  # 室外温度 
  - platform: template 
    name: "Weather Temperature" 
    id: weather_temperature0 
  # 室外湿度 
  - platform: template 
    name: "Weather Humidity" 
    id: weather_humidity0 
  # 室外风速 
  - platform: template 
    id: outside_wind_speed0 
 
 
 
text_sensor: 
  ### OPENWEATHERMAP 
  # 当日天气 
  - platform: template 
    name: "Weather Condition0" 
    id: weather_condition0 
  # 当日天气描述-中文 
  - platform: template 
    name: "Weather Condition0" 
    id: weather_condition0_c 
 
 
 
time: 
  ### INTERNET TIME 
  - platform: sntp 
    id: sntp_time 
 
interval: 
    ### OPENWEATHERMAP 
  - interval: 5min 
    then: 
      - http_request.get: 
          # 调用 5 天 / 3 小时预测数据 
          url: http://api.openweathermap.org/data/2.5/forecast?lat=****&lon=****&appid=****************&lang=zh_cn&units=metric 
          on_response: 
            then: 
              - lambda: |- 
                  json::parse_json(id(http_request_data).get_string(), [](JsonObject doc) { 
                    auto listf = doc["list"]; 
 
                    float tempf0 = listf[0]["main"]["temp"]; 
                    float humf0 = listf[0]["main"]["humidity"]; 
                    float winspeedf0 = listf[0]["wind"]["speed"]; 
 
                    id(weather_temperature0).publish_state(tempf0); 
                    id(weather_humidity0).publish_state(humf0); 
                    id(outside_wind_speed0).publish_state(winspeedf0); 
 
                    const char* weatherdesc0 = listf[0]["weather"][0]["main"]; 
                    const char* weatherdesc0_c = listf[0]["weather"][0]["description"]; 
                    if (weatherdesc0) id(weather_condition0).publish_state(weatherdesc0); 
                    if (weatherdesc0_c) id(weather_condition0_c).publish_state(weatherdesc0_c); 
                  }); 
 
 
 
 |