Binary_sensor Templates are driving me bonkers

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

Use modern format or UI helpers (Settings / Helpers / Create / Template / Binary). Don’t use ChatGPT: it is terrible at HA.

Please paste this into Developer Tools / Template:

{{ state_attr('sensor.oneplus_9_bluetooth_connection', 'connected_paired_devices') }}

and post the result here when the Oneplus phone is connected to your Audi.

1 Like

I thank you very much for your attempt to help, but forgive me, I didn’t understand. Maybe it’s my low level of English, or maybe Home assistant, I didn’t understand.
I only understood the second part and this is the result.
In my opinion it’s a problem with spaces… non-existent spaces appear

[
  "EF:98:00:39:33:84 (Forerunner 935)",
  "9C:DF:03:2F:AB:A7 (Audi UHV 8290           )"
]

You are using old code style. Also, do not use ChatGPT for HA.

You could do this:

{{ state_attr('sensor.oneplus_9_bluetooth_connection',
              'connected_paired_devices')
   |select('contains','Audi')
   |select('contains','9C:DF:03:2F:AB:A7')
   |list|count > 0 }}

1 Like

yes, now work with this my last code:

      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 }}

but are you telling me that it’s an old system and I should update my sensors created with a new method? Oh my God, I don’t know where to start, but it works like this now, and I don’t know how to include your beautiful code in my sensor :slight_smile: I apologize for that

      oneplus9_connect_to_a3:
        friendly_name: "Vale in A3"
        value_template: >
          {{ state_attr('sensor.oneplus_9_bluetooth_connection',
                        'connected_paired_devices')
             |select('contains','Audi')
             |select('contains','9C:DF:03:2F:AB:A7')
             |list|count > 0 }}

Yes. Delete all your code for these four sensors.

Re-create them in the UI, like my screenshot:

Settings, then Devices & Services:

image

and Helpers:

image

then Create:

image

Find template ( {} icon):

image

Template a binary sensor:

image

Then fill in the box like my screenshot in my last post; repeat for the other three with MAC and BT ID as needed.

Expand for all the templates

Vale in A3:

{{ state_attr('sensor.oneplus_9_bluetooth_connection',
              'connected_paired_devices')
   |select('contains','Audi')
   |select('contains','9C:DF:03:2F:AB:A7')
   |list|count > 0 }}

Vale in Ecosport:

{{ state_attr('sensor.oneplus_9_bluetooth_connection',
              'connected_paired_devices')
   |select('contains','EcoSport')
   |select('contains','C4:F3:12:E9:0E:D4')
   |list|count > 0 }}

Diego in A3:

{{ state_attr('sensor.pixel_7_pro_bluetooth_connection',
              'connected_paired_devices')
   |select('contains','Audi')
   |select('contains','9C:DF:03:2F:AB:A7')
   |list|count > 0 }}

Diego in Ecosport:

{{ state_attr('sensor.pixel_7_pro_bluetooth_connection',
              'connected_paired_devices')
   |select('contains','EcoSport')
   |select('contains','C4:F3:12:E9:0E:D4')
   |list|count > 0 }}
1 Like

Fantastic! done and arranged the 4 bluetooth template binary sensors

Now I wanted to fix all the other sensors, but here for example, with the template it is not possible to also consider device_class, delay_on and, delay_off in the code.

Work only with {{ states('sensor.presa_vialetto_power') | int(default=0) > 5 }}
but I would like all this

  - platform: template
    sensors:      
      stato_operativo_presa_vialetto:
        friendly_name: "Presa Vialetto ON"
        value_template: >
          {{ states('sensor.presa_vialetto_power') | int(default=0) > 5 }}
        device_class: power
        delay_on:
          seconds: 5
        delay_off:
          seconds: 10

Yeah, there are some things you cannot do with UI helpers. You can set the device class, but not the delay parameters.

1 Like

ok, I’ll keep the old code then. Thank you

1 Like