Ble ibeacon as a remote switch

Using esphome on an esp32c6 I and coded this with ble ibeacon detection of the HolyIOT type to detect the button push. It does this but I don’t how to use the detection as a switch which in this case will be to change the state of a GPIO pin.

esphome:
  name: lightsw1
  friendly_name: lightSw1

esp32:
  board: esp32-c6-devkitc-1
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "tvqTEM+atkTXDQqZaWqAiZlUvo1DETepjhDbwr0ifVM="

ota:
  - platform: esphome
    password: "a7a1ecf7b8e986181f4223f52907ce75"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lightsw1 Fallback Hotspot"
    password: "Yk4TCb8p7wmL"


captive_portal:
output:
  - platform: gpio
    pin: GPIO15
    id: board_led 
sensor:
  - platform: template
    name: "BLE Sensor"
    id: ble_sensor

esp32_ble_tracker:
  on_ble_advertise:
    - mac_address:
        - e5:0a:33:0c:2d:32
      then:
        - lambda: |-  
            ESP_LOGD("ble_adv", "  Advertised service data:");
            for (auto data : x.get_service_datas()) {
                ESP_LOGD("ble_adv", "    - %s: (length %i)", data.uuid.to_string().c_str(), data.data.size());
                if (data.uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(0x5242)) {
                if (data.data.size() > 0) {
                   uint8_t value = data.data[11];  // byte holds switch data in bit[0] 0 being lsb
                // Process the value (e.g., publish to a sensor)
                   id(ble_sensor).publish_state(value);
                ESP_LOGD("ble_adv", " switch data   - %d: " , value);
              }
            }
            ESP_LOGD("ble_adv", "  Data: %s", format_hex_pretty(data.data).c_str());   // this displays the switch data
            }
            
            ESP_LOGD("ble_adv", "  Advertised manufacturer data:");
            for (auto data : x.get_manufacturer_datas()) {
                ESP_LOGD("ble_adv", "    - %s: (length %i)", data.uuid.to_string().c_str(), data.data.size());
            }


binary_sensor:
  - platform: template 
    name: "room switch"
    id: room_switch
    lambda: |-
      if (id(ble_sensor).state) {
        return ON;
      } else {
        return OFF;
      }
# this section does not compile and reports that I cannot use ON here
switch:
  - platform: output
    output: board_led
    name: "room_light"
    id: room_switch
      ON: board_led.turn_off
          
        
      - OFF 
          board_led.turn_on

From where you got that switch config?

binary_sensor:
  - platform: template 
    name: "room switch"
    id: room_switch
    lambda: |-
      if (id(ble_sensor).state) {
        return true;
      } else {
        return false;
      }

    on_press:
      then:
        - switch.turn_on: room_switch
    on_release
      then:
        - switch.turn_off: room_switch


switch:
  - platform: gpio
    pin: GPIO15
    id: room_switch
    name: "room light"

Remove the output component for gpio15.
And be aware that C6 gpio15 is strapping pin.

From the definition of the board_led. On the esp32c6 mini this is connected to the led on board which is active low

Ok.
Just swap the turn_off and turn_on

Did you configure the beacon itself to only transmit when the button is pressed? You’ll have to do that unless its configured like that by default but, none of the beacons w/button I use are configured that way by default but, they’re also a different brand than what you have so IDK.

Ive had nothing but issues with different types of BT beacons w/button but, i was always trying to have the best of both worlds and use the beacon as a BT presence beacon and utilize the button but, the way they use 2 different UUID’s and the delays to switch between those UUID’s, it was nothing but a nightmare that i could never get to work in an acceptable way for me so, hopefully you have a better time with just using it as simple button.

I did not reprogram the iBeacon but made a push button supply power to the beacon to extend its CR3032 battery lifetime. The other change I made was to move the detection to a separate ESP32C6 that switched a GPIO pin on another esp8266 modules running esphome. This improved reliability at the expense of longer actuation times. A rather imperfect solution. I intend to look at Enocean batteryless switches in the future.

Thanks for your help. I now have it function correctly but the scanning is not very reliable

You’re welcome.
In what sense it’s not reliable?
Try to play with the scanning parameters.

I dont think you have it set up right at all. Every type of wireless BT i’ve used, they use 2 and sometimes even more UUID’s because they can have the ability built-in for things like sending a unique UUID for single press, double press, press and hold etc

Do you have a BT beacon scanner app on your phone? I use Kbeacon Pro and its in the app store or play store and shows BT scans like this.

You should download a scanner and then check to see if

  1. Is the button broadcasting a UUID by default, even when its just sitting idle.

  2. When you press the button, does the UUID change to a different one?

  3. Check to see if your button has more capabilities than just being used to do a single function that’s one button press.

Here is one i use for example and see how it sais all the different button press combinations and then different ways to have the UUID broadcast based on how its pressed? Yours should be able to be configured similar to this and then however you configure it to transmit, thats the way you need to configure your esp32 ble tracker with 1 or multiple sensors for however many button combinations you configure. You may even be able to pick up additional information like it’s battery level and any other sensor values if it has them also.