Assistance needed for automation - bluetooth connection sensor

Hi, I’m new to HA and would like to apologize first for my silly question below:

I’m trying to set up an automation that when my phone is connected to my car’s bluetooth HA app then send a notification. The bluetooth connection sensor in the HA app is activated. My automation yaml is below but it simply won’t work.

Any help will really be appreciated.

alias: send a notification when connected to car bluetooth
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.oppo_bluetooth_connection
    attribute: connected_paired_devices
    to: 2C:DC:AD:C5:A8:C6 (VW)
condition: []
action:
  - service: notify.mobile_app_oppo
    data:
      message: Car Bluetooth Connected
mode: single

I have solved this with a template. State triggers would (often) not work since there may be more connected devices and the order might change also:

trigger:
  - platform: template
    value_template: "{{ '2C:DC:AD:C5:A8:C6 (VW)' in state_attr('sensor.oppo_bluetooth_connection', 'connected_paired_devices') }}"

This should work, if your mac adress is in the attribute the trigger would fire.

1 Like

It worked!

Thank you so much for your speedy reply.

My first post on this forum and resolved my problem instantly! Absolutely impressive!

Glad i could help. No matter if it is the first or the hundreds post, in this community there is always somebody who helps with your problems, i was also impressed the first time my (simple) questions were solved.
The community around home assistant is really big and great.

1 Like

First things first, if your problem is solved, it is always a nice idea to mark the corresponding post (in this case the first answer from @madface) as solution. It not only shows the appreciation for the one who helped, it also shows the thread as solved, so others can either skip (no more help needed) or look into the thread for a working solution. Thanks for that. :slight_smile:

I’d also like to offer another solution or better an explanation, why your code @Okcool might be not working. :slight_smile: The main problem is, you’re trying to do “everything” in the trigger. In cases like this, it is mostly better, to split this into trigger and condition. This is because triggers are very specific in when they fire the trigger event.

My guess is, that is what @madface meant by “State triggers would (often) not work”. Here is the part from the support page, that is interesting:

Fires when the state of any of given entities changes. If only entity_id is given, the trigger will fire for all state changes, even if only state attributes change. If at least one of from, to, not_from, or not_to are given, the trigger will fire on any matching state change, but not if only attributes change. To trigger on all state changes, but not on changed attributes, set at least one of from, to, not_from, or not_to to null.

So in your case I’d try it like this:

trigger:
  - platform: state
    entity_id: sensor.oppo_bluetooth_connection
condition:
  - condition: state
    entity_id: sensor.oppo_bluetooth_connection
    attribute: connected_paired_devices
    state: "2C:DC:AD:C5:A8:C6 (VW)"

That way it triggers with every change of the state and attributes of the sensor and checks afterwards if the change is something to react on.

And another note (as well from the docs):

Use quotes around your values for from and to to avoid the YAML parser from interpreting values as booleans.

So, yes, welcome to the loony bin (forum)! :slight_smile: :wave: And just to be clear, the way @madface suggested is correct and valid in the same way mine is, it is just a personal choice! :slight_smile:

1 Like

@paddy0174

Good explanation. Your way should work also, but when anything changes in the entity or the attributes it would fire again, even if the car is still connected and this wasn’t the change.
What i meant with state triggers (often) not work (especially in this scenario) had another reason:
The connected_paired_devices attribute can hold more than one device (for example a smartwatch which is connected always). When there is more than one device it is complicated to do this with a state trigger (you don’t know which devices are connected and what is the order in the attribute). So in my case it was the best to check if the mac adress is in the attribute, no matter how many devices are connected and what is the order of them.
As always, there are many approaches to solve a problem :slightly_smiling_face:

Very good point, I haven’t thought about other connected devices. :smiley: I tend to overlook things I don’t use (or hate) myself, and why on earth would I want to know, how many more steps I should have done today? :rofl: :rofl:

@ paddy0174 madface

Appreciate all your help!

Have marked Mike’s post as solution and leant a lot from the advices:)

1 Like

I’m guessing things have moved on since this comment as the op should work according to the docs?

I’m having the same issue with it not triggering though, and whilst I could create a template sensor for the attribute, I need to understand why it’s not!

When using the automation editor any quotes are removed, so this is no longer the case?