XCray
April 19, 2020, 8:54am
1
Monitoring the status of WAN connection on OpenWRT router in HomeAssistant
reference:
https://openwrt.org/docs/guide-user/base-system/hotplug
configuration.yaml:
binary_sensor:
- platform: mqtt
state_topic: "openwrt/pppoe-wan"
payload_on: "ifup"
payload_off: "ifdown"
name: internet
device_class: connectivity
json_attributes_topic: "openwrt/wan-ip"
last 3 lines are not essential. Of cause, there should be one mqtt broker in the configuration:
mqtt:
broker: mqtt_broker #your own broker address
script under /etc/hotplug.d/ directory of OpenWRT router:
#!/bin/sh
[ "$INTERFACE" = "wan" ] || exit 0
mosquitto_pub -h mqtt_broker -t openwrt/pppoe-wan -m "$ACTION"
wanip=$(ifconfig pppoe-wan | awk '/inet addr/{print substr($2,6)}')
mosquitto_pub -h mqtt_broker -t openwrt/wan-ip -m "{\"wan-ip\":\""$wanip"\"}"
last 2 lines are not essential.
The topics should match between the script and the configuration.
link: repo on GitHub
3 Likes
XCray
April 20, 2020, 5:13am
2
optimized code:
configuration.yaml:
binary_sensor:
- platform: mqtt
state_topic: "openwrt/pppoe-wan"
json_attributes_topic: "openwrt/wan-ip"
name: internet
device_class: connectivity
script under /etc/hotplug.d/ directory of OpenWRT router:
#!/bin/sh
[ "$INTERFACE" = "wan" ] || exit 0
[ "$ACTION" = "ifdown" ] && mosquitto_pub -h x.x.x.x -t openwrt/pppoe-wan -m OFF
[ "$ACTION" = "ifup" ] && mosquitto_pub -h x.x.x.x -t openwrt/pppoe-wan -m ON
[ "$ACTION" = "ifupdate" ] && (
wanip=$(ifconfig pppoe-wan | awk '/inet addr/{print substr($2,6)}')
mosquitto_pub -h x.x.x.x -t openwrt/wan-ip -m "{\"wan-ip\":\""$wanip"\"}" )
1 Like
thanks for this work, its great.
in which folder do you add the script?
I copied it into every folder, only if I restart homeassistant does it take days until the script is executed once
XCray
May 4, 2020, 9:00am
4
pls put the script under “/etc/hotplug.d/iface” directory on your openwrt router.
XCray:
/etc/hotplug.d/iface
is not executed for me when the internet is down, only if I manually go to restart
Yes i have make the script executable
XCray
May 27, 2020, 4:04am
8
maybe the names of the interfaces are diffrent.
to make sure, you could add env > /tmp/iface.log
to the begainning of the script, and check the log later
We should make a feature request for a full fledged OpenWRT integration with Sensors, switches, etc
A lot of open-source enthusiasts use both HA and OpenWRT, so it makes sense to have one.
5 Likes
This would be great, just switched my complete network infrastructure over to openwrt and I’m a bit baffled that it gives much less metrics than my old (commercial) solution.
Also I wouldn’t resurrect the dead (turned off mqtt 2 years ago) to get information from openwrt to ha.
1 Like
nilux
May 9, 2022, 6:42am
12
For OpenWRT there’s vnstat, bandwidthmonitor/nlbwmon, and statistics/collectd, all with a gui.
Can I get any of feeding it’s data into ha?
nilux
May 9, 2022, 7:53am
14
Not that I know of. For HA I use the upnp/igd integration. It shows WAN KiB/s and packets both ways.
1 Like
Anything special I need to set up on the openwrt side for that?
I also searched the openwrt site and can’t find anything particular helpful about this
nilux
May 10, 2022, 6:26am
16
Do you have upnp installed and enabled? miniupnpd or luci-app-upnp.
yousaf465
(Yousaf465)
August 1, 2022, 5:26pm
17
nilux:
miniupnpd
installed both.
Now it should show up in HA?
nilux
August 2, 2022, 12:44pm
18
If you have enabled upnp in openwrt, I think it should show up in HA
yousaf465
(Yousaf465)
August 2, 2022, 2:03pm
19
My current setting, but no mention of Openwrt in HA. I can see Fresbtomato and Huwaei but not OPENWRT.
nilux
August 3, 2022, 10:33am
20
Maybe also tick the ”start upnp/pmp” box?
yousaf465
(Yousaf465)
August 3, 2022, 3:21pm
21
same result. no UPNP device was found even though Freshtomato and Huwaei are working fine.