List wifi networks from hassio command line to use in a sensor

Hello All. You can list wifi networks in HASSIO in the networks tab.

Is it programmatically possible to list wifi networks and create a sensor of the data? I was originally going to use a shell command sensor but nmcli isn’t available in the homeassistant container. Is there another way to get this data in home assistant? An api call? I’m open to options.

API:

/network/interface/<interface>/accesspoints

https://developers.home-assistant.io/docs/api/supervisor/endpoints/#network

1 Like

Bingo! Thank-you!

Hi
I tried this:

rest_command:
  listar_redes_wifi:
    url: "http://192.168.1.202:8123/network/interface/wlan0/accesspoints"
    method: GET
    headers:
      Authorization: "Bearer !secret api_supervisor_token"
    content_type: "application/json"

automation
  - id: actualizar_lista_redes_wifi
    alias: Actualizar lista de redes WiFi
    description: Llama al comando para listar redes WiFi disponibles
    trigger:
      - platform: time_pattern
        seconds: "/5"
    action:
      - service: rest_command.listar_redes_wifi
      - service: input_text.set_value
        target:
          entity_id: input_text.redes_wifi
        data:
          value: "{{ value_json }}"
    mode: single

But, it does not work…

this works for me

 aps=`curl -sSL -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" http://supervisor/network/interface/wlan0/accesspoints`

could you share the whole code?

# docker exec -it homeassistant exec bash

homeassistant:/config# curl -sSL -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" http://supervisor/network/interface/wlan0/accesspoints
{"result":"ok","data":{"accesspoints":[{"mode":"infrastructure","ssid":"#REDACTED }]}}

homeassistant:/config# 

I use a command line sensor to execute a script. That script contains that command and returns some info for me.

#!/bin/bash

aps=`curl -sSL -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" http://supervisor/network/interface/wlan0/accesspoints`

bucs21=`echo "$aps" | jq  -j '.data | .accesspoints[] | select(.mac=="B4:FB:E4:17:BC:34") | .frequency'`
bucs22=`echo "$aps" | jq  -j '.data | .accesspoints[] | select(.mac=="B4:FB:E4:94:45:64") | .frequency'`

bucs51=`echo "$aps" | jq  -j '.data | .accesspoints[] | select(.mac=="B4:FB:E4:18:BC:34") | .frequency'`
bucs52=`echo "$aps" | jq  -j '.data | .accesspoints[] | select(.mac=="B4:FB:E4:95:45:64") | .frequency'`

R=0
[[ -z $bucs21 ]] && bucs21=?
[[ -z $bucs22 ]] && bucs22=?
[[ -z $bucs51 ]] && bucs51=?
[[ -z $bucs52 ]] && bucs52=?
if [[ "$bucs21" == "$bucs22" ]]; then
  R=2:$bucs21
fi
if [[ "$bucs51" == "$bucs52" ]]; then
  if [[ "$R" == "0" ]]; then
    R=5:$bucs51:$bucs522
  else
    R=2:$bucs21-5:$bucs51
  fi
fi

echo "$R"
exit 0