I would like to use the bluetooth sensor of my Home Assistant companion app on android to remember my last parking location.
My plan is to do this via an automation. I think this is something that might be very helpful to other members as well.
In the android app I can see under Settings>Companion App>Manage Sensors>Bluetooth Connection under “paired_devices” that my car is known to the app. Also I can see which devices are currently connected under “connected_paired_devices”.
Also in HA I can see these states under developer tools.
When going in the automation section of HA (GUI not YAML) I can create a trigger from a change in state.
I can choose my smartphones sensor: [sensor.samsung_s_10_bluetooth_connection] and I can choose the attribute “connected paired devices”.
In the next field “From” I put in the MAC of my car’s bluetooth.
However, I struggle to get to the next step. I don’t know what to put into the attribute field for “To” . I tried to leave it blank or put in a zero but neither would trigger the automation.
I would appreciate your help to get this automation running.
Since the connected_paired_devices attribute returns a list, not a string you will need to alter your approach. For your situation, you want to fire the automation whenever that list goes from containing your car’s MAC to not containing it. You can’t specify static to or from values because the list (at least in theory) could contain any number of other MAC values at the same time it contains your car’s MAC value. AFAIK, there isn’t a way to specify this trigger without using templating.
trigger:
- alias: trigger on disconnect from car
platform: template
value_template: >
{{ state_attr('sensor.samsung_s_10_bluetooth_connection'
, 'connected_paired_devices') is not search('xx:xx:xx:xx:xx') }}
Hi thanks a lot!
I have so far not been working with templates.
When I entered your code into the " When a template triggers" section of the automation wizard nothing happened on disconnecting the bluetooth device. I changed the MAC accordingly.
I used this code:
trigger:
alias: trigger on disconnect from car
platform: template
value_template: >
{{ state_attr(‘sensor.samsung_s_10_bluetooth_connection’
, ‘connected_paired_devices’) is not search(‘08:DF:1F:AA:BE:00’) }}
Do you have any idea where the problem lies? Does it work for you? I did not test it with a car but a bluetooth speaker to make it easier. In the states section under developer tools I can see it being listed as connected or not connected.
Without proper formatting it is difficult to tell where the issue might be. Post your whole automation instead of showing it piecemeal… that way we don’t have to guess about possible issues.
With the appropriate MAC for my device, what I posted has been tested and does work for me.
Sorry for the broken formatting.
Here is the version I use:
I put it into the trigger section of “when a template triggers” and the result should be that I get a message sent to my mobile, reproving to current location. I can trigger the action manually so the sending bit should be fine, however, it would just not trigger it.
Post your actual automation configuration. Re-posting what I posted will not give us any clues about what is happening in your automation. You can access it from the Automation editor by clicking on three dot menu at the top right hand of the page, then click “Edit in YAML”. Select the complete contents and paste it here.
alias: TEST BLUETOOTH Disconnected
description: ""
trigger:
- platform: template
value_template: |-
trigger:
- alias: trigger on disconnect from car
platform: template
value_template: >
{{ state_attr('sensor.samsung_s_10_bluetooth_connection'
, 'connected_paired_devices') is not search('08:DF:1F:AA:BE:00') }}
condition: []
action:
- service: notify.mobile_app_samsung_s_10
data:
message: >-
The car is parked at: {{ states('sensor.samsung_s_10_geocoded_location')
}}
data:
"[object Object]": test bluetooth disconnected
mode: single
You made a copy/paste error… it’s easy to do with the way template triggers are shown in the UI automation editor.
alias: TEST BLUETOOTH Disconnected
description: ""
trigger:
- alias: trigger on disconnect from car
platform: template
value_template: >
{{ state_attr('sensor.samsung_s_10_bluetooth_connection'
, 'connected_paired_devices') is not search('08:DF:1F:AA:BE:00') }}
condition: []
action:
- service: notify.mobile_app_samsung_s_10
data:
message: >-
The car is parked at: {{ states('sensor.samsung_s_10_geocoded_location') }}
mode: single
I case you want a different approach this is what I’ve been using.
It creates a device tracker to show the vehicle location on the HA map instead of sending a notification of the location. But you can combine this with yours if you want both.
First you need to create a device tracker in your known_devices.yaml (create that file in your config directory if it doesn’t exist):
toyota_tacoma:
icon:
name: Toyota Tacoma
picture: /local/tacoma.jpg
track: true
then create a variable to save the GPS location. I use the custom integration hass-variables for this but you might be able to use your existing location sensor:
Oh, thanks for the inspiration! you are totally right, with the concept of a map rather than a push message. I was just not able to implement anything other than a message but I will give your approach a try!
I don’t understand what you are actually asking… Which part of the GUI? Which element of Bluetooth reporting? For that matter, OP’s question relates to the interaction between Home Assistant core and the Home Assistant Companion app, so which “HA” are you asking about?
I also don’t understand the “anymore”… as far as I can tell this is the way the referenced sensor attribute has worked since at least 2020.
I guess it’s a little bit of both. First, you’re comparing apples and oranges; trying to match a (partial) string to a list, which would never be true. Second, AFAIK the to and from are only set up to handle certain data types, and lists are not one of those.
If you want to use a State trigger with a specified attribute you can. Leave the to: field empty, then use a Template condition to search for your paired device within the attribute’s list.
You might see this as it “not working”, but it may be the only practical solution to address a situation where HA has no way to guarantee the order or content of the list of connected devices coming from your phone. Even if State triggers accepted lists in the to: field, the State trigger is a very literal tool and you would need to include every combination of connected devices for reliable functioning.
Thanks for your answers, I still think the GUI should handle this.
if the Bluetooth connected_paired_devices returns a list, the GUI should handle that, either by saying it’s not supported or by just handling it. Right now it doesn’t give any hints to what is wrong.
Since I specifically want to trigger when I loose connection to my car I need to know that it was actually connected to the car before I trigger anything. Thus I want to use From:
From what I can understand from the value_template it only checks the current status.
If I understand things correctly this will trigger every time I loose or connect a BT-device to my phone and I’m not in my car.
alias: Uppdatera bilen när BT kopplas bort
description: ""
trigger:
- platform: state
entity_id:
- sensor.pixel_6a_bluetooth_connection
attribute: connected_paired_devices
for:
hours: 0
minutes: 0
seconds: 0
condition:
- condition: template
value_template: |
{{ state_attr('sensor.pixel_6a_bluetooth_connection'
, 'connected_paired_devices') is not search('00:92:A5:05:29:75 (Kia)') }}
action:
- service: kia_uvo.force_update
data:
device_id: d92adcbbeac7763c72f8603919727ab6sdfsdf
mode: single
If you use the trigger variable in your template instead of using the state_attr() function you can access both the prior and new values for the attribute.
alias: Uppdatera bilen när BT kopplas bort
description: ""
trigger:
- platform: state
entity_id:
- sensor.pixel_6a_bluetooth_connection
attribute: connected_paired_devices
condition:
- condition: template
value_template: |
{% set car = '00:92:A5:05:29:75 (Kia)' %}
{{ car in trigger.from_state.attributes.connected_paired_devices and
not car in trigger.to_state.attributes.connected_paired_devices }}
action:
- service: kia_uvo.force_update
data:
device_id: d92adcbbeac7763c72f8603919727ab6sdfsdf
mode: single