Binary_sensor Templates are driving me bonkers

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.

Reference: Rate limiting updates

I’ll perform a test and get back to you with the result.

1 Like

Yep, that’s what I read too, it would only update after HA restart, Reloading the configs, or testing within the template tool, guessing it was hitting rate limiting. So I opted to make it trigger based, so that it would always work. As It was not, always working without a trigger.

It’s not “hitting rate limiting”. Re-read the documentation explaining when rate limiting is applied.

The result of my experiment confirms the template is updated whenever there’s a change in the value of a referenced entity’s state or its attributes. In other words, it behaves like an implicit State Trigger for each entity in the template.


FWIW, the suggested Template Binary Sensor was also confirmed to work in Jgree321’s previous post.

Back in august, when I made this post - and when I eventually added and setup these templates, it needed the triggers, otherwise it wouldn’t update. I’m just posting what has been working for me, since august of last year, still today, on 2023.1.7.

I have not gone through to remove triggers, only stating what was working since then, and continues to work. I did not see a need to remove them from my templates, I only saw responses to the thread I created, and added what I am still doing. Mainly to show the other user how to put templates in separate files

I’m not implying you are wrong, on the current latest HAOS build. As well, if you scroll up, even you early in this post suggested triggers because the sensors were not updating.

If you scroll up further you’ll see it was eroosenmallen who was first to suggest employing a trigger (and their supplied example had incorrect syntax). My first post in this topic simply suggested to employ the trigger variable in the template (I didn’t introduce or advocate for the use of a State Trigger, merely to reference its result).

My only interest in this topic is that what’s suggested in the Solution post may mislead other users to believe it’s the optimal way to solve the issue. It’s a way to solve it but not one that others should emulate.

Thanks!
Took me a few tries but managed to get it moved over to a file.
Realized I guess you don’t need the template: mapping when it’s being included from file
Having the -binary_sensor: also got me. Had to remove -

I’m also going crazy with the binary sensors linked to Bluetooth, I also tried to get help from Chat GPT which turned my sensors upside down, but the result doesn’t change, 3 out of 4 work, but one never works and I don’t understand why.
I would like to create a binary sensor that is ON when a phone is connected to a certain Bluetooth device, well, if it is the pixel that is connected, it works on both A3 and Ecosport, if it is Neplus 9 that is connected, it only works if connected to Ecosport, but not when connected to Audi UHV 8290

  - platform: template
    sensors:
      oneplus9_connect_to_ecosport:
        friendly_name: "Vale in Ecosport"
        value_template: >
          {% set devices = state_attr('sensor.oneplus_9_bluetooth_connection', 'connected_paired_devices') %}
          {{ devices and 'C4:F3:12:E9:0E:D4 (Ford EcoSport)' in devices }}

      pixel7pro_connect_to_ecosport:
        friendly_name: "Diego in Ecosport"
        value_template: >
          {% set devices = state_attr('sensor.pixel_7_pro_bluetooth_connection', 'connected_paired_devices') %}
          {{ devices and 'C4:F3:12:E9:0E:D4 (Ford EcoSport)' in devices }}

      pixel7pro_connect_to_a3:
        friendly_name: "Diego in A3"
        value_template: >
          {% set devices = state_attr('sensor.pixel_7_pro_bluetooth_connection', 'connected_paired_devices') %}
          {{ devices and '9C:DF:03:2F:AB:A7 (Audi UHV 8290)' in devices }}

      oneplus9_connect_to_a3:
        friendly_name: "Vale in A3"
        value_template: >
          {% set devices = state_attr('sensor.oneplus_9_bluetooth_connection', 'connected_paired_devices') %}
          {{ devices and '9C:DF:03:2F:AB:A7 (Audi UHV 8290)' in devices }}

If anyone has any suggestions, I would be grateful. I preferred to post again below, instead of creating a new thread, considering that the problem could be similar