Need help with interesting template

Hello,
I am strugling few days with one template and need some help :slight_smile:

I have an entiry sensor.s24ultra_bluetooth_connection and it has one of attribute - paired_devices:

paired_devices: A0:D3:65:D3:94:9B (INTUNE-4SJ0S34), C6:32:34:36:3E:84 (QIMIC WS-21006), 04:52:C7:C3:17:08 (LE-Sheep Queen), 98:52:3D:F2:79:73 (Soundcore Liberty 2 Pro), FF:FF:10:E3:B7:C2 (Selfie), 98:52:3D:F2:39:34 (Soundcore Liberty 2 Pro-L), A4:04:50:43:BD:C5 (My Skoda 8129)

How to make a template to check for example if string FF:FF:10:E3:B7:C2 (Selfie) or word “Selfie” exists in this attribute - give true, else give false.

I would be grateful for help.

Assuming paired_devices contains a comma-delimited string, this ought to work.

{{ 'Selfie' in state_attr('sensor.s24ultra_bluetooth_connection', 'paired_devices') }}

If it fails to work then the value isn’t simply a string.

It shows false in a result, but it should be true… As I understand it returns an array when there is more then one

But it works like this:

{{ 'FF:FF:10:E3:B7:C2 (Selfie)' in state_attr('sensor.s24ultra_bluetooth_connection', 'paired_devices') }}

This also suits :slight_smile: thanks.

Then it means the value is not a string but a list (and the value sample you posted was misleading).

The fact it worked only when you supplied the full MAC address with “(Selfie)” confirms it’s a list.

Here’s the optimal way to search for a partial string within a list.

{{ state_attr('sensor.s24ultra_bluetooth_connection', 'paired_devices') is search('Selfie') }}

If you want to learn more about it, copy-paste the following example into the Template Editor and observe the results.

Partial string in a string (True).
{{ 'Selfie' in 'bla, bla, bla Selfie, blabla' }}

Partial string in a list (False).
{{ 'Selfie' in ['bla', 'bla', 'bla Selfie', 'blabla'] }}

Complete string in a list (True).
{{ 'bla Selfie' in ['bla', 'bla', 'bla Selfie', 'blabla'] }}

Partial string in a list (True).
{{ ['bla', 'bla', 'bla Selfie', 'blabla'] is search('Selfie') }}

thank you.

1 Like

You’re welcome but despite me showing you how to do it, you marked your own post as the Solution. :thinking:

yes as it worked with a full string including mac address, but you pointed me to the correct direction. I remarked your post as solution :slight_smile:

1 Like