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

 找回密码
 立即注册
查看: 352|回复: 7

[技术讨论] 查询油价并在调价前一天推送调价信息到微信

[复制链接]

14

主题

70

帖子

406

积分

论坛分享达人

积分
406
金钱
336
HASS币
0
发表于 2024-5-23 23:41:34 | 显示全部楼层 |阅读模式
本帖最后由 gl695133087 于 2024-5-24 00:23 编辑

最近一直在折腾openwrt和homeassistant,昨天突然想起之前在github看到过一个获取最新油价的帖子,我也想试试,找了很多帖子,至于参考了哪些大佬的,我记不得了,这里希望大佬们见谅,接入ha部分做了一定调整,增加了直接显示92汽油油价的一个传感;微信推送部分的代码完全是本人用gpt一路摸索写的。微信推送使用的是AnPush,每天有30条免费推送,够用了。AnPush具体的大家自行去搜,不会的认真研究下文档。下面直接上干货—nodered流代码。
[
    {
        "id": "8098bb5720f117e3",
        "type": "ha-sensor",
        "z": "de66179675455cb6",
        "name": "最新油价",
        "entityConfig": "97bd4f8f.bee1f",
        "version": 0,
        "state": "state",
        "stateType": "msg",
        "attributes": [
            {
                "property": "'92#汽油'",
                "value": "price[1]",
                "valueType": "msg"
            },
            {
                "property": "95#汽油",
                "value": "price[3]",
                "valueType": "msg"
            },
            {
                "property": "98#汽油",
                "value": "price[5]",
                "valueType": "msg"
            },
            {
                "property": "0#柴油",
                "value": "price[7]",
                "valueType": "msg"
            },
            {
                "property": "Tips",
                "value": "tips",
                "valueType": "msg"
            }
        ],
        "inputOverride": "allow",
        "outputProperties": [],
        "x": 755,
        "y": 360,
        "wires": [
            []
        ],
        "l": false
    },
    {
        "id": "3b688f94218a4435",
        "type": "http request",
        "z": "de66179675455cb6",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://www.qiyoujiage.com/sichuan",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 320,
        "y": 280,
        "wires": [
            [
                "334c4d17e420103e"
            ]
        ]
    },
    {
        "id": "5bbe2854613b3376",
        "type": "inject",
        "z": "de66179675455cb6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "20 00 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 280,
        "wires": [
            [
                "3b688f94218a4435"
            ]
        ]
    },
    {
        "id": "334c4d17e420103e",
        "type": "html",
        "z": "de66179675455cb6",
        "name": "提取",
        "property": "payload",
        "outproperty": "payload",
        "tag": "#youjiaCont> div",
        "ret": "text",
        "as": "single",
        "x": 490,
        "y": 280,
        "wires": [
            [
                "833235cd88485027"
            ]
        ]
    },
    {
        "id": "833235cd88485027",
        "type": "function",
        "z": "de66179675455cb6",
        "name": "",
        "func": "var OilPrice = msg.payload[0].split('\\n');\n// 去除数组中的空元素\nvar filteredArray = OilPrice.filter(function (element) {\n    return element !== '';\n});\n\nvar StateTips = msg.payload[1].split('\\n');\nvar endIndex = StateTips[2].indexOf('。');\nvar TipsTips = StateTips[2].substring(0, endIndex + 1);\n\nvar newmsg = {};\nnewmsg.price = filteredArray;\nnewmsg.state = StateTips[1];\nnewmsg.tips = TipsTips;\nflow.set('newmsg', newmsg); //将access_token储存\nreturn newmsg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 630,
        "y": 280,
        "wires": [
            [
                "8098bb5720f117e3",
                "8e4671326328e5f9",
                "626a76e2505b39c8"
            ]
        ]
    },
    {
        "id": "8e4671326328e5f9",
        "type": "ha-sensor",
        "z": "de66179675455cb6",
        "name": "92#汽油油价",
        "entityConfig": "3e6cb13c5ba04c83",
        "version": 0,
        "state": "price[1]",
        "stateType": "msg",
        "attributes": [
            {
                "property": "92#汽油",
                "value": "price[1]",
                "valueType": "msg"
            },
            {
                "property": "95#汽油",
                "value": "price[3]",
                "valueType": "msg"
            },
            {
                "property": "98#汽油",
                "value": "price[5]",
                "valueType": "msg"
            },
            {
                "property": "0#柴油",
                "value": "price[7]",
                "valueType": "msg"
            },
            {
                "property": "Tips",
                "value": "tips",
                "valueType": "msg"
            },
            {
                "property": "tiaozhendate",
                "value": "state",
                "valueType": "msg"
            }
        ],
        "inputOverride": "allow",
        "outputProperties": [],
        "x": 755,
        "y": 200,
        "wires": [
            []
        ],
        "l": false
    },
    {
        "id": "c521c69735aa60c6",
        "type": "http request",
        "z": "de66179675455cb6",
        "name": "微信推送",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.anpush.com/push/you_anpush_api", //you_anpush_api换成你自己的api,每天免费30次推送,够用了_
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [
            {
                "keyType": "Content-Type",
                "keyValue": "",
                "valueType": "other",
                "valueValue": "application/x-www-form-urlencoded"
            }
        ],
        "x": 1680,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "70b08a1de0c3bf37",
        "type": "function",
        "z": "de66179675455cb6",
        "name": "日期相同推送调价信息到微信AnPush",
        "func": "// @ts-nocheck\nmsg.payload={\n    title: "今日油价",\n    content: flow.get("newmsg").price + ';'+flow.get("newmsg").state+ ';' + flow.get("newmsg").tips,\n    channel: 'your_anpush_channel',\n}\nreturn msg;",//your_anpush_channel换成你在anpush的信息通道代码
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1430,
        "y": 280,
        "wires": [
            [
                "c521c69735aa60c6"
            ]
        ]
    },
    {
        "id": "626a76e2505b39c8",
        "type": "function",
        "z": "de66179675455cb6",
        "name": "对比当前日期和调价前一天日期",
        "func": "var n1 = flow.get("newmsg").state.indexOf('价');//获取“价”所在位置\nvar n2 = flow.get('newmsg').state.indexOf('月');\nvar n3 = flow.get('newmsg').state.indexOf('日');\n\n//获取月\nvar str1 = flow.get('newmsg').state.substring(n1+1,n2);\n//将月份调整为两位数,1位数的十位补0\nif (str1.length === 1) {\n    // 处理条件1的情况\n    str1 = '0'+str1;\n} //else if (input === 'condition2') {\n    // 处理条件2的情况\n//    msg.payload = 'Condition 2 is true';\n//}\n else {\n    // 处理其他情况\n    str1;\n}\n\n//获取日\nvar str2 = flow.get('newmsg').state.substring(n2 + 1, n3)-1;\n//将月份调整为两位数,1位数的十位补0\nif (str2.length === 1) {\n    // 处理条件1的情况\n    str2 = '0' + str2;\n} //else if (input === 'condition2') {\n// 处理条件2的情况\n//    msg.payload = 'Condition 2 is true';\n//}\nelse {\n    // 处理其他情况\n    str2;\n}\n\n//整合油价调整日期\nvar tiaozhendate =str1+'月'+str2+'日';\n\n// 获取当前日期和时间\nvar currentDate = new Date();\n//var currentDateString = currentDate.toISOString().slice(0,10);\n\n// 将当前日期发送到下一个节点\nvar currentDate = new Date();\n\n// 将日期格式转换为所需格式(例如:YYYY-MM-DD)\nvar formattedDate = ('0' + (currentDate.getMonth() + 1)).slice(-2) + '月' + ('0' + currentDate.getDate()).slice(-2)+'日';\n\n// 对比两个日期是否相同\nif (tiaozhendate === formattedDate){\n    msg.payload = 'on';\n}else{\n    msg.payload = 'off';\n}\n\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 890,
        "y": 280,
        "wires": [
            [
                "ed92894a6d3e5520"
            ]
        ]
    },
    {
        "id": "ed92894a6d3e5520",
        "type": "switch",
        "z": "de66179675455cb6",
        "name": "判断两个日期是否相同",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1140,
        "y": 280,
        "wires": [
            [
                "70b08a1de0c3bf37"
            ]
        ]
    },
    {
        "id": "97bd4f8f.bee1f",
        "type": "ha-entity-config",
        "server": "8ee52cc75649ea4c",
        "deviceConfig": "52396cf973e98382",
        "name": "最新油价",
        "version": "6",
        "entityType": "sensor",
        "haConfig": [
            {
                "property": "name",
                "value": "最新油价"
            },
            {
                "property": "icon",
                "value": "mdi:gas-station"
            },
            {
                "property": "entity_picture",
                "value": ""
            },
            {
                "property": "entity_category",
                "value": ""
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            },
            {
                "property": "state_class",
                "value": ""
            }
        ],
        "resend": true,
        "debugEnabled": false
    },
    {
        "id": "3e6cb13c5ba04c83",
        "type": "ha-entity-config",
        "server": "8ee52cc75649ea4c",
        "deviceConfig": "b0765d0d7d439dae",
        "name": "92#汽油油价",
        "version": "6",
        "entityType": "sensor",
        "haConfig": [
            {
                "property": "name",
                "value": "92#汽油油价"
            },
            {
                "property": "icon",
                "value": "mdi:gas-station"
            },
            {
                "property": "entity_picture",
                "value": ""
            },
            {
                "property": "entity_category",
                "value": ""
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            },
            {
                "property": "state_class",
                "value": ""
            }
        ],
        "resend": false,
        "debugEnabled": false
    },
    {
        "id": "8ee52cc75649ea4c",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "default",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": false
    },
    {
        "id": "52396cf973e98382",
        "type": "ha-device-config",
        "name": "最新油价",
        "hwVersion": "",
        "manufacturer": "Node-RED",
        "model": "",
        "swVersion": ""
    },
    {
        "id": "b0765d0d7d439dae",
        "type": "ha-device-config",
        "name": "92#汽油油价",
        "hwVersion": "",
        "manufacturer": "Node-RED",
        "model": "",
        "swVersion": ""
    }
]

评分

参与人数 1金钱 +12 收起 理由
隔壁的王叔叔 + 12 感谢楼主分享!

查看全部评分

回复

使用道具 举报

14

主题

70

帖子

406

积分

论坛分享达人

积分
406
金钱
336
HASS币
0
 楼主| 发表于 2024-5-23 23:43:51 | 显示全部楼层


坛内发帖有大小限制,节点图补不上来
回复

使用道具 举报

7

主题

1105

帖子

3510

积分

论坛元老

Rank: 8Rank: 8

积分
3510
金钱
2405
HASS币
0
发表于 2024-5-24 08:46:44 | 显示全部楼层
感谢分享,最好是把json发上来。
回复

使用道具 举报

39

主题

2083

帖子

7855

积分

元老级技术达人

积分
7855
金钱
5757
HASS币
110
发表于 2024-5-24 13:18:00 | 显示全部楼层
看起来和我分享的那个有点像
回复

使用道具 举报

14

主题

70

帖子

406

积分

论坛分享达人

积分
406
金钱
336
HASS币
0
 楼主| 发表于 2024-5-24 21:21:40 | 显示全部楼层
ghostist 发表于 2024-5-24 13:18
看起来和我分享的那个有点像

我一看还真是呢,拉取油价接入ha部分用了您的分享。https://bbs.hassbian.com/thread-24721-1-1.html
回复

使用道具 举报

14

主题

70

帖子

406

积分

论坛分享达人

积分
406
金钱
336
HASS币
0
 楼主| 发表于 2024-5-24 21:30:19 | 显示全部楼层
json文件已上传,记得把里面的your_anpush_api,your_anpush_channel替换成你自己申请到的api和设置的信道,研究下anpush的文档也可以换成钉钉等其他方式推送。里面涉及需要更改省份的,在浏览器打开获取油价的url,在里面选好后复制出来替换掉就行了。

flows.zip

2.85 KB, 下载次数: 9

回复

使用道具 举报

1

主题

30

帖子

196

积分

注册会员

Rank: 2

积分
196
金钱
166
HASS币
0
发表于 2024-5-26 00:04:49 | 显示全部楼层
非常感谢
回复

使用道具 举报

39

主题

2083

帖子

7855

积分

元老级技术达人

积分
7855
金钱
5757
HASS币
110
发表于 2024-5-27 08:51:24 | 显示全部楼层
gl695133087 发表于 2024-5-24 21:21
我一看还真是呢,拉取油价接入ha部分用了您的分享。https://bbs.hassbian.com/thread-24721-1-1.html。 ...

我习惯引用他人的内容都放上原地址,不过也确实出现过引用太久 太老,最后找不到原帖的情况
说明原帖都是有价值的 习惯性记录一下,有心人还可以顺着原帖学习一下思路

我这个随便用吧 也是面向GPT编程的
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-6-17 01:52 , Processed in 0.078494 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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