跳转至

彩云天气

目前彩云天气应该是用过里面数据最好的天气了, 这里给大家提供一个node red 版本的天气,初步在ha中可以显示正常的数据

node_red_caiyun

节点配置

首先需要到基础数据中修改自己的 经纬度和mqtt 即可使用
node_red_caiyun

[{"id":"f3171c00.36549","type":"tab","label":"彩云天气","disabled":false,"info":""},{"id":"dde147e5.f6a5a8","type":"inject","z":"f3171c00.36549","name":"每5分支执行一次","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":false,"onceDelay":0.1,"x":202,"y":136,"wires":[["273cbceb.a2f5e4"]]},{"id":"eae4d31a.e02af","type":"http request","z":"f3171c00.36549","name":"实时天气","method":"GET","ret":"obj","url":"https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/realtime.json","tls":"","x":385,"y":178,"wires":[["d263d804.dca208"]]},{"id":"2b390237.36056e","type":"debug","z":"f3171c00.36549","name":"调试信息","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":590,"y":26,"wires":[]},{"id":"273cbceb.a2f5e4","type":"function","z":"f3171c00.36549","name":"基础数据","func":"//api key\nmsg.api_key = 'Y2FpeXVuIGFwaSB3ZWI'\n//精度\nmsg.jingdu = '120.271605'\n//纬度\nmsg.weidu = '30.388969'\n\n\nreturn msg;","outputs":1,"noerr":0,"x":191,"y":239,"wires":[["fdbc1a14.c40668","eae4d31a.e02af"]]},{"id":"fdbc1a14.c40668","type":"http request","z":"f3171c00.36549","name":"天气预报","method":"GET","ret":"obj","url":"https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/forecast.json?unit=metric:v2&alert=true","tls":"","x":366,"y":254,"wires":[["d440c608.bc41e8"]]},{"id":"d263d804.dca208","type":"function","z":"f3171c00.36549","name":"数据处理","func":"var REALTIME_TYPES = {\n 'temperature': ['temperature', '°C','mdi:thermometer'],\n 'skycon': ['skycon', '',''],\n 'cloudrate': ['cloudrate', '%','mdi:weather-partlycloudy'],\n 'aqi': ['AQI', ' ','mdi:cloud-outline'],\n 'humidity': ['humidity', '%','mdi:water-percent'],\n 'pm25': ['pm25', 'μg/m3','mdi:blur'],\n 'o3': ['o3', 'μg/m3','mdi:blur'],\n 'co': ['co', 'μg/m3','mdi:blur'],\n 'pm10': ['pm10', 'μg/m3','mdi:blur'],\n 'no2': ['no2', 'μg/m3','mdi:blur'],\n 'pres': ['pres', 'Pa','mdi:arrow-collapse-down'],\n 'so2': ['so2', 'μg/m3','mdi:blur'],\n\n}\n\nvar SKYCON_TYPE = {\n 'CLEAR_DAY':'晴天',\n 'CLEAR_NIGHT':'晴夜',\n 'PARTLY_CLOUDY_DAY':'多云',\n 'PARTLY_CLOUDY_NIGHT':'多云',\n 'CLOUDY':'阴',\n 'RAIN':'雨',\n 'SNOW':'雪',\n 'WIND':'风',\n 'FOG':'雾',\n 'HAZE':'雾霾',\n}\n\n\nfunction direction_chinese(direction){\n if (direction > 337.4 || direction < 22.5)\n final_direction = '北风'\n else if (direction > 22.4 && direction < 67.5)\n final_direction = '东北风'\n else if ( direction > 67.4 && direction < 112.5)\n final_direction = '东风'\n else if ( direction > 112.4 && direction < 157.5)\n final_direction = '东南风'\n else if ( direction > 157.4 && direction < 202.5)\n final_direction = '南风'\n else if ( direction > 202.4 && direction < 247.5)\n final_direction = '西南风'\n else if ( direction > 247.4 && direction < 292.5)\n final_direction = '西风'\n else if ( direction > 292.4 && direction < 337.5)\n final_direction = '西北风'\n else\n final_direction = '无数据'\n return final_direction\n}\n\nvar payload = msg.payload\n\nvar w = {}\n\nfor(var key in REALTIME_TYPES) {\n var value = payload['result'][key]\n if (value > 0) {\n \n }else {\n \n }\n w[key] = payload['result'][key] //+ REALTIME_TYPES[key][1]\n}\n\nw['humidity'] = w['humidity'] * 100\n\n////天气概况\nw['skycon'] = SKYCON_TYPE[w['skycon']]\n////风\nw['wind_direction'] = direction_chinese(payload['result']['wind']['direction'])\nw['wind_speed'] = payload['result']['wind']['speed'] //+ 'Km/h'\n//降水\nif (payload['result']['precipitation']) {\n    w['nearest_precipitation_distance'] = payload['result']['precipitation']['nearest']['distance'] + 'Km/h'\n    w['nearest_precipitation_intensity'] = payload['result']['precipitation']['nearest']['intensity'] + 'mm'\n}else {\n    w['nearest_precipitation_distance'] = \"暂无\"\n    w['nearest_precipitation_intensity'] =\"暂无\"\n}\n\nif (payload['result']['precipitation']) {\n    w['local_precipitation_intensity'] = payload['result']['precipitation']['local']['intensity'] + 'Km/h'\n    w['local_datasource'] = payload['result']['precipitation']['local']['datasource']\n}else {\n    w['local_precipitation_intensity'] = \"暂无\"\n    w['local_datasource'] = \"暂无\"\n}\n\n\nmsg.payload = w\nmsg.all = payload\n\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":588,"y":168,"wires":[["2b390237.36056e","9e274c47.1b60c"]]},{"id":"d440c608.bc41e8","type":"function","z":"f3171c00.36549","name":"数据处理","func":"var data_forecast = msg.payload\n\nvar REALTIME_TYPES = {\n 'probability_30': ['%'],\n 'probability_60': ['%'],\n 'probability_90': ['%'],\n 'probability_120': ['%'],\n 'probability_3h': ['%'],\n 'probability_4h': ['%'],\n 'wind_min': ['Km/h'],\n 'wind_max': ['Km/h'],\n 'temperature_min': ['°C'],\n 'temperature_max': ['°C'],\n\n}\n\nvar w = {}\nvar c = {} //不带单位,用来做自动化\n\n//描述\nc['description'] = data_forecast['result']['minutely']['description']\n//降雨率\nvar probability = data_forecast['result']['minutely']['probability']\nc['probability_30'] = (probability[0] * 100).toFixed(2) \nc['probability_60'] = (probability[1] * 100).toFixed(2) \nc['probability_90'] = (probability[2] * 100).toFixed(2) \nc['probability_120'] = (probability[3] * 100).toFixed(2)\n//\nvar probability_4h = data_forecast['result']['minutely']['probability_4h']\nif (probability_4h) {\n c['probability_3h'] = (probability_4h[2] * 100).toFixed(2)\n c['probability_4h'] = (probability_4h[3] * 100).toFixed(2)\n}else {\n c['probability_3h'] = '您的API_KEY不支持该参数[probability_3h]'\n c['probability_4h'] = '您的API_KEY不支持该参数[probability_4h]'\n REALTIME_TYPES['probability_3h'][0] = ''\n REALTIME_TYPES['probability_4h'][0] = ''\n}\n//风\nvar wind = data_forecast['result']['daily']['wind']\n//温度\nvar temperature = data_forecast['result']['daily']['temperature']\n\nc['wind_min'] = wind[0]['min']['speed']\nc['wind_max'] = wind[0]['max']['speed']\n\nc['temperature_min'] = temperature[0]['min']\nc['temperature_max'] = temperature[0]['max']\n\nfor(var key in REALTIME_TYPES) {\n w[key] = c[key]\n}\n\nvar alert_content = data_forecast['result']['alert']['content']\nif (alert_content && alert_content.lenght > 0) {\n w['alert'] = alert_content[0]['description']\n w['alert_source'] = alert_content[0]['source']\n w['alert_county'] = alert_content[0]['county']\n}else {\n w['alert'] = '暂无预警'\n w['alert_source'] = '暂无预警'\n w['alert_county'] = '暂无预警'\n}\n\nmsg.payload = w\nmsg.c = c\nmsg.all = data_forecast\nreturn msg;","outputs":1,"noerr":0,"x":586,"y":258,"wires":[["2b390237.36056e","4e0c02e8.3092cc"]]},{"id":"9e274c47.1b60c","type":"mqtt out","z":"f3171c00.36549","name":"mqtt发送","topic":"node-red/caiyun/realtime","qos":"0","retain":"false","broker":"a10a8571.a17148","x":847,"y":174,"wires":[]},{"id":"4e0c02e8.3092cc","type":"mqtt out","z":"f3171c00.36549","name":"mqtt发送","topic":"node-red/caiyun/forecast","qos":"0","retain":"","broker":"a10a8571.a17148","x":836,"y":264,"wires":[]},{"id":"cc3060bb.aa63e","type":"mqtt in","z":"f3171c00.36549","name":"","topic":"node-red/homeassistant-setup","qos":"2","broker":"a10a8571.a17148","x":196,"y":324,"wires":[["273cbceb.a2f5e4"]]},{"id":"a10a8571.a17148","type":"mqtt-broker","z":"","name":"N1","broker":"192.168.3.10","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"node-red/server/status","birthQos":"0","birthPayload":"在线","closeTopic":"node-red/server/status","closeQos":"0","closePayload":"关闭","willTopic":"node-red/server/status","willQos":"0","willPayload":"不可用"}]

HA 配置

我的ha配置都是基于 packages, 所以第一步需要检查是否开启了,如下图:
node_red_caiyun

homeassistant:
  packages: !include_dir_named packages
  # Name of the location where Home Assistant is running
  name: Home

注意: 如果有提示自动化配置错误的请保证你的 automations.yaml 最少有一个自动化配置, 如果没有可以把下面信息填进去
原因: 好像是HA的bug, 因为我在彩云天气里面添加了一个HA启动的时候通知一下 node red 这样可以保证一启动就能看到数据

# automations.yaml
- alias: 'xxx'
  trigger:
  - platform: homeassistant
    event: start
  condition:
    condition: time
    after: '07:00:00'
    before: '22:00:00'   
  action:
    - delay:
        seconds: 1                      

  • forecast_weather.yaml

    sensor:
      - platform: mqtt
        name: "caiyun_temperature_min"
        friendly_name: "最低温度"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.temperature_min }}"
        unit_of_measurement: '°C'
        device_class: 'temperature'
      - platform: mqtt
        name: "caiyun_temperature_max"
        friendly_name: "最高温度"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.temperature_max }}"
        unit_of_measurement: '°C'
        device_class: 'temperature'
      - platform: mqtt
        name: "caiyun_wind_max"
        friendly_name: "最高风速"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.wind_max }}"
        unit_of_measurement: 'Km/h'
      - platform: mqtt
        name: "caiyun_wind_min"
        friendly_name: "最低风速"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.wind_min }}"
        unit_of_measurement: 'Km/h'
      - platform: mqtt
        name: "caiyun_probability_30"
        friendly_name: "30分钟降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_30 }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_probability_60"
        friendly_name: "60分钟降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_60 }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_probability_90"
        friendly_name: "90分钟降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_90 }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_probability_120"
        friendly_name: "120分钟降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_120 }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_probability_3h"
        friendly_name: "3小时降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_3h }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_probability_4h"
        friendly_name: "4小时钟降雨率"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.probability_4h }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_alert_source"
        friendly_name: "预警来源"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.alert_source }}"
      - platform: mqtt
        name: "caiyun_alert"
        friendly_name: "预警消息"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.alert }}"
      - platform: mqtt
        name: "caiyun_alert_county"
        friendly_name: "预警城市"
        state_topic: "node-red/caiyun/forecast"
        value_template: "{{ value_json.alert_county }}"
    
    homeassistant:
      customize:
        sensor.caiyun_temperature_min:
          friendly_name: 最低温度
        sensor.caiyun_temperature_max:
          friendly_name: 最高温度
        sensor.caiyun_wind_max:
          friendly_name: 最高风速
        sensor.caiyun_wind_min:
          friendly_name: 最低风速
        sensor.caiyun_probability_30:
          friendly_name: 30分钟降雨率
        sensor.caiyun_probability_60:
          friendly_name: 60分钟降雨率
        sensor.caiyun_probability_90:
          friendly_name: 90分钟降雨率
        sensor.caiyun_probability_120:
          friendly_name: 120分钟降雨率
        sensor.caiyun_probability_3h:
          friendly_name: 3小时降雨率
        sensor.caiyun_probability_4h:
          friendly_name: 4小时钟降雨率
        sensor.caiyun_alert_source:
          friendly_name: 预警来源
        sensor.caiyun_alert:
          friendly_name: 预警消息
        sensor.caiyun_alert_county:
          friendly_name: 预警城市
    
    
    group:
    
      caiyunMinutely:
        name: 彩云分钟天气
        view: no
        entities:
          - sensor.caiyun_temperature_min
          - sensor.caiyun_temperature_max
          - sensor.caiyun_wind_max
          - sensor.caiyun_wind_min
          - sensor.caiyun_probability_30
          - sensor.caiyun_probability_60
          - sensor.caiyun_probability_90
          - sensor.caiyun_probability_120
          - sensor.caiyun_probability_3h
          - sensor.caiyun_probability_4h
          - sensor.caiyun_alert_source
          - sensor.caiyun_alert
          - sensor.caiyun_alert_county
    

  • realtime_weather.yaml

    sensor:
      - platform: mqtt
        name: "caiyun_temperature"
        friendly_name: "温度"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.temperature }}"
        unit_of_measurement: '°C'
        device_class: 'temperature'
      - platform: mqtt
        name: "caiyun_skycon"
        friendly_name: "彩云Skycon"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.skycon }}"
      - platform: mqtt
        name: "caiyun_cloudrate"
        friendly_name: "云层覆盖率"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.cloudrate }}"
        unit_of_measurement: '%'
      - platform: mqtt
        name: "caiyun_aqi"
        friendly_name: "空气质量指数"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.aqi }}"
      - platform: mqtt
        name: "caiyun_humidity"
        friendly_name: "湿度"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.humidity }}"
        unit_of_measurement: '%'
        device_class: 'humidity'
      - platform: mqtt
        name: "caiyun_pm10"
        friendly_name: "PM1.0"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.pm10 }}"
        unit_of_measurement: '°μg/m3'
      - platform: mqtt
        name: "caiyun_pm25"
        friendly_name: "PM2.5"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.pm25 }}"
        unit_of_measurement: 'μg/m3'
      - platform: mqtt
        name: "caiyun_o3"
        friendly_name: "臭氧"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.o3 }}"
        unit_of_measurement: 'μg/m3'
      - platform: mqtt
        name: "caiyun_co"
        friendly_name: "一氧化碳"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.co }}"
        unit_of_measurement: 'μg/m3'
      - platform: mqtt
        name: "caiyun_so2"
        friendly_name: "二氧化硫"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.so2 }}"
        unit_of_measurement: 'μg/m3'
      - platform: mqtt
        name: "caiyun_no2"
        friendly_name: "二氧化氮"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.no2 }}"
        unit_of_measurement: 'μg/m3'
      - platform: mqtt
        name: "caiyun_pres"
        friendly_name: "大气压"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.pres }}"
        unit_of_measurement: 'Pa'
      - platform: mqtt
        name: "caiyun_nearest_precipitation_distance"
        friendly_name: "附近降雨距离"
        state_topic: "node-red/caiyun/realtime"
        value_template: "{{ value_json.nearest_precipitation_distance }}"
        unit_of_measurement: 'Km/h'
    
    
    
    homeassistant:
      customize:
        sensor.caiyun_temperature:
          friendly_name: 温度
        sensor.caiyun_skycon:
          friendly_name: 彩云Skycon
        sensor.caiyun_cloudrate:
          friendly_name: 云层覆盖率
        sensor.caiyun_aqi:
          friendly_name: 空气质量指数
        sensor.caiyun_humidity:
          friendly_name: 湿度
        sensor.caiyun_pm10:
          friendly_name: PM1.0
        sensor.caiyun_pm25:
          friendly_name: PM2.5
        sensor.caiyun_o3:
          friendly_name: 臭氧
        sensor.caiyun_co:
          friendly_name: 一氧化碳
        sensor.caiyun_so2:
          friendly_name: 二氧化硫
        sensor.caiyun_no2:
          friendly_name: 二氧化氮
        sensor.caiyun_pres:
          friendly_name: 大气压
        sensor.caiyun_nearest_precipitation_distance:
          friendly_name: 附近降雨距离
    
    
    group:
      caiyunweather:
        name: 彩云天气
        view: yes
        icon: mdi:weather-partlycloudy
        entities:
          - group.caiyunRealtime
          - group.caiyunMinutely
    
    
    
      caiyunRealtime:
        name: 彩云即时天气
        view: no
        entities:
          - sensor.caiyun_temperature
          - sensor.caiyun_skycon
          - sensor.caiyun_cloudrate
          - sensor.caiyun_aqi
          - sensor.caiyun_humidity
          - sensor.caiyun_pm10
          - sensor.caiyun_pm25
          - sensor.caiyun_o3
          - sensor.caiyun_co
          - sensor.caiyun_so2
          - sensor.caiyun_no2
          - sensor.caiyun_pres
          - sensor.caiyun_nearest_precipitation_distance
    
    
    automation:
      - alias: 'homeassistant_setup'
        trigger:
        - platform: homeassistant
          event: start
        action:
          - delay:
              seconds: 10                  
          - service: mqtt.publish
            data:
              topic: 'node-red/homeassistant-setup'
              payload: 'setup'