WiFi BSSID Sensor for Home Assistant Server

Hi all,
My Home Assistant server hosted on a Raspberry Pi 4b was having some occasional WiFi issues so I wanted to setup a way to monitor what BSSID it was connected to for diagnosing if issues are related to a particular WiFi AP in my area as the network the RPi is on is large and can be congested.
Anyways the best way I found to do this was via the Advanced SSH & Web Terminal add-on to gather the BSSID and then use MQTT and cron to push to HA.

Prerequisites:

  • Mosquitto broker add-on already running.
  • Advanced SSH & Web Terminal add-on
    • If you’re having issues, try turning Protection mode off, however on my device this is NOT needed.

Advanced SSH & Web Terminal Config:

Under Configuration → Options click the three dots icon and “Edit in YAML” then replace the packages and init_commands section with the following:

packages:
  - networkmanager-cli
  - mosquitto-clients
init_commands:
  - >
    cat << 'EOF' > /usr/local/bin/push_bssid_mqtt.sh

    #!/bin/sh

    BSSID=$(/usr/bin/nmcli -t -f active,bssid dev wifi list ifname wlan0 | grep
    '^yes' | cut -d: -f2- | sed 's/\\//g')

    /usr/bin/mosquitto_pub -h localhost -t "ha/wifi/bssid" -m "$BSSID"

    EOF

    chmod +x /usr/local/bin/push_bssid_mqtt.sh

    (crontab -l 2>/dev/null | grep -v push_bssid; echo "* * * * *
    /usr/local/bin/push_bssid_mqtt.sh") | crontab -

    crond -b

package and init_commands run on every add-on start, so this survives reboots and HA updates. crond -b is needed because the daemon doesn’t auto start in this container.

HA Sensor:

Then in your configuration.yaml you’ll need to add a sensor like:

mqtt:
  sensor:
    - name: "HA WiFi BSSID"
      unique_id: ha_wifi_bssid_mqtt
      state_topic: "ha/wifi/bssid"

Verify:

If you want to make sure this is all working properly, ssh in and check:

ps aux | grep crond
crontab -l
mosquitto_sub -h localhost -t "ha/wifi/bssid" -v

Then wait for the minute to roll over and you should see something like:

ha/wifi/bssid 4B:93:C3:DD:8E:B8

May I ask you why you didn’t make your own local app ?
It’s realy easy.

I can see in your code lines that your skills are good,

Not to rain on anyone’s parade, but since HA is headless wouldn’t it be better to just connect it to your network with a cable rather than using wifi that has occasional issues?

It absolutely would be! However in our environment that is unfortunately not an option.