Automation condition - checking in BT connected_paired_devices for a MAC address

I’m trying to solve a similar problem to the one here, but I don’t quite understand why my value template conditional doesn’t pass and hoping someone could help me, or point me to some references.

In my case, I’m wanting to check whether my phone is connected to my car via BT as an automation condition. I understand that the connected_paired_devices attribute will return a list. I’m trying to search for my car’s BT MAC address (60:38:0E:54:3C:CA) in the returned list like this but it never works and I don’t understand why (I’m a novice at this):

      - condition: template
        value_template: >-
          Value: {{ '60:38:0E:54:3C:CA' in
          state_attr('sensor.ant_samsung_s22_sm_s901e_bluetooth_connection',
          'connected_paired_devices') }}

I always get “Condition did not pass” in the Visual Tester when I manually test (with my phone actively connected to my car of course). I’m guessing the way I’m searching for the MAC Address in the returned list isn’t valid, but I’m not sure of the right/best way to go about it. I’m a little confused as I’ve seen posts where other people used this same method and said it worked for them.

In HA, when I go to the sensor.ant_samsung_s22_sm_s901e_bluetooth_connection entity and check the Attributes, I do see my car’s MAC address (60:38:0E:54:3C:CA) in the list of Connected paired devices.

Any pointers greatly appreciated!

I’m running Home Assistant in a Hyper-V VM:
Core 2024.1.3
Supervisor 2023.12.0
Operating System 11.4
Frontend 20240104.0

You have an unnecessary Value: in the template and will need to flatten the possible list from the sensor into a string - try:

      - condition: template
        value_template: >-
          {{ '60:38:0E:54:3C:CA' in
          state_attr('sensor.ant_samsung_s22_sm_s901e_bluetooth_connection',
          'connected_paired_devices') | string }}

Best thing to do in this case is test the value_template with the Developer Tools templating tool to check you receive a simple true / false return value.

A condition always has to be either true or false. And if it’s not true then it defaults to false by definition.

“Value: true” isn’t a valid boolean value. it’s a key value pair string.

the test you are using should work (it doesn’t need to be converted to a string since it already is a string in the attribute) as long as the MAC address string you are looking for is correct.

If the sensor you are using is from the HA Companion app then at some point in the past the string that is used in the “connected_paired_devices” attribute changed to include the device name. So at that time I needed to change my test to this:

"{{ '84:DD:20:AA:FF:EE (TOYOTA Tacoma)' in state_attr('sensor.my_mobile_app_bluetooth_connection', 'connected_paired_devices') }}"

you should check the attributes of your sensor to make sure you are checking for the right entire string not just the MAC portion.

Thanks for the feedback. I had noticed that the device name was being included and had also tried that as well, but same result.

I’m currently testing the automation using my smartwatch which is connected to my phone via BT. It is showing up in the entity attributes as follows:


and the Condition template code I’m using is:

      - condition: template
        value_template: >-
          Value: {{ '28:3D:C2:A4:72:01 (Galaxy Watch5 Pro (7KKW))' in
          state_attr('sensor.ant_samsung_s22_sm_s901e_bluetooth_connection',
          'connected_paired_devices') }}

and when I test it I still get “Condition did not pass” in the Test so I’m absolutely baffled.
I should mention that I’m using the HA Companion app (2023.12.4-full) on an Android phone (Samsung Galaxy S22) as the source of the sensor.

Thank you for your feedback! This worked and solved my problem!
I’m still a little confused why what I had wasn’t working but clearly I have a lot more learning to do to understand this!

Did you read the first part of my post above where I explained in a bit more detail why it didn’t work?