Detecting WiFi networks

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: Rasbian
    • Java Runtime Environment: Zulu Embedded
    • openHAB version: 2.5.0~S1736

My RPi is using ethernet, so the onboard wifi is currently going unused. I’m looking for a way to scan for wifi networks, and use the list returned; the end goal is to detect if my wifi networks are up, and if my car (which has its own wifi network) is home or not. I’m not looking for specific devices, just a list of available wifi networks in the vicinity. If I need to write an exec script to handle it, I can, but I was hoping that there is a more “OpenHAB” way to do things. Any ideas?

I would imagine the only way will be to get that from a script, return json and parse it within OH.

If you do, please share said solution as its one of the things on my todo

1 Like

I don’t own a pi 4 yet, but in my pi 3 I can get the list with this command :

iwlist wlan0 scan | grep ESSID

If it works you can use it to create a script

I used a Raspberry Pi 4 running Rasbian, but as far as I know, this should work on any version of Linux. YMMV.

What we want is a cleaned-up version of the output “iwlist wlan0 scan” creates. Unfortunately, iwlist needs sudo to run. Fortunately, we can make that happen! Start with this:

sudo visudo -f /etc/sudoers.d/010_openhab-nopasswd

The sudoers.d directory holds any files that gives (or removes) sudo access to/from programs and users. This allows openhab to use the sudo command - but we don’t want it to be able to run absolutely anything, that’s a security risk. So, we add the following line, limiting the passwordless sudo command to one specific command:

openhab ALL=(root) NOPASSWD: /sbin/iwlist wlan0 scan

This assumes your openhab user is called “openhab”, and your wifi device name is “wlan0”.
Save and close, and now openhab has permission to use sudo to run the scan command! Of course, the output is rather overwhelming; we’re only looking for one specific SSID. Additionally, OpenHAB doesn’t use a “real” shell to run commands, so sudo will complain. To fix both problems, we’ll wrap our sudo command in a command line script (if your openhab directory is elsewhere, use that instead of ‘/etc/openhab2/’):

nano /etc/openhab2/

Inside the script file, add the following:

SSIDs=`sudo /sbin/iwlist wlan0 scan`
names=`echo "$SSIDs" | grep -oP "ESSID:\K(\"[^\"]+\")" | sort`
echo "$names"
[[ $names =~ "\"MyNetwork\"" ]] || exit 1
exit 0

This will use a “real” shell to get the full output of iwlist, break it down into a list of double-quoted SSIDs, print that list, and check if the list contains the one network you’re looking for; if so, it exist with status 0, otherwise, status 1. Note that this searches by name, so DO NOT use this to unlock doors or disarm security systems! Anyone could create a wifi network with the right name and it would work!

Save and close it, then test it out:

sudo su openhab -c /etc/openhab2/

Hopefully, it should return a list of wifi networks, like this (without asking for a password!):

That’s the command line stuff done; next, we make a new Exec thing. For the command, use:


Set the timeout to something low, like 5 (or 10 if you have a lot of networks nearby); set it to run no more frequently than your timeout (I suggest at least 1 minute). The Output will be the list of wifi networks; the exit code will be the presence indicator, 0 if the network is in range, 1 if it is out of range. In my sitemap, I have the following:

Text item=CommandWiFiCheck_ExitValue label="My car is... [MAP(]" icon="car_red"

Where contains:

And that’s all it took; not as easy as some things, but at least I didn’t have to make a weirdly convoluted rule for it.