找回密码
 立即注册

微信扫码登录

搜索
查看: 2987|回复: 14

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

[复制链接]

15

主题

69

回帖

526

积分

论坛分享达人

积分
526
金钱
442
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 感谢楼主分享!

查看全部评分

回复

使用道具 举报

15

主题

69

回帖

526

积分

论坛分享达人

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


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

使用道具 举报

15

主题

69

回帖

526

积分

论坛分享达人

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

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

使用道具 举报

15

主题

69

回帖

526

积分

论坛分享达人

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

flows.zip

2.85 KB, 下载次数: 41

回复

使用道具 举报

15

主题

69

回帖

526

积分

论坛分享达人

积分
526
金钱
442
HASS币
0
 楼主| 发表于 2024-9-10 00:17:12 | 显示全部楼层
[{"id":"354b77ea224b34fa","type":"http request","z":"a0d142e5f5ffdc44","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://www.qiyoujiage.com/guizhou/qianxinan.shtml","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":340,"y":340,"wires":[["0b50352509aaa71b"]]},{"id":"f8c53c2c58c2de41","type":"inject","z":"a0d142e5f5ffdc44","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"43200","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":320,"wires":[["354b77ea224b34fa"]]},{"id":"9cb2b5a6707ef8a6","type":"mqtt in","z":"a0d142e5f5ffdc44","name":"主动刷新","topic":"youjia/contrl/shuaxin","qos":"0","datatype":"utf8","broker":"7cb95522e79640f4","nl":false,"rap":true,"rh":0,"inputs":0,"x":140,"y":360,"wires":[["354b77ea224b34fa"]]},{"id":"0b50352509aaa71b","type":"html","z":"a0d142e5f5ffdc44","name":"提取","property":"payload","outproperty":"payload","tag":"#youjiaCont> div","ret":"text","as":"single","chr":"","x":490,"y":340,"wires":[["d4c09bce0a44c7fb"]]},{"id":"d4c09bce0a44c7fb","type":"function","z":"a0d142e5f5ffdc44","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');\n//var endIndex = StateTips[2].indexOf('。');\nvar TipsTips = StateTips[2].substring(0, 500);\n\nvar newmsg = {};\nnewmsg.price = filteredArray;\nnewmsg.state = StateTips[1];\nnewmsg.tips = TipsTips;\nflow.set('newmsg', newmsg); //将access_token储存\n\nreturn newmsg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":340,"wires":[["ff63fe69898cd1ba"]]},{"id":"ff63fe69898cd1ba","type":"function","z":"a0d142e5f5ffdc44","name":"整合mqtt流","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//获取日\nvar str2 = flow.get('newmsg').state.substring(n2 + 1, n3);\n\n//整合油价调整日期\nvar tiaozhendate =str1+'月'+str2+'日';\n\nmsg.payload = {\n    "汽油92": flow.get("newmsg").price[1],\n    "汽油95": flow.get("newmsg").price[3],\n    "汽油98": flow.get("newmsg").price[5],\n    "柴油0": flow.get("newmsg").price[7],\n    "下一轮调价日期": tiaozhendate,\n    "调整情况": flow.get("newmsg").tips\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":340,"wires":[["ae6a162efd208da1"]]},{"id":"ae6a162efd208da1","type":"mqtt out","z":"a0d142e5f5ffdc44","name":"最新油价","topic":"youjia/zuixinyoujia","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7cb95522e79640f4","x":1100,"y":340,"wires":[]},{"id":"626a76e2505b39c8","type":"function","z":"a0d142e5f5ffdc44","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":390,"y":420,"wires":[["ed92894a6d3e5520"]]},{"id":"84876af9798c96c7","type":"inject","z":"a0d142e5f5ffdc44","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 01 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":420,"wires":[["626a76e2505b39c8"]]},{"id":"ed92894a6d3e5520","type":"switch","z":"a0d142e5f5ffdc44","name":"判断两个日期是否相同","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":660,"y":420,"wires":[["70b08a1de0c3bf37"]]},{"id":"70b08a1de0c3bf37","type":"function","z":"a0d142e5f5ffdc44","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: '30019',\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":950,"y":420,"wires":[["c521c69735aa60c6"]]},{"id":"c521c69735aa60c6","type":"http request","z":"a0d142e5f5ffdc44","name":"微信推送","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.anpush.com/push/{your_token}","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Content-Type","keyValue":"","valueType":"other","valueValue":"application/x-www-form-urlencoded"}],"x":1180,"y":420,"wires":[[]]},{"id":"7cb95522e79640f4","type":"mqtt-broker","name":"ha上的mqtt","broker":"192.168.31.8","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
回复

使用道具 举报

15

主题

69

回帖

526

积分

论坛分享达人

积分
526
金钱
442
HASS币
0
 楼主| 发表于 2024-9-10 00:19:02 | 显示全部楼层

之前的流已经不能正确获取了,应该是网站有所调整,我改了下,能继续使用。微信推送部分没改,暂时不能推送!
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-7-16 14:48 , Processed in 0.281527 second(s), 13 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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