江宁公交】nodered 官方接口,一劳永逸

文章正文
发布时间:2025-07-02 21:09

敬告:本帖本着“授人以鱼不如授人以渔”的思想,只给出关键点,具体自己实现


众所周知江宁不属于南京
包括医保、就学、社保都是独立于南京市的
当然还有公交

之前一直是小电驴+开车解决通勤问题
然鹅媳妇不会停那种升降车位,选择公交地铁通勤
在昨日大降温 中午苦等半小时才来车后,提出了需求:想知道车啥时候到
地铁有运行线路图,基本上可以卡点
公交不行,受影响太多,所以要搞数据

当然 第一时间想到的就是上论坛搜,以前有印象看到过公交信息的帖子
然后就只有北京的、车来了的(车来了很奇怪,小程序可以搜到,但是论坛帖子里给的地址就没有我所在的地区)
然后就自己搞呗

一搜:江宁有官方的查询方式
虽然微信里直接访问就可以查,但是每次还要点点点,本着所有数据归集到HA的原则:
NR获取数据,HA搞界面显示

录制_2022_12_01_11_40_31_436.gif


登录/注册后可看大图

录制_2022_12_01_11_40_31_436.gif (1.08 MB, 下载次数: 0)

下载附件

2022-12-1 11:42 上传



教程开始:
1、数据获取
访问,选线路、选方向、然后F12,根据图片找到报文

1.png


登录/注册后可看大图

1.png (114.02 KB, 下载次数: 0)

下载附件

2022-12-1 11:48 上传



然后切换到【标头】那里,红色部分是我们需要的

2.png


登录/注册后可看大图

2.png (74.19 KB, 下载次数: 0)

下载附件

2022-12-1 11:50 上传



2、NR里创建请求

3.png


登录/注册后可看大图

3.png (130.24 KB, 下载次数: 0)

下载附件

2022-12-1 11:54 上传


如图,根据上面F12获取的信息,创建请求头,然后向目的地址请求
返回数据后进行解析,然后创建HA实体
2.1 设置请求头,对应上面F12界面里的【请求标头】

4.png


登录/注册后可看大图

4.png (111.1 KB, 下载次数: 0)

下载附件

2022-12-1 13:13 上传


2.2 设置请求地址,对应上面F12界面里的【请求网址】

5.png


登录/注册后可看大图

5.png (98.9 KB, 下载次数: 0)

下载附件

2022-12-1 13:13 上传


2.3 返回数据处理,这里主要就是删除多余字符,然后分割字符串,再替换,保留前3条有用信息,如果没有信息那就说“暂时无法获取”

6.png


登录/注册后可看大图

6.png (117.6 KB, 下载次数: 0)

下载附件

2022-12-1 13:13 上传



3、HA里显示
考虑到
这里用到了custom:button-card和custom:state-switch,这两个第三方卡片,在HACS里搜索安装即可
自带的用到了vertical-stack、horizontal-stack、markdown这三种卡片
实现了去程和返程切换显示以及开启、关闭刷新功能(节省人家服务器资源,每分钟刷新数据才有意义,但是有点过分)
配置比较麻烦,贴上源码自己理解学习一下

type: vertical-stack cards: - type: horizontal-stack cards: - type: custom:button-card color_type: card entity: switch.businfo_switch name: 公交信息切换 state: - value: 'on' color: rgb(52, 168, 83) icon: mdi:home-account name: "回家" - value: 'off' color: rgb(66, 133, 244) icon: mdi:domain name: "去公司" styles: card: - height: 80px - width: 120px tap_action: action: toggle - type: custom:button-card color_type: card entity: switch.businfo_update_switch name: 刷新开关 layout: icon_label label: > [[[ return states['sensor.businfo_uptime'].state; ]]] show_label: true show_name: false state: - value: 'on' icon: mdi:timer-sync-outline name: "刷新中" - value: 'off' icon: mdi:timer-remove-outline name: "停止刷新" styles: card: - height: 80px - type: custom:state-switch entity: switch.businfo_switch default: 'on' transition: flip-x transition_time: 500 states: "on": type: markdown content: >- ## <font color=#34a853>义乌 </font>站 <ha-icon icon="mdi:bus"></ha-icon><font size=5 color=#34a853>829</font> <font size=3 color=#ea4335> {{ states('sensor.829lu_yi_wu_wu_yi_lu_zhou')}}</font> - {{ state_attr('sensor.829lu_yi_wu_wu_yi_lu_zhou','next_bus_info')}} - {{ state_attr('sensor.829lu_yi_wu_wu_yi_lu_zhou','next_bus_info2')}} <ha-icon icon="mdi:bus"></ha-icon><font size=5 color=#34a853>851</font> <font size=3 color=#ea4335> {{ states('sensor.851lu_yi_wu_wu_yi_lu_zhou')}}</font> - {{ state_attr('sensor.851lu_yi_wu_wu_yi_lu_zhou','next_bus_info')}} - {{ state_attr('sensor.851lu_yi_wu_wu_yi_lu_zhou','next_bus_info2')}} "off": type: markdown content: >- ## <font color=#4285f4>武夷绿洲 </font>站 <ha-icon icon="mdi:bus"></ha-icon><font size=5 color=#4285f4>829</font> <font size=3 color=#ea4335> {{ states('sensor.829lu_wu_yi_lu_zhou_yi_wu')}}</font> - {{ state_attr('sensor.829lu_wu_yi_lu_zhou_yi_wu','next_bus_info')}} - {{ state_attr('sensor.829lu_wu_yi_lu_zhou_yi_wu','next_bus_info2')}} <ha-icon icon="mdi:bus"></ha-icon><font size=5 color=#4285f4>851</font> <font size=3 color=#ea4335> {{ states('sensor.851lu_wu_yi_lu_zhou_yi_wu')}}</font> - {{ state_attr('sensor.851lu_wu_yi_lu_zhou_yi_wu','next_bus_info')}} - {{ state_attr('sensor.851lu_wu_yi_lu_zhou_yi_wu','next_bus_info2')}}

复制代码