This blueprint will turn off the smart plug/relay after the mobile device is charged to a given percent. It will also turn it back on when the device is discharged. It does only when a given person is located at home.
a mobile device with HA official mobile app installed
It does not check if the plug has indeed a consumer attached (I didāt have a plug with power control).
You can import the blueprint using this topic URL.
blueprint:
name: Stop/start charging based on battery level when home.
description: Turn off the smart plug/relay after the device is charged to a given
percent. Turn it back on when the device is discharged.
domain: automation
input:
switch:
name: Smart plug
description: Smart plug/relay to turn off
selector:
entity:
domain: switch
phone_battery:
name: Phone Battery
description: Battery level sensor
selector:
entity:
domain: sensor
device_class: battery
charged_percent:
name: Charged percent
description: When charged up to this percent, turn off the plug/relay
selector:
number:
min: 1.0
max: 100.0
unit_of_measurement: '%'
mode: slider
step: 1.0
default: 100
discharged_percent:
name: Discharged percent
description: When discharged down to this percent turn on the plug/relay
selector:
number:
min: 0.0
max: 99.0
unit_of_measurement: '%'
mode: slider
step: 1.0
default: 20
person_home:
name: Person
description: Person that should be home for the plug to turn off
selector:
entity:
domain: person
variables:
phone_battery: !input 'phone_battery'
charged_percent: !input 'charged_percent'
discharged_percent: !input 'discharged_percent'
trigger:
- platform: state
entity_id: !input 'phone_battery'
action:
- choose:
- conditions:
- condition: template
value_template: '{{ (states(phone_battery)|int) >= (charged_percent|int) }}'
- condition: state
entity_id: !input 'person_home'
state: home
sequence:
- service: switch.turn_off
data: {}
entity_id: !input 'switch'
- conditions:
- condition: template
value_template: '{{ (states(phone_battery)|int) <= (discharged_percent|int) }}'
sequence:
- service: switch.turn_on
data: {}
entity_id: !input 'switch'
default: []
I would appreciate (as many others) some form of autmation based on your next alarm.
Since weāre already using the HA app to get the battery percantage, there is a parameter for the next alarm. I suggest some form of condition - (variable) amount of time before the next alarm -> keep the plug on regardless of battery state.
I think this would work better if the switch on function didnāt rely on the battery level but rather the battery state. That way as soon as the device reports discharging then the switch will be turned back on and be ready for use again untill it hits the target battery level.
The mobile device reports discharging the very next second after the power is disconnected.
So, if the switch is turned off at 90%, the device reports immediately it is discharging, then the switch is turned back on. Because the battery is still 90%, it would enter in a loop of turn off / turn on.
it worked for me, however my phone (OnePlus Nord) takes minutes to let know HA the battery % almost always the phone reach 100% when the plug goes off, any idea to have a more frequently update?
I guess you could try to disable deep sleep mode, but the latency really isnāt that large in my opinion. Is there a specific reason youād like to speed up the reaction time?
To turn off deep sleep: settings > battery > battery optimization > top right corner 3 dot menu advanced optimization>turn off sleep standby optimization
Great blueprint but what determines how often or when the companion app updates?
I had it set on my iPhone as a test and battery level reached to turn on but nothing happened, it was until minutes later something triggered my phone to update it sent the battery level and it all kicked in.
I noticed that it tied in with the last update sensor, not looking to have it update every 5 milliseconds but wondering what determines the update timing.
My intention is to use on the wifeās phone so she can plug it overnight and itāll charge to 100% and then plug will turn off, iām wondering if I need to set it say 98% then by the time it gets updated into HA the automation will run and itāll be at 98 or more, what I donāt want is it sat there at 100% plugged in for 2-3 hours until the phone finally āupdatesā, this is what Iām trying to avoid.
Not sure what itās like for iPhones to be honest. You could test it a few times and see what happens, but normally it really shouldnāt take 2h for the app to update its sensors.
Wanting to use the same feature for Sonos Roam.
Roam turns itself off after 3 minutes (unavailable), so i have to wake it up every ones in a while to check the battery level. (turn plug on and off to wake up).
Any interest in adding this feature to your blueprint?
I am having the same issue as well. Set up this blueprint last night on my OnePlus and it charged to 98% before turning off.
My settings were turn off the switch when the charge reaches 85%. So it charged an additional 13% before it turned off.
I am wondering if the person.user condition is what is causing this issue as the sensor is not updating frequently enough to check to see if the user is at home.
I experienced the same delay as iPhone user to report sensors informations to home assistant. The operating system decide when and in what condition report.
I solved this making a iOS automation using the apple application to trigger home assistant every threshold battery changes.
itās very simple. here is a couple of screen.
I use the official apple application for automation on ios (the name is traslated according your language)
Creating an automation which just update sensors using the HA ios companion app installed, the the complete automation is done on HA.
So i can charge my phone all night without overcharging or this way also my wall tablet can alway be connected to power without stay always full charged.
I actually use the exact same setup for a long time - just never thought about pushing it to HA in addition to only send some local notification messages. Usually HA has some delay of sometimes few minutes so I didnāt have an actual need for this shortcut adaption. But it doesnāt harm so thx