Automations: Condition Attribute state not working

Hi,

trying to trigger automation only when driving my car. I have a trigger when entering a zone.

I am checking if my mobile is connected with car via bluetooth:

condition: state
entity_id: sensor.mi_6x_polaczenie_bluetooth
attribute: connected_paired_devices
state: "74:D7:CA:AD:30:4C (Toyota Touch)"

Unfortunately when I check condition in GUI via test button, I always get Condition did not pass.

I was checking state of the attribute in Developpers tools with

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

and got above attribute as result.

What am I doing wrong with my automation?

Regards,

Maciek

Developpers tools

 {{ '74:D7:CA:AD:30:4C (Toyota Touch))' in state_attr('sensor.mi_6x_polaczenie_bluetooth', 'connected_paired_devices') }}

Perhaps I was not clear enough.

On DevTools all works fine. It is just automation I have problem with.

Thanks. Tried value_template before, but it did not work, not work, not sure if it matters that attribute value is more than just MAC?

I think the automation should look something like this, of course with your own trigger and action.

The condition should be like this, to my best knowledge

description: "test automation"
mode: single
trigger:
  - platform: state
    entity_id:
      - input_boolean.aa_test_boolean
condition:
  - condition: template
    value_template: >-
      {{ '74:D7:CA:AD:30:4C (Toyota Touch))' in
      state_attr('sensor.mi_6x_polaczenie_bluetooth',
      'connected_paired_devices') }}
action:
  - device_id: b81f18d9f8a302b925c324f95ce05f45
    domain: button
    entity_id: button.530e_flash_lights
    type: press

The Test button has a known bug that causes it to fail when testing Template Conditions. Until it’s fixed, I suggest you don’t rely on it to test your Template Conditions.

FWIW, there was a PR recently submitted to fix it but, due to some issues, it hasn’t been accepted yet.

  - platform: template           
    sensors:
      in_my_car:
        friendly_name: "Connection to the car "
        value_template: >
            {% if "50:4F:B5:07:9C:CA (MB-7079CDA)" in state_attr('sensor.sm_g975f_bluetooth_connection', 'connected_paired_devices') -%}                
               on
            {% else -%}
               off
            {% endif -%}

Automations

platform: state
entity_id:
  - sensor.in_my_car
to: "on"
...

Thanks, I didn’t know IT.

Thanks a lot!

For the record, you didn’t need to create a Template Sensor to solve the problem you originally reported.

automation

description: "test automation"
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.sm_g975f_bluetooth_connection
    value_template: >
      {% if "74:D7:CA:AD:30:4C (Toyota Touch))" in
      state_attr(sensor.mi_6x_polaczenie_bluetooth',
      'connected_paired_devices') -%}                
          3
      {% else -%}
          1
      {% endif -%}
    above: 1
condition: []
action:
  - device_id: b81f18d9f8a302b925c324f95ce05f45
    domain: button
    entity_id: button.530e_flash_lights
    type: press

That’s a curious application of a Numeric State Trigger. It’s simpler to use a Template Trigger.

alias: example 
trigger:
  - platform: template 
    value_template: >
      {{ "74:D7:CA:AD:30:4C (Toyota Touch))" in state_attr(sensor.mi_6x_polaczenie_bluetooth', 'connected_paired_devices') }}
condition: []
action:
  ... actions go here ...  
1 Like