Automation Condition Improperly Evaluates False

Is there an issue with testing state of a MAC address in the condition?

id: '1659662784853'
alias: Update Honda Location - Jason Phone Bluetooth
description: ''
  - platform: event
    event_type: state_changed
      entity_id: device_tracker.jason_phone
  - condition: state
    entity_id: sensor.jason_s_phone_bluetooth_connection
    attribute: connected_paired_devices
    state: 01:02:03:04:05:06
  - service: device_tracker.see
      dev_id: car_honda
        - '{{ state_attr(''device_tracker.jason_phone'', ''latitude'') }}'
        - '{{ state_attr(''device_tracker.jason_phone'', ''longitude'') }}'
mode: single

The condition never evaluates true. I’ve tried placing quotes around the state but the UI seems to strip them out. Here’s a piece of the trace

Executed: August 4, 2022 at 9:59:18 PM
result: false
Executed: August 4, 2022 at 9:59:18 PM
result: false
  - 01:02:03:04:05:06
wanted_state: 01:02:03:04:05:06

What am I doing wrong?

Probably a UI bug. Try using only the YAML editor and quoting the MAC.

  - condition: state
    entity_id: sensor.jason_s_phone_bluetooth_connection
    attribute: connected_paired_devices
    state: '01:02:03:04:05:06'

Thanks. My thought is that it’s a UI bug too, but I need to test it out in pure YAML. I hesitate to use that as a permanent solution because I’m making an effort to use more UI Automations where I can. I’ll raise the issue as a possible bug, if necessary.

Can the condition statement be modified in anyway, perhaps to easily strip out the colons before evaluating?

Only with a template condition, and if you are using that then you may not have to:

  - condition: template
    value_template: "{{ is_state_attr('sensor.jason_s_phone_bluetooth_connection', 'connected_paired_devices', '01:02:03:04:05:06') }}"

Stripped out colons it would be:

  - condition: template
    value_template: "{{ state_attr('sensor.jason_s_phone_bluetooth_connection', 'connected_paired_devices')|replace(':', '') == '010203040506') }}"

That’s exactly what I’m looking for! Unfortunately, I get the following error:

## Error occurred while testing condition

template value should be a string for dictionary value @ data['conditions'][0]['value_template']. Got None

Format looks right to me. I played with some quotes, too. Both examples give errors.

There’s currently a bug with the UI condition template checking. Ignore it and check the template in Developer Tools → Templates.

Thanks for heads up on the bug. I ignored it and sucessfully tested in Dev Tools… Unfortunately, in real life testing, the condition is still evaluating false!

Executed: August 5, 2022 at 6:44:39 AM
result: false
Executed: August 5, 2022 at 6:44:39 AM
result: false
  - sensor.jason_s_phone_bluetooth_connection

Try this version; it’s essentially equivalent to tom_l’s suggestion to use replace but employs slugify.

It uses the slugify filter to convert the the value of connected_paired_devices to a slug (basic ASCII characters only) and then compares it to the desired string but represented as a slug.

  - condition: template
    value_template: "{{ state_attr('sensor.jason_s_phone_bluetooth_connection', 'connected_paired_devices') | slugify == '01_02_03_04_05_06' }}"

Test the template in the Template Editor first and, if it’s successful, try it in the automation’s Template Condition.

1 Like

By golly, you’ve helped me find the actual error! And, unfortunately, exposed a few problems…

Testing in the Dev Tools, I get the following error: “TypeError: decoding to str: need a bytes-like object, list found.” Seems this is returning a list (of one item), which is why I may have kept getting an evaluation of false.

I can reference the list’s first element, but if doesn’t exist (i.e., paired connection not present) I will still get an error. Also, there exists the possibility that I get get a list with an array length >1. Probably unlikely, but it exists. Therefore, a full and robust solution would require searching an array of undetermined length provided that its length is >0. Ugh.

Is it better to iteratively search the array or concatenate the elements and search for a substring? I’d think the latter because I can this disregard zero lengths.

Also, for anyone else following along, slugify strips CAPS. This is not apparent in my bogus sequentially numbered MAC address.

Try this condition then:

  - condition: template
    value_template: "{{ '01:02:03:04:05:06' in state_attr('sensor.jason_s_phone_bluetooth_connection', 'connected_paired_devices') }}"

A ready-made function! Love it. That did the trick. Thank you!

1 Like

That’s definitely why all of the suggested comparison techniques failed to find a matching value.

Out of curiosity, what originally led you to believe the received value was simply a string?

What does the following template report in the Template Editor? Ensure it’s the only line you paste into the editing window (no other information or even blank lines).

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

Honestly, it was just ignorance, really… I didn’t know and didn’t take the time to determine the datatype. However, with hindsight being what it is, in my original post, you’ll see that the trace DID place a dash in the state, which I assume did indicate that it was returning a list of 1. We all missed that!

With no connections, I get the following results:


Using another attribute of this entity (paired_devices, meaning all stored pairings) with listed items, I get the following:


I cannot repeat your test at the moment with one connection but I’m assuming it would be the same as above w/ one entity.

Thanks for carrying out the test. It confirm the value is a list (empty or with one or more items).