『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 30480|回复: 69

[流程系列] 【node-red】彩云天气-支持2个城市分发给hass,包含yaml

  [复制链接]

40

主题

1128

帖子

4252

积分

论坛元老

Rank: 8Rank: 8

积分
4252
金钱
3124
HASS币
0
发表于 2019-5-6 16:49:51 | 显示全部楼层 |阅读模式
本帖最后由 goldstep 于 2019-5-6 17:11 编辑

参考花神的教程帖子https://bbs.hassbian.com/forum.php?mod=viewthread&tid=5345&highlight=%E5%BD%A9%E4%BA%91,稍加修改,彩云天气的key实现2个城市的天气数据推送,
前提
1、要彩云平台支持你所选的两个城市。
2、你在2个地方部署了hass,这2个地方使用frps穿透给一个域名和mqtt服务进行统一管理,细节部分请自行baidu。



结合自己ha的实际应用,整理了一套推送2地天气的流程:
游客,如果您要查看本帖隐藏内容请回复
]

微信图片_20190506162630.png
效果如下:
微信图片_20190506165042.png




代码部分:


[
    {
        "id": "cc405f26.55827",
        "type": "inject",
        "z": "c074f42b.f6c088",
        "name": "每5分钟更新一次天气",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "300",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "x": 250,
        "y": 320,
        "wires": [
            [
                "65adc498.bc16fc",
                "62ae1970.f2d5e8"
            ]
        ]
    },
    {
        "id": "5b139640.329d78",
        "type": "http request",
        "z": "c074f42b.f6c088",
        "name": "实时天气",
        "method": "GET",
        "ret": "obj",
        "url": "[url=https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/realtime.json]https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/realtime.json[/url]",
        "tls": "",
        "x": 520,
        "y": 220,
        "wires": [
            [
                "7a457f00.54ab1"
            ]
        ]
    },
    {
        "id": "8197202e.07adf",
        "type": "debug",
        "z": "c074f42b.f6c088",
        "name": "调试信息",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "true",
        "x": 910,
        "y": 220,
        "wires": []
    },
    {
        "id": "62ae1970.f2d5e8",
        "type": "function",
        "z": "c074f42b.f6c088",
        "name": "北京基础数据",
        "func": "//api key\nmsg.api_key = ''\n//精度\nmsg.jingdu = ''\n//纬度\nmsg.weidu = ''\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 340,
        "y": 180,
        "wires": [
            [
                "670c1d09.039f84",
                "5b139640.329d78"
            ]
        ]
    },
    {
        "id": "670c1d09.039f84",
        "type": "http request",
        "z": "c074f42b.f6c088",
        "name": "天气预报",
        "method": "GET",
        "ret": "obj",
        "url": "[url=https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/forecast.json?unit=metric:v2&alert=true]https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/forecast.json?unit=metric:v2&alert=true[/url]",
        "tls": "",
        "x": 500,
        "y": 60,
        "wires": [
            [
                "1af1c48b.96027b"
            ]
        ]
    },
    {
        "id": "7a457f00.54ab1",
        "type": "function",
        "z": "c074f42b.f6c088",
        "name": "数据处理",
        "func": "var REALTIME_TYPES = {\n 'temperature': ['temperature', '摄氏度','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', '毫克每立方','mdi:blur'],\n 'o3': ['o3', '毫克每立方','mdi:blur'],\n 'co': ['co', '毫克每立方','mdi:blur'],\n 'pm10': ['pm10', '毫克每立方','mdi:blur'],\n 'no2': ['no2', '毫克每立方','mdi:blur'],\n 'pres': ['pres', '帕','mdi:arrow-collapse-down'],\n 'so2': ['so2', '毫克每立方','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\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//降水\nw['nearest_precipitation_distance'] = payload['result']['precipitation']['nearest']['distance'] + '千米'\nw['nearest_precipitation_intensity'] = payload['result']['precipitation']['nearest']['intensity'] + 'mm'\nw['local_precipitation_intensity'] = payload['result']['precipitation']['local']['intensity'] + 'Km/h'\nw['local_datasource'] = payload['result']['precipitation']['local']['datasource']\n\nmsg.payload = w\nmsg.all = payload\n\n\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 680,
        "y": 300,
        "wires": [
            [
                "8197202e.07adf",
                "5c275717.09d878"
            ]
        ]
    },
    {
        "id": "1af1c48b.96027b",
        "type": "function",
        "z": "c074f42b.f6c088",
        "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': ['千米每小时'],\n 'wind_max': ['千米每小时'],\n 'temperature_min': ['摄氏度'],\n 'temperature_max': ['摄氏度'],\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] + REALTIME_TYPES[key][0]\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": 700,
        "y": 120,
        "wires": [
            [
                "8197202e.07adf",
                "b37ba7eb.a6b268"
            ]
        ]
    },
    {
        "id": "5c275717.09d878",
        "type": "mqtt out",
        "z": "c074f42b.f6c088",
        "name": "mqtt发送北京天气",
        "topic": "/node-red/caiyun/realtimebj",
        "qos": "0",
        "retain": "false",
        "broker": "be6fbf37.e5d2b",
        "x": 930,
        "y": 340,
        "wires": []
    },
    {
        "id": "b37ba7eb.a6b268",
        "type": "mqtt out",
        "z": "c074f42b.f6c088",
        "name": "mqtt发送北京天气",
        "topic": "/node-red/caiyun/forecastbj",
        "qos": "0",
        "retain": "",
        "broker": "be6fbf37.e5d2b",
        "x": 970,
        "y": 80,
        "wires": []
    },
    {
        "id": "ab020489.1205d8",
        "type": "http request",
        "z": "c074f42b.f6c088",
        "name": "实时天气",
        "method": "GET",
        "ret": "obj",
        "url": "[url=https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/realtime.json]https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/realtime.json[/url]",
        "tls": "",
        "x": 480,
        "y": 540,
        "wires": [
            [
                "c22fba76.a16058"
            ]
        ]
    },
    {
        "id": "2a68a664.f7a97a",
        "type": "debug",
        "z": "c074f42b.f6c088",
        "name": "调试信息",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 840,
        "y": 580,
        "wires": []
    },
    {
        "id": "d376a8a4.651f38",
        "type": "http request",
        "z": "c074f42b.f6c088",
        "name": "天气预报",
        "method": "GET",
        "ret": "obj",
        "url": "[url=https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/forecast.json?unit=metric:v2&alert=true]https://api.caiyunapp.com/v2/{{{api_key}}}/{{{jingdu}}},{{{weidu}}}/forecast.json?unit=metric:v2&alert=true[/url]",
        "tls": "",
        "x": 500,
        "y": 360,
        "wires": [
            [
                "27c0fc6e.0c0b44"
            ]
        ]
    },
    {
        "id": "c22fba76.a16058",
        "type": "function",
        "z": "c074f42b.f6c088",
        "name": "数据处理",
        "func": "var REALTIME_TYPES = {\n 'temperature': ['temperature', '摄氏度','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', '微克每立方','mdi:blur'],\n 'o3': ['o3', '微克每立方','mdi:blur'],\n 'co': ['co', '微克每立方','mdi:blur'],\n 'pm10': ['pm10', '微克每立方','mdi:blur'],\n 'no2': ['no2', '微克每立方','mdi:blur'],\n 'pres': ['pres', '帕','mdi:arrow-collapse-down'],\n 'so2': ['so2', '微克每立方','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\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//降水\nw['nearest_precipitation_distance'] = payload['result']['precipitation']['nearest']['distance'] + '千米'\nw['nearest_precipitation_intensity'] = payload['result']['precipitation']['nearest']['intensity'] + 'mm'\nw['local_precipitation_intensity'] = payload['result']['precipitation']['local']['intensity'] + 'Km/h'\nw['local_datasource'] = payload['result']['precipitation']['local']['datasource']\n\nmsg.payload = w\nmsg.all = payload\n\n\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 640,
        "y": 640,
        "wires": [
            [
                "2a68a664.f7a97a",
                "e19afc09.09f6d"
            ]
        ]
    },
    {
        "id": "27c0fc6e.0c0b44",
        "type": "function",
        "z": "c074f42b.f6c088",
        "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': ['千米每小时'],\n 'wind_max': ['千米每小时'],\n 'temperature_min': ['摄氏度'],\n 'temperature_max': ['摄氏度'],\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] + REALTIME_TYPES[key][0]\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": 680,
        "y": 480,
        "wires": [
            [
                "2a68a664.f7a97a",
                "c827ce1d.3333"
            ]
        ]
    },
    {
        "id": "e19afc09.09f6d",
        "type": "mqtt out",
        "z": "c074f42b.f6c088",
        "name": "mqtt发送武汉天气",
        "topic": "/node-red/caiyun/realtimewh",
        "qos": "0",
        "retain": "false",
        "broker": "be6fbf37.e5d2b",
        "x": 870,
        "y": 720,
        "wires": []
    },
    {
        "id": "c827ce1d.3333",
        "type": "mqtt out",
        "z": "c074f42b.f6c088",
        "name": "mqtt发送武汉天气",
        "topic": "/node-red/caiyun/forecastwh",
        "qos": "0",
        "retain": "",
        "broker": "be6fbf37.e5d2b",
        "x": 910,
        "y": 480,
        "wires": []
    },
    {
        "id": "65adc498.bc16fc",
        "type": "function",
        "z": "c074f42b.f6c088",
        "name": "上海基础数据",
        "func": "//api key\nmsg.api_key = ''\n//精度\nmsg.jingdu = ''\n//纬度\nmsg.weidu = ''\n\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 320,
        "y": 480,
        "wires": [
            [
                "d376a8a4.651f38",
                "ab020489.1205d8"
            ]
        ]
    },
    {
        "id": "be6fbf37.e5d2b",
        "type": "mqtt-broker",
        "z": "",
        "name": "mqtt",
        "broker": "192.168.0.124",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]




评分

参与人数 1金钱 +20 收起 理由
+ 20 NR火到爆啊

查看全部评分

回复

使用道具 举报

8

主题

2073

帖子

6053

积分

论坛元老

流水无味

Rank: 8Rank: 8

积分
6053
金钱
3980
HASS币
145

灌水之王

发表于 2019-5-6 18:59:31 | 显示全部楼层
我有强迫症
看见你的图的线条我想打死你
回复

使用道具 举报

27

主题

558

帖子

4376

积分

论坛元老

Rank: 8Rank: 8

积分
4376
金钱
3818
HASS币
0
发表于 2019-5-6 22:20:20 | 显示全部楼层
谢谢分享
回复

使用道具 举报

123

主题

4661

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16410
金钱
11664
HASS币
45
发表于 2019-5-6 22:44:15 | 显示全部楼层
NR这么火,HA表示压力很大
回复

使用道具 举报

2

主题

166

帖子

2777

积分

金牌会员

Rank: 6Rank: 6

积分
2777
金钱
2611
HASS币
0
发表于 2019-5-7 14:56:22 | 显示全部楼层
谢谢分享。最近都研究NR了。
回复

使用道具 举报

20

主题

700

帖子

3106

积分

论坛元老

Rank: 8Rank: 8

积分
3106
金钱
2406
HASS币
0
发表于 2019-5-8 21:13:30 | 显示全部楼层
这个不错哦
回复

使用道具 举报

0

主题

32

帖子

208

积分

中级会员

Rank: 3Rank: 3

积分
208
金钱
176
HASS币
0
发表于 2019-5-10 12:40:41 | 显示全部楼层
学习一下
回复

使用道具 举报

10

主题

155

帖子

773

积分

论坛积极会员

积分
773
金钱
618
HASS币
0
发表于 2019-5-10 20:42:48 | 显示全部楼层
谢谢分享
回复

使用道具 举报

13

主题

100

帖子

321

积分

中级会员

Rank: 3Rank: 3

积分
321
金钱
221
HASS币
0
发表于 2019-5-10 22:15:01 | 显示全部楼层
多试试看
回复

使用道具 举报

0

主题

60

帖子

193

积分

严重灌水用户

积分
193
金钱
133
HASS币
0
发表于 2019-5-12 10:14:20 | 显示全部楼层
感谢楼主分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-21 18:31 , Processed in 1.408539 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表