How to use bluetooth connection sensor?

Now that we have a sensor dedicated to bluetooth connections, anyone tried to implement it in any way?
To be more straightforward, I have a few use cases based on specific connected BT devices, but my phone is also constantly connected to my swartwatch and/or BT headphones, so using value from connected_paired_devices attribute is not gonna work, I need to somehow filter the specific mac and use it as a trigger. Unfortunately I’m not really good at templating :pensive:
Any help would be greatly appreciated.

I’ve used it as an alernative to another use case that was brought up in the thread linked below.

it starts here and I’ve updated things as the thread progressed.

I have this in a function node of Node red.

var wasConnected = msg.data.old_state.attributes.connected_paired_devices.includes("4E:C3:9E:5B:9C:86");

if(wasConnected){
    var isConnected = msg.data.new_state.attributes.connected_paired_devices.includes("4E:C3:9E:5B:9C:86");
    if(!isConnected){
        msg.BTtrigger = "true";
    }else{
        msg.BTtrigger = "false";
    }
}else{
    msg.BTtrigger = "false";
}
//4E:C3:9E:5B:9C:86
return msg;

This is to trigger on when you disconnect from a specific device.
In this case it’s a Bluetooth device in the car, and when this happens the msg.BTtrigger becomes “true”.

EDIT:
Now that I look at my code I see that this is a better approach…

var wasConnected = msg.data.old_state.attributes.connected_paired_devices.includes("4E:C3:9E:5B:9C:86");

msg.BTtrigger = "false";
if(wasConnected){
    var isConnected = msg.data.new_state.attributes.connected_paired_devices.includes("4E:C3:9E:5B:9C:86");
    if(!isConnected){
        msg.BTtrigger = "true";
    }
}
//4E:C3:9E:5B:9C:86
return msg;

It does the same thing in fewer lines…

2 Likes

Here’s the template I use to detect if my headphones are still connected to my phone when I arrive home (and periodically thereafter).

      phone_headphones_jason_alert:
        delay_on:
          minutes: 10 # wait until in house, settled
        value_template: >
          {% set p = is_state('device_tracker.jason_headphone','home') %}
          {% set c = '00:18:6B:97:9B:B9' in state_attr('sensor.jphone_bluetooth_connection','connected_paired_devices') %}
          {{ is_state('input_boolean.presence_automation','on')
              and is_state('binary_sensor.alerts_enabled','on')
              and is_state('binary_sensor.jason_home','on')
              and ( p or c )
              and not is_state('alert.phone_mobile_headphone_jason','off') }}

And in the tts announcement…

{% set c = '00:18:6B:97:9B:B9' in state_attr('sensor.jphone_bluetooth_connection','connected_paired_devices') %}
    Jason, get it together. You've left your headphones on again{{ ', and they are still connected to your phone!' if c else '.' }}

@finity you might find this interesting, I just finished reading your parked car thread this is not as fancy but just as useful!

5 Likes

Hey @jazzyisj. You don’t have to be that mean to yourself. And definitely don’t have to write scripts to get machines to scould you to ‘get it together’. I think that you writing scripts to help you not be forgetful shows how incredibly good you are at coping with a totally ordinary flaw. I think you can be proud of that.

1 Like

HA! That is one of the nicest things “Stella” (we have affectionately named our TTS voice) says to me everyday. It’s all in good fun. Visitors certainly get a kick out of my house telling me off on the regular :laughing:

Funny story - a few years back one of the people running in our local municipal election stopped by to give us their spiel. At the time the reminder announcement to feed our pets said “Hey as####le, it’s time to feed yer frickin’ animals!” and of course it went off while the fella was there talking. The look on his face was priceless. I did ultimately decide to tone that one down a bit. :rofl:

1 Like

I am using Bluetooth connection sensor as a trigger to fetch the latest data from my car. When my connection is dropped between my phone and my car, i am initiating the automation.

My template:
{{ state_attr("sensor.fuatx3pro_bluetooth_connection", "connected_paired_devices") is search(states('input_text.kia_bt_mac'), ignorecase=True)|int }}

I am using an input text here to store the mac value of the car Bluetooth adapter

I’ve used this a lot for opening the correct garage when I’m coming home in one car or the other.

Having just switched to an iPhone, I’m wondering if similar functionality exists there? The Bluetooth connection sensor and its connected_paired_sevices doesn’t seem to exist at all.

Has anyone done a clever workaround for iPhone?