A switch for turning bt-proxy on or off

Hey there :slight_smile:
I have some projects in the pipeline, where I plan to include an esp32 for some basic things like turn an led on / of… etc.

Now, I want also to make use of the other benefits that I can have when using an ESP32 - for example, they should act as an bluetooth proxy, and maybe also include voice assistant later.

BUT:
I would like to implement a Switch component to turn on or of the Bluetooth Proxy functionallity

so is there a way to implement this:

bluetooth_proxy:
  active: true

with a turn on / off switch that can be controlled within the Dashboard from Homeassistant?

Have you read the docs? Do you see an on/off?

Anyway, why?

if I would have seen any “on/off” option, I would probably not ask if there’s a method to implement such…
The question “why” - is another such question that doesn’t matter in that case… I would like to have an option to switch the bt proxy on or off … if that’s possible somehow - even with more complex code

I always “love” the question why someone want to have a very specific request, just because it doesn’t make sense to others…

So… is there any way to change such setting from true to false with a switch / script - whatever?
Yes? → then it would be nice to provide some help
No → then, that’s also fine for me… :slight_smile:
Thanks.

Fair comment. But sometimes asking “why” reveals an alternative.

I don’t think there is a way to turn it off/on on the fly. There may be a way within c++.

1 Like

ok… sorry for my “rude” words…
The reason is pretty simple… I want to implement BT Proxy - but I just want to enable it when I actually need it… right now, I don’t need it - but I don’t want to change the Config every time when I might need it…

Also, sometimes, you might just disable some functionallities for a certain time… so I do have some Plant sensors on plants on our balkony… I have a BT Proxy to get those information in the kitchen… but when we bring the plant inside over the winter, I don’t need the BT Proxy enabled in the kitchen… :wink:

Why in the world would you ever need to toggle it on/off? That makes about as much sense as wanting to toggle your router on/off. BT proxying is meant to run in the background and be a BT middleman. I think you might be overthinking this and gonna make things more difficult than they need to be.

and?
Is that any kind of problem for you?

I really wonder why people argue about the reason for a request, if they can’t provide an answer to a specific question…
is it just for the sake of writing something?

Anyway - as I already explained, I do have some devices, that should act as a BT Proxy under some conditions.
And if these conditions are not met, they don’t need to do ANY kind of BT stuff…
It’s just like the newly integrated switch for the Voice-Assistant to switch wake-word detection on or of…

IF I use the device for VoiceControl, I don’t “NEED” to switch between Push-To-Talk or WakeWord detection… I could just configure the device as I want it to behave.
But I CAN do it…

And I would like to be able to Controll options the ESP can do - if I need it - without always changing the Code and recompile the whole thing.

I don’t think, that my question: “Is it possible” is completely invalid, just because YOU don’t need such options…?
Knowing how to enable specific parts in the configuration could be an interesting part in other areas, too… not only for BT …
It could also enable other configurations which you might need in some cases - and in others not.

You would have just much more flexibility in the end.

Anyway… thanks for your reply without any information.

I don’t think their is anything like a wifi.disable/wifi.enable action for bluetooth in esphome. :thinking:

Might want to raise a feature request over @ github :point_down:

But depending on your needs the wifi.disable probably disables bluetooth too… :wink:

yes, and it disables everything else I would STILL need the ESP for…
So… yes, if there’s no way for doing this, I will raise a feature request :slight_smile:

Well, not everything. Local automations and stuff continue to work but you obviously loose the “connected” (“smart”) feature of that esphome node :wink:

Do Automations Work Without a Network Connection

YES! All automations you define in ESPHome are executed on the ESP itself and will continue to work even if the WiFi network is down or the MQTT server is not reachable.

that’s why I wrote: “I would need the ESP for” :wink:
Because, that is the main purpose of the device in this particular case or more precise, in nearly all of my cases… :slight_smile:

1 Like

Why wouldn’t you need bt proxy in the kitchen when the plants are in the kitchen?

You could go to source code of Bluetooth proxy, create a writable property to do that. It doesn’t exist as of now.

On the other hand, there is custom component which might have this feature, ble_gateway and you can send the ble advertisement to custom passive ble monitor integration.

do you really think, I would not know that I might need the BT-Proxy there when the plant would just be placed aprox 2m ?

The plant will be placed to a complete different location for the winter… maybe the cellar… where I would then need another BL-proxy, because I don’t want to move my S3-Box from the kitchen to the cellar.
So … yep… I don’t need the BT Proxy in the cellar over summer times and I would not need the BT-Proxy over winter times in the kitchen - at least as of now.

Maybe, also relevant… before more questions will be asked… all the esp devices, that act as BT-Proxy do have other tasks, too… so BT-Proxy is just a “Side-Effect” which I want use.

The device in the cellar for example would read my water-meter… + having BT Proxy option as Bonus.
Some other ESP will be built in in Figures and Model-Kits, to controll lights, and so on… + Bonus having BT Proxy which I can just enable if required.

That being said - I just don’t want to recompile every ESP when I come into the situation, that I do need BT Proxy… it should be like a feature, that can be turned off if not required.

@anon63427907: Thanks… I don’t think, that I will be able to edit the source code for my requirements…
But I will have a look into the custom component to see, if that would enable the feature.

Nevertheless… I already found a feature request to implement an “enable / disable” option for the whole BLE stuff… it seems that I am not the only one witch such an idea.

Then again, having bt proxy on all the time will not do any harm and will strengthen your BT reachability and reliability. I am not trying to decry your use case, but I find it hard to tell why you want to turn it off.

The one thing I can think of is if the esp32 is not up to task of running bt proxy and whatever else you want it to do. bt proxy is quite resource intensive. But in that case you wouldn’t even want to turn bt proxy on seasonally, because whatever else you want it to do might stop working properly. You’d just get another dirt cheap esp32.

I guess I just don’t get it, but that’s OK, you understand your use case! I think I see you learning c++ …

  • Toggle Proxy
  • Toggle Scan
  • Toggle Active Scan
  • Toggle Continuous Scan

bluetooth_proxy:
  id: ble_proxy
  active: true

switch:
  - platform: template
    name: BLE Proxy
    icon: mdi:bluetooth-connect
    id: bluetooth_proxy_switch
    entity_category: diagnostic
    restore_mode: RESTORE_DEFAULT_ON
    optimistic: true
    lambda: |- 
      return id(ble_proxy)->has_active() == true;
    turn_on_action:
      - lambda: |- 
           id(ble_proxy)->set_active(true);
      - globals.set:
          id: bluetooth_proxy_active
          value: 'true'           
      - delay: 2s # wait for BluetoothProxy to start up
    turn_off_action:
      - lambda: |- 
           id(ble_proxy)->set_active(false);
      - globals.set:
          id: bluetooth_proxy_active
          value: 'false'                      
      - delay: 2s # wait for BluetoothProxy to close

#  - platform: template
#    name: BLE Scan
#    icon: mdi:bluetooth-transfer
#    id: ble_scan
#    entity_category: diagnostic
#    optimistic: true
##    internal: true
#    lambda: |- 
#      return id(esp32_ble_tracker_scanning) == true;
#    turn_on_action:
#      - esp32_ble_tracker.start_scan
#    turn_off_action:
#      - esp32_ble_tracker.stop_scan

  - platform: template
    name: BLE Scan Active
    icon: mdi:bluetooth-transfer
    id: ble_tracker_active
    entity_category: diagnostic
    optimistic: true
    lambda: |- 
      return id(esp32_ble_tracker_scan_parameters_active) == true;
    turn_on_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_active
          value: 'true'
      - lambda: |-
          ble_tracker->set_scan_active(true);
          ble_tracker->setup();
    turn_off_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_active
          value: 'false'
      - lambda: |-
          ble_tracker->set_scan_active(false);
          ble_tracker->setup();

  - platform: template
    name: BLE Scan Continuous
    icon: mdi:bluetooth-transfer
    id: ble_tracker_continuous
    entity_category: diagnostic
    optimistic: true
    lambda: |- 
      return id(esp32_ble_tracker_scan_parameters_continuous) == true;
    turn_on_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_continuous
          value: 'true'
      - lambda: |-
          ble_tracker->set_scan_continuous(true);
          ble_tracker->setup();
    turn_off_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_continuous
          value: 'false'
      - lambda: |-
          ble_tracker->set_scan_continuous(false);
          ble_tracker->setup();

Bonus: count how many connectinons it has:

sensor:
  - platform: template
    name: BLE Proxy
    icon: mdi:bluetooth-settings
    id: ${device_name}_esp32_ble_connections
    entity_category: diagnostic
    unit_of_measurement: "connections"
    accuracy_decimals: 0    
    update_interval: 60s    
    lambda: |-
      auto bt_proxy = id(ble_proxy);
      int connections = bt_proxy->get_bluetooth_connections_limit();
      int free = bt_proxy->get_bluetooth_connections_free();
      return (connections-free);
4 Likes

Hi, thank you so much :slight_smile:

I asume, that I need to create globals:

bluetooth_proxy_active
esp32_ble_tracker_scan_parameters_active
esp32_ble_tracker_scan_parameters_continuous

just similar to this?

globals:
  - id: bluetooth_proxy_active
    type: bool
    restore_value: yes
    initial_value: "false"

  - id: esp32_ble_tracker_scan_parameters_active
    type: bool
    restore_value: yes
    initial_value: "false"

  - id: esp32_ble_tracker_scan_parameters_continuous
    type: bool
    restore_value: yes
    initial_value: "false"

It compiled successfully
grafik

And here’s the complete code for all that are interested in:

globals:
  - id: bluetooth_proxy_active
    type: bool
    restore_value: yes
    initial_value: "false"

  - id: esp32_ble_tracker_scan_parameters_active
    type: bool
    restore_value: yes
    initial_value: "false"

  - id: esp32_ble_tracker_scan_parameters_continuous
    type: bool
    restore_value: yes
    initial_value: "false"

sensor:
  - platform: template
    name: BLE Proxy
    icon: mdi:bluetooth-settings
    id: ble_connections
    entity_category: diagnostic
    unit_of_measurement: "connections"
    accuracy_decimals: 0    
    update_interval: 60s    
    lambda: |-
      auto bt_proxy = id(ble_proxy);
      int connections = bt_proxy->get_bluetooth_connections_limit();
      int free = bt_proxy->get_bluetooth_connections_free();
      return (connections-free);

# Bluetooth
esp32_ble_tracker:
  id: ble_tracker
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: false

bluetooth_proxy:
  id: ble_proxy
  active: true

switch:
  - platform: template
    name: BLE Proxy
    icon: mdi:bluetooth-connect
    id: bluetooth_proxy_switch
    entity_category: diagnostic
    restore_mode: RESTORE_DEFAULT_ON
    optimistic: true
    lambda: |- 
      return id(ble_proxy)->has_active() == true;
    turn_on_action:
      - lambda: |- 
           id(ble_proxy)->set_active(true);
      - globals.set:
          id: bluetooth_proxy_active
          value: 'true'           
      - delay: 2s # wait for BluetoothProxy to start up
    turn_off_action:
      - lambda: |- 
           id(ble_proxy)->set_active(false);
      - globals.set:
          id: bluetooth_proxy_active
          value: 'false'                      
      - delay: 2s # wait for BluetoothProxy to close


  #  - platform: template
#    name: BLE Scan
#    icon: mdi:bluetooth-transfer
#    id: ble_scan
#    entity_category: diagnostic
#    optimistic: true
##    internal: true
#    lambda: |- 
#      return id(esp32_ble_tracker_scanning) == true;
#    turn_on_action:
#      - esp32_ble_tracker.start_scan
#    turn_off_action:
#      - esp32_ble_tracker.stop_scan

  - platform: template
    name: BLE Scan Active
    icon: mdi:bluetooth-transfer
    id: ble_tracker_active
    entity_category: diagnostic
    optimistic: true
    lambda: |- 
      return id(esp32_ble_tracker_scan_parameters_active) == true;
    turn_on_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_active
          value: 'true'
      - lambda: |-
          ble_tracker->set_scan_active(true);
          ble_tracker->setup();
    turn_off_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_active
          value: 'false'
      - lambda: |-
          ble_tracker->set_scan_active(false);
          ble_tracker->setup();

  - platform: template
    name: BLE Scan Continuous
    icon: mdi:bluetooth-transfer
    id: ble_tracker_continuous
    entity_category: diagnostic
    optimistic: true
    lambda: |- 
      return id(esp32_ble_tracker_scan_parameters_continuous) == true;
    turn_on_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_continuous
          value: 'true'
      - lambda: |-
          ble_tracker->set_scan_continuous(true);
          ble_tracker->setup();
    turn_off_action:
      - globals.set:
          id: esp32_ble_tracker_scan_parameters_continuous
          value: 'false'
      - lambda: |-
          ble_tracker->set_scan_continuous(false);
          ble_tracker->setup();

Looks great but active property of Bluetooth proxy correspondence to active listening (vs passive one). It is not enabling or disabling the proxy. Am i missing anything here?

I did some further tests with the code above and found the following (for my plant-Sensors, at least)…

turning all three switches off:
Plant-Sensor becomes unavailable

image
image

  • turning on Bluetooth Proxy allone:
    No change, all Sensors are unavailable
    ESP32-S3-Box is rebooting randomly

  • turning on Bluetooth Proxy + BLE Scan active:
    no change, all Sensors are unavailable

  • turning on BLE Continous Scan → (all others are off)
    Plant Sensors become available.

So, the BLE Continous Scan seems to activate the whole BLE Stack on the device, which will automatically enable the Bluetooth Proxy.

In this case, the other two switches, especially the one for the Bluetooth Proxy seems out of function.
But so far, it would fit my needs, with only enabling / disabling Bluetooth at all.

1 Like

Why even overcomplicate things by switching On/Off? Set up a BT only device somewhere hidden and out of the way in any rooms you need and then use seperate esp32 boards for whatever else. These boards are a few $$ not a few thousand $$ where you need to make 1 board do everything. Some things just shouldnt be overcomplicated.