Home Assistant - Automatically turn on / turn off bluetooth on your smartphone

Home Assistant - Automatically turn on / turn off bluetooth on your smartphone

Good sunday guys,

In this article we saw how to turn on / turn off manually the bluetooth on your smartphone.

Today we'll learn how to create an automation to do this for us.

Use case

Your smartwatch is connected via bluetooth to your smartphone but, if for some reason you don't have the smartwatch on your wrist, keeping bluetooth enabled on your smartphone is useless as well as probably a cause for an undesired battery's consumption.

Software configuration

  1. Home Assistant. I'm currently running version?2022.8.0.
  2. Mobile App integration?configured in Home Assistant. This integration allows Home Assistant mobile apps (Android / IOS) to integrate with your Home Assistant environment.
  3. The companion app must have the bluetooth connection sensor enabled.
  4. The companion app must have the permissions to modify the bluetooth connection (turn on / turn off).

Home Assistant code / procedure (yaml)

Add the entity sensor.yoursmartphone_bluetooth_connection to your dashboard.

This entity provides the attribute connected paired devices that refers to the list of paired devices that are currently connected:

No alt text provided for this image

When the smartwatch is not connected to your smartphone the list will be empty.

Taking into the account the behaviour described above, we can configure an auotmation to automatically turn off the bluetooth according to this trigger.

This is the piece of code you have to write down:

alias: Smartphone - Automatically disable bluetooth if not connected to my smartwatch
description: Smartphone - Automatically disable bluetooth if not connected to my smartwatch for one minute
trigger:
? - platform: template
? ? value_template: >-
? ? ? {{state_attr("sensor.yoursmartphone_bluetooth_connection",
? ? ? "connected_paired_devices") == [ ]}}? ?
? ? for:
? ? ? hours: 0
? ? ? minutes: 1
? ? ? seconds: 0
? ? ? milliseconds: 0
condition: []
action:
? - service: notify.mobile_app_yoursmartphone
? ? data:
? ? ? message: command_bluetooth
? ? ? data:
        command: turn_off
mode: single        

Some specifics about the code above

  1. The trigger is based on a template.
  2. To filter out quick and multiple bluetooth disconnections, the trigger must be true for 1 minute (you can set any value of course).
  3. When the automation has fired, the action will turn off the bluetooth via a notification command. If you have updated your Android APP to the latest 2022.8 version, you must use command in place of title.

Thank you for reading. ?? I do hope you have enjoyed this article.

I'm available for any feedback so, feel free to contact me or share your thoughts in the comments section below!

Meanwhile,?if you liked this article share it or?subscribe?to my newsletter?about?Home Assistant?to read previous episodes and still up to date on latest news.

Arwyn Harris

Civil Contingencies Advisor at Met Office

1 年

slight tweak needed in your code - i needed the 3rd from last line to read "data:" rather than "data" to get it working. All good now though and it's a great tip - thanks

回复

要查看或添加评论,请登录

Emiliano Guerzoni的更多文章

社区洞察

其他会员也浏览了