Condition phone bluetooth connection

I’m trying to create a condition in an automation that checks if my phone is connected to either of our cars.
I’ve tried a few ways, using the visual editor:

condition: state
entity_id: sensor.pixel_6_pro_bluetooth_connection
attribute: connected_paired_devices
state: 28:A1xxxxx:9D (SUBARU BT)

and the template with the ‘in’ function:

condition: template
value_template: "{{ '28:A1xxxx:9D (SUBARU BT)' in state_attr('sensor.pixel_6_pro_bluetooth_connection', 'connected_paired_devices') }}"

When testing either of these, it reports “Condition Did Not Pass”.
I’ve checked in Developer Tools the actual state/attribute value when the car is connected, and it shows exactly the value i’m testing for, but it still reports False.

sensor.pixel_6_pro_bluetooth_connection
Pixel 6 Pro Bluetooth Connection
0	state_class: measurement
connected_not_paired_devices: 
connected_paired_devices: 28:A1:xxxx:9D (SUBARU BT)

Surely there’s something simple here i’ve missed, any help would be appreciated.

There are a couple an existing threads discussing templating the bluetooth connection attribute.

This template from @123 is working for me…

condition: template
value_template: >
    {% set c = state_attr('sensor.pixel_6_pro_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('28:A1xxxx:9D') }}
6 Likes

Thank you so much for this. I had a good look at a few other posts but didn’t come across this one.
Seems it has something to do with sensor updating.
Regardless, using your code with the ‘search’, even with partial match (SUBARU) works great. Thankyou!

I’ve been getting very frustrated with this… As far as I can tell template conditions NEVER evaluate to true…

I tried your example:

condition: template
value_template: >
    {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('DD:****:0C') }}

which always evaluates to false. I even tried:

condition: template
value_template: >
    {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none }}

and that always evaluates to false…

I have tried various variations with None and none (I have seen both used in other posts) and have tried != as well as is not. Nothing seems to work… What am I doing wrong!!!

BTW - I hate yaml with a fiery passion and don’t understand the syntax. I am not sure if that’s wrong perhaps.

Thanks

In your example in the picture there are missing the double quotes wrapping the template, thats why the condition does not pass.
The template with the bluetooth connection looks good, what is the result in the developer tools when you paste this under template?

{{ state_attr('sensor.pixel_fold_bluetooth_connection', 'connected_paired_devices') }}
1 Like

I’ve not seen the developer tools before. That’s going to make it much easier to test!!

[
  "94:****:BE (Google Pixel Watch 2)",
  "DD:****:0C ((BLE)Edge 1030)"
]

So I have used the developer tools to verify that this returns true:

    {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('DD:***:0C') }}

but when I paste it into an automation condition I always get “This condition does not pass”

condition: template
value_template: >
    {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('DD:****:0C') }}

Have you actually tested the automation to see if the condition is working properly? There have been issues in the past with the Automation Editor’s Condition tester not working reliably with Template conditions.

Well it didn’t work last night before I started editing it today… It’s not that easy to test as I need to be cycling home. I’d need to cycle out of my home zone and back in each time I want to test it.

Was it stopped by the condition or did the trigger not fire? What does the debug trace show?

Automation fired, condition was not met

Then i would suggest the bluetooth connection is not always on due to powersavings or something like this from the phone. For double checking it, you could create a binary sensor:

template:
  - binary_sensor:
      - unique_id: is_my_bluetooth_device_connected
        state: >
          {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 'connected_paired_devices') %}          
          {{ c is not none and c is search('DD:***:0C') }}
        attributes:
          friendly_name: "is_my_bluetooth_device_connected"

Don’t forget to replace the MAC with the original one, the template don’t accept wildcards (afaik). Then after cycling you can see in the history of the sensor if the bluetooth was always connected or not. And if yes, you could use the binary_sensor in the condition.

Thanks for the reply. I tried creating that sensor. In the developer tools my template evaluates to true, by binsary sensor it always marked as disconnected though.

Dev tools:

    {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('DD:DB:F1:****:0C') }}

Sensor:

template:
  - binary_sensor:
      - unique_id: is_my_bluetooth_device_connected
        state: >
          {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
    'connected_paired_devices') %}
    {{ c is not none and c is search('DD:DB:F1:****:0C') }}
        attributes:
          friendly_name: "is_my_bluetooth_device_connected"

You mean the binary sensor is always off (which means the mac is not found in the sensor or any other problem the template don’t evaluate to true)?
I am not sure why, maybe the indentation might be the problem. Try to indent it like this:

template:
  - binary_sensor:
      - unique_id: is_my_bluetooth_device_connected
        state: >
          {% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 
          'connected_paired_devices') %}
          {{ c is not none and c is search('DD:DB:F1:****:0C') }}
        attributes:
          friendly_name: "is_my_bluetooth_device_connected"

I hate yaml with a fiery hatred!!! :slight_smile:

I tried copy and pasting yours and it didn’t help.

In the developer tools I can see the template evaluate to true when I connect my device and return to false when I turn it off. The binary sensor show nothing other than Disconnected though… :frowning:

This is getting extremely frustrating (but thanks very much for trying to help)

There is actually no YAML required to create this binary sensor.

You can go to:
Settings > Devices & Services > Helpers
Open your Home Assistant instance and show your helper entities.
There you can create a new template helper, and choose template a binary sensor
Paste the jinja template in the right field
(so only this part)

{% set c = state_attr('sensor.pixel_fold_bluetooth_connection', 'connected_paired_devices') %}
{{ c is not none and c is search('DD:DB:F1:****:0C') }}

Woohoo! That works.

It was most definitely user error! I was doing as you had suggested - ussing the template helper to create the sensor but I was pasting in the whole yaml rather than just the template.

Hopefully this will now open my garage door for me the next time I cycle home! It really is a PITA having to dig through my bag for my phone in the dark to open it!

Many thanks to all for your continued efforts to solve this for me!

Ah, that explains a lot of confusion.
The whole YAML was meant to be pasted into your configuration.yaml :slight_smile:

ok, sorry for that. You can’t edit configuration.yaml through the web UI can you? Do I have to edit the file via the file system?

If you are using HAOS, you can install an add-on for that. I recommend the Studio Code Server add-on

Open your Home Assistant instance and show the dashboard of an add-on.