Need a little help with a binary sensor/template

I am hoping for some assistance to add a binary sensor (true/false) …that gets its state from the below entity’s attribute connected_paired_devices (when it’s value includes 30:C0:1B:2D:07:24 in it).

The below template is as far as I got. I’m not even sure if I’m going about it the right way. My guess is I also need a binary sensor. However, I’m not sure. I’d really appreciate some assistance to move forward with this.

- platform: template
  sensors:
    jbl_bluetooth_connected:
      value_template: >-
              {% set connected_not_paired_device = state_attr('sensor.sm_n960u_bluetooth_connection', 'connected_paired_devices') %}
              {% if (connected_not_paired_device | regex_match(find="30\:C0\:1B\:2D\:07\:24")) %}
                true
              {% endif %}

Try omitting the find= part of the argument to regex_match. You can use the template tab in developer tools to fiddle around with templates to test things.

Thanks for your suggestion. I pasted the code in the template editor (removing find=). I’m not sure if I’m doing this right. It’s returning a value of 0.0.

try this template:

value_template: "{{ '30:C0:1B:2D:07:24' in state_attr('sensor.sm_n960u_bluetooth_connection', 'connected_paired_devices') }}"

it should return true or false so you should be able to use it in a template binary sensor instead of as a regular template sensor.

EDIT:

but if you are on v117 you might need to convert the attributre data type to a string:

value_template: "{{ '30:C0:1B:2D:07:24' in state_attr('sensor.sm_n960u_bluetooth_connection', 'connected_paired_devices') | string }}"
1 Like

Thanks. Worked like a charm. Much simpler than I thought!

1 Like