Is there a simple way to detect the presence of a particular SSID?

I know you selected @WallyR’s answer as solution but I just wanted to throw in a piece of code that I developed to solve a similar problem for the Chromecast issues I encountered.
I got it to work on an ESP32 with 2.4GHz networks but failed to implement it on an ESP32-C5 using it for 5GHz networks.

Either way, here’s the code if you’re interested - this special version provides a sensor in HA that lists all SSID staring with “T” (for Test):

esphome:
  name: ssid-scanner
  friendly_name: SSID-Scanner

esp32:
  board: esp32dev

logger:

api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxx"

ota:
  - platform: esphome
    password: "password"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    then:
      - lambda: |-
          ESP_LOGD("scan", "Wi-Fi connected — configuring continuous scan");

          static wifi_scan_config_t scan_conf = {};
          scan_conf.scan_type   = WIFI_SCAN_TYPE_ACTIVE;
          scan_conf.show_hidden = false;

          static esp_event_handler_instance_t scan_done_hinst;
          esp_event_handler_instance_register(
            WIFI_EVENT,
            WIFI_EVENT_SCAN_DONE,
            [](void*, esp_event_base_t, int32_t, void*) {
              ESP_LOGD("scan", "WIFI_EVENT_SCAN_DONE — filtering SSIDs");

              uint16_t ap_count = 0;
              esp_wifi_scan_get_ap_num(&ap_count);
              if (ap_count == 0) {
                id(last_found_ssids).publish_state("None");
                ESP_LOGI("scan", "No APs found");
              } else {
                auto *aps = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * ap_count);
                esp_wifi_scan_get_ap_records(&ap_count, aps);

                std::string ssid_list;
                for (int i = 0; i < ap_count; i++) {
                  const char* ssid = reinterpret_cast<const char*>(aps[i].ssid);
                  if (ssid[0] == 'T') {  // Filter condition — adjust as needed
                    if (!ssid_list.empty()) ssid_list += ", ";
                    ssid_list += ssid;
                  }
                }

                if (ssid_list.empty()) {
                  id(last_found_ssids).publish_state("None");
                  ESP_LOGI("scan", "No matching SSIDs found");
                } else {
                  id(last_found_ssids).publish_state(ssid_list);
                  ESP_LOGI("scan", "Matched SSIDs: %s", ssid_list.c_str());
                }

                free(aps);
              }

              vTaskDelay(pdMS_TO_TICKS(5000));  // 5s pause between scans
              esp_wifi_scan_start(&scan_conf, false);
            },
            nullptr,
            &scan_done_hinst
          );

          esp_wifi_scan_start(&scan_conf, false);

  ap:
    ssid: "SSID-Checker Fallback Hotspot"
    password: "password"

captive_portal:

web_server:
  port: 80
  include_internal: true

text_sensor:
  - platform: template
    name: "Last Found SSIDs"
    id: last_found_ssids
1 Like