Binary_sensor Templates are driving me bonkers

@Didgeridrew @123 Thank you very much for all of your responses. I really appreciate both of your inputs and reading through my responses. :smiling_face_with_three_hearts:

1 Like

For testing I just used this template binary sensor config and it works exactly as expected:

template:
  - binary_sensor:
      - name: "My Phone Connected To Car Bluetooth"
        state: >-
          {{state_attr('sensor.my_mobile_app_bluetooth_connection', 'connected_paired_devices') != [] 
            and (('84:DD:DD:62:FA:FE' in state_attr('sensor.my_mobile_app_bluetooth_connection','connected_paired_devices')) 
            or ('84:DD:DD:62:FA:FE' in state_attr('sensor.my_mobile_app_bluetooth_connection','connected_paired_devices')))}}

it switches from ‘on’ to ‘off’ as my phone connects and disconnects from my car bluetooth.

the ‘connected_paired_devices’ is indeed a list so it needs the bracket notation to test if it’s empty.

Not sure if it’s useful anymore tho.

1 Like

I’m trying to get the binary sensor working for bluetooth connected devices.
Sensor is turned on in companion app settings and I can view state of the bluetooth_connection in state viewer and I see the device in the connected list.

Copied the template exactly as above but no matter what I do it always returns false anytime I list a MAC address. The only way I can get it to return true is only using

state_attr('sensor.my_mobile_app_bluetooth_connection', 'connected_paired_devices') != []

When I use that it will return true.

Show what you have that doesn’t work.

there were changes to the way that the bluetooth devices show up in the attributes.

Here is the full template I was using.
Fought for a while getting it to work at all until I realized I was missing the include in config file for templates…
I’ve tried adding the full string which includes the device name and that didn’t work either.

template:
    - binary_sensor:
    - name: "My Phone Connected To Car Bluetooth"
         state: >-
            {{state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') != [] 
            and (('D0:1B:49:DC:32:84' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices'))
            or ('DC:0D:30:19:B1:27' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices')))}}

Ok, I see the problem.

at some point HA required that you need to test in the template that the attribute you are testing is actually defined:

{{ state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') is defined 
            and (('D0:1B:49:DC:32:84' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices'))
            or ('DC:0D:30:19:B1:27' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices')))}}

I tested the template with my devices and it seems to work.

I updated the template as shown:

template:
  - binary_sensor:
      - name: "My Phone Connected To Car Bluetooth"
        state: >-
          {{ state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') is defined 
            and (('D0:1B:49:DC:32:84' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices'))
            or ('DC:0D:30:19:B1:27' in state_attr('sensor.le2125_bluetooth_connection','connected_paired_devices')))}}

It’s still just staying “Off”. Restarted HA also after update
Could I be missing some other necessary include in the config.yaml?
Still new to HA but I appreciate your help

Post a screenshot of Developer Tools / States for that entity.

Try this version:

template:
  - binary_sensor:
      - name: "My Phone Connected To Car Bluetooth"
        state: >-
          {% set c = state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') %}
          {{ c is not none and c is search('D0:1B:49:DC:32:84|DC:0D:30:19:B1:27') }}

I would that before you create the Template Binary Sensor, test the template in the Template Editor.

{% set c = state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') %}
c is: {{ c }}
{{ c is not none and c is search('D0:1B:49:DC:32:84|DC:0D:30:19:B1:27') }}

Getting close. Your latest version works in template editor showing True.
Still staying off when used in config file. I suspect I have some formatting wrong in my config.yaml
Giving me a warning about a duplicated mapping key on line 27: right after the template:

# Loads default set of integrations. Do not remove.
default_config:
# Include Templates
template: !include_dir_merge_named templates/

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

proximity:
  home_me_ft:
    zone: home
    devices:
      - person.xxx_xxxxx
    tolerance: 50
    unit_of_measurement: ft

template:
  - binary_sensor:
    - name: "My Phone Connected To Car Bluetooth"
      unique_id: test
      state: >-
          {% set c = state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') %}
          c is: {{ c }}
          {{ c is not none and c is search('D0:1B:49:DC:32:84|DC:0D:30:19:B1:27') }}

You have two template: declarations. Put your new template in a separate file in your reposts templates/ folder.

Remove the template: line from the start, so it starts with - binary_sensor:

Ok. Could you point me to where I can read more about how to go about this?
I created a new folder and a file within the folder containing the template: declaration above
I then added a line:
packages: !include_dir_named conf
File is within a sub folder of the folder named conf
Now I’m getting an error in the config.yaml file trying to use the -binary sensor:
Failing here lol

Why did you add the packages line?

Did you already have a yaml file in your templates/ folder? If not, revert to what you had before and remove the template: !include... line.

Originally the templates folder was empty…
I realize that error now.

I removed the include statement from config.yaml which left me with this which shows no errors.
In the developer template tools it will report true. So confused why it will not when it resides in the config file

# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

proximity:
  home_me_ft:
    zone: home
    devices:
      - person.xxx_xxxxxx
    tolerance: 50
    unit_of_measurement: ft

template:
  - binary_sensor:
    - name: "My Phone"
      unique_id: test
      state: >-
        {% set c = state_attr('sensor.le2125_bluetooth_connection', 'connected_paired_devices') %}
        c is: {{ c }}
        {{ c is not none and c is search('D0:1B:49:DC:32:84|DC:0D:30:19:B1:27') }}

This post seems to reference an issue like that but the code you suggested seems what was reported to work there

I don’t know if you noticed but what you added to your configuration.yaml file is not what I suggested. Your version has different indentation and uses the wrong template; it uses the template I suggested to use in the Template Editor for experimentation. The one used in the Template Binary Sensor is only two lines, not three.

I had sort of noticed, in that I realized the {{ c }} was just showing a printout in the template editor.
I had deleted that from the config file but it wasn’t working.
So I just tried again and pasted exactly what you listed and it finally went to ON!
Wow…
Wish I understood exactly where it was breaking down but just glad it finally worked. I think I’ll leave it alone for now and tackle creating the template in another file later. Sure I’ll break it again trying that.
I’m a recent convert from Smartthings after webcore was broken and I’m enjoying HA. A lot to learn though.

Sorry, I misled you by accident.

There was a requirement in (I think…) markdown cards to test that the attribute exists but the issue with your original template wasn’t that.

it should have worked as written but you needed to add the entire listed name in the test.

The reason it worked in my test is because I used the complete entry and not just truncated to the mac address.

So in case you care my full working example is now:

{{ ('00:18:6B:4E:AA:BB (LG HBS730)' in state_attr('sensor.my_mobile_app_bluetooth_connection','connected_paired_devices'))
  or ('84:DD:20:CC:AA:BB (TOYOTA Tacoma)' in state_attr('sensor.my_mobile_app_bluetooth_connection','connected_paired_devices'))}}

But the solution above works and is a bit smaller. I just wanted to clarify/correct my post above.

This is what has been working for me since starting this post

configuration.yaml

# Include Templates
template: !include_dir_list templates/

templates/brad_phone_car_bluetooth.yaml

trigger:
  - platform: state
    id: brad_phone
    entity_id: sensor.brad_pixel_6_pro_bluetooth_connection
binary_sensor:
  - name: "Brad Phone Connected To Car Bluetooth"
    state: >-
           {% if trigger.id == 'brad_phone' %}
             {% set c_d = state_attr('sensor.brad_pixel_6_pro_bluetooth_connection', 'connected_paired_devices')%}
             {{(c_d!=None) and ('08:76:95:92:A7:B7' in c_d) 
             or ('00:32:A0:03:47:1B' in c_d)}}
           {% endif %}

templates/brittney_phone_car_bluetooth.yaml

trigger:
  - platform: state
    id: brittney_phone
    entity_id: sensor.brittney_pixel_6_bluetooth_connection
binary_sensor:
  - name: "Brittney Phone Connected To Car Bluetooth"
    state: >-
           {% if trigger.id == 'brittney_phone' %}
             {% set c_d = state_attr('sensor.brittney_pixel_6_bluetooth_connection', 'connected_paired_devices')%}
             {{(c_d!=None) and ('08:76:95:86:E7:9D' in c_d)}}
           {% endif %}

@Jgree321 the above, if you are looking to maintain your templates as separate files. :ok_hand:

Which gives you the ability to keep your templates separate by a nice folder structure

In your Trigger-based Template Binary Sensor shown below, why does it bother to check the value of the trigger.id when there’s only one possible value (because there’s only one trigger)?

In addition, why does it even need a trigger? Without a trigger, the template will be processed every time there’s a change in the value of the sensor’s state or one of its attributes.

trigger:
  - platform: state
    id: brad_phone
    entity_id: sensor.brad_pixel_6_pro_bluetooth_connection
binary_sensor:
  - name: "Brad Phone Connected To Car Bluetooth"
    state: >-
           {% if trigger.id == 'brad_phone' %}
             {% set c_d = state_attr('sensor.brad_pixel_6_pro_bluetooth_connection', 'connected_paired_devices')%}
             {{(c_d!=None) and ('08:76:95:92:A7:B7' in c_d) 
             or ('00:32:A0:03:47:1B' in c_d)}}
           {% endif %}

If you’re interested, it can be reduced to a simple Template Binary Sensor (i.e. not Trigger-based):

  - binary_sensor:
      - name: "Brad Phone Connected To Car Bluetooth"
        state: >-
          {% set c = state_attr('sensor.brad_pixel_6_pro_bluetooth_connection', 'connected_paired_devices') %}
          {{ c is not none and c is search('08:76:95:92:A7:B7|00:32:A0:03:47:1B') }}

Because it was not actually updating the sensor, without the trigger checking the state.

As well I named the trigger, as I utilize the trigger named elsewhere in other templates or automations, so I can rely on one trigger, instead of having duplicates throughout my configs.