How to check if '00:21:CC:0A:35:DF' is in an attribute list?

I want to test if ‘00:21:CC:0A:35:DF’ is found in sensor.vog_l04_bluetooth_connection connected_paired_devices. The state attributes are:

connected_not_paired_devices: '[]'
connected_paired_devices: '[AC:E3:42:71:9E:E9]'
paired_devices: >-
  [E0:7D:EA:42:D1:07, 40:16:3B:AD:9A:BD, C2:BB:9F:89:01:45, AC:E3:42:71:9E:E9,
  C4:A2:60:01:85:76, D4:81:CA:F0:A1:CA, 08:EF:3B:24:2E:C7, C9:C1:54:1B:88:C4,
  90:F1:AA:F9:36:AB, 00:23:11:00:00:20, 00:21:CC:0A:35:DF, 1C:12:B0:B4:DE:89,
  00:23:15:00:3C:AA]
unit_of_measurement: connection(s)
friendly_name: VOG-L04 Bluetooth Connection
icon: 'mdi:bluetooth'

The BT device I am checking for is not currently connected.
This template in dev tools returned the element to examine:

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

Then I added testing for string equality:

{% if is_state_attr('sensor.vog_l04_bluetooth_connection','connected_paired_devices','[AC:E3:42:71:9E:E9]') %}
  true 
{% endif %}

and it returned True.

What’s the next step to return true/false if the list contains ‘00:21:CC:0A:35:DF’ ?
Thanks.

I continued searching, reading, testing and came up with this:

{{ state_attr('sensor.vog_l04_bluetooth_connection','connected_paired_devices') }}
{% set bt_devices = state_attr('sensor.vog_l04_bluetooth_connection', 'connected_paired_devices').split(',') %}
{% for device in bt_devices %}
{% if device == '[00:21:CC:0A:35:DF]' %}
  true
{% else %}
  false
{% endif %}
{% endfor %}

Would there be another shorter method?

This will return true if the address is in the list/string:

"{{ '00:21:CC:0A:35:DF' in state_attr('sensor.vog_l04_bluetooth_connection','connected_paired_devices') }}"
1 Like

Thank you so much! I knew there had to be a better way than what I lashed together!!
That’ll complete my automation to open garage door/unlock front door…

alias: Daniel arrives home
description: ''
trigger:
  - platform: state
    entity_id: person.daniel_brunt
    to: home
condition: []
action:
  - service: alarm_control_panel.alarm_disarm
    data: {}
    entity_id: alarm_control_panel.alexa_guard_da03a
  - choose:
      - conditions:
          - type: is_plugged_in
            condition: device
            device_id: 52fa40cb8ef749a1a7800d6495551219
            entity_id: binary_sensor.vog_l04_is_charging
            domain: binary_sensor
          - condition: or
            conditions:
              - condition: template
                value_template: >-
                  "{{ '00:21:CC:0A:35:DF' in
                  state_attr('sensor.vog_l04_bluetooth_connection','connected_paired_devices')
                  }}"
        sequence:
          - type: toggle
            device_id: b6b8158202d211eba4bc95be153ab261
            entity_id: switch.mimolite_switch
            domain: switch
          - wait_for_trigger:
              - type: not_plugged_in
                platform: device
                device_id: 52fa40cb8ef749a1a7800d6495551219
                entity_id: binary_sensor.vog_l04_is_charging
                domain: binary_sensor
              - platform: template
                value_template: >-
                  "{{ '00:21:CC:0A:35:DF' not in
                  state_attr('sensor.vog_l04_bluetooth_connection','connected_paired_devices')
                  }}"
            timeout: '01:00'
            continue_on_timeout: false
          - type: toggle
            device_id: b6b8158202d211eba4bc95be153ab261
            entity_id: switch.mimolite_switch
            domain: switch
      - conditions:
          - type: is_not_plugged_in
            condition: device
            device_id: 52fa40cb8ef749a1a7800d6495551219
            entity_id: binary_sensor.vog_l04_is_charging
            domain: binary_sensor
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: sun.sun
                    state: below_horizon
                sequence:
                  - type: turn_on
                    device_id: b6b7e97b02d211ebb345b1a87b2c5b26
                    entity_id: switch.nzw30_smart_switch_switch
                    domain: switch
            default: []
          - service: logbook.log
            data:
              name: Daniel Arrives Home
              message: Wait for motion trigger...
              domain: automation
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_a2154d04_ias_zone
          - wait_for_trigger:
              - type: motion
                platform: device
                device_id: ad660333093c11ebaadee17e5246204a
                entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_a2154d04_ias_zone
                domain: binary_sensor
            timeout: '00:05:00'
            continue_on_timeout: false
          - service: logbook.log
            data:
              name: Daniel Arrives Home
              message: 'Motion detected, unlock front door'
              domain: automation
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_a2154d04_ias_zone
          - device_id: b6f24cd502d211ebb38ba1e9f8bad99f
            domain: lock
            entity_id: lock.zm1701_electronic_deadbolt_door_lock
            type: unlock
    default: []
mode: single

1 Like