This automation will switch on your smart plug/relay if your phone (or any other battery) drops below a minimum percentage and turn it off again when it reaches a maximum percentage. It also includes a toggle to allow for a higher maximum during certain hours to allow for full charging before bed, as well as a condition that the owner of the device should be in a certain state before triggering (i.e. don’t switch the plug if I’m not home).
Requirements
- A switch that can be turned on/off with
switch.turn_on
andswitch.turn_off
- A battery level sensor, such as the one in the Home Assistant app
- A person entity, such as the one created by the Home Assistant app
Note
While posting this, I noticed there’s also a similar blueprint that just turns a plug off at a certain battery level, so if that’s all you need, take a look here: 🔋 Turn off phone charging after the phone is charged
Disclaimer
This is my first blueprint and I have a suspicion things could be made simpler by calculating more using variables.
Update
I moved the set
variables into their own variables
block for easier debugging and also used the battery state directly instead of through the trigger
. I think this make the blueprint a bit easier to work with.
The URL is now also a proper git repo instead of a one-off, so updates should be automatic now.
Update 2
I simplified the logic to use choose
instead of a template and also let the plug stay untouched if the battery level is not in a state that needs action.
The import link to this post doesn’t work though and I have no idea why.
Figured it out: the blueprint has to be at the bottom of the post.
Update 3
Added trigger for person state change, so the charger can turn on when you get home.
Blueprint
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
Or you can import this Blueprint by using the forum topic URL:
blueprint:
name: Toggle charger by battery
description: Switch a smart-plug on or off depending on a battery's level
domain: automation
input:
battery:
name: Battery
description: This battery's level will control the smart plug
selector:
entity:
domain: sensor
device_class: battery
person:
name: Person
description: But only when this person is in the specified state
selector:
entity:
domain: person
state:
name: Person State
description: The state the person must be in
default: home
target_switch:
name: Smart Plug
description: The smart plug to switch on or off
selector:
entity:
domain: switch
charge_min:
name: Lowest Battery Charge
description: The lowest charge the battery should be allowed to drop to
selector:
number:
min: 0
max: 95
step: 5
unit_of_measurement: "%"
mode: slider
default: 30
charge_max:
name: Highest Battery Charge
description: The highest charge the battery should be allowed to reach
selector:
number:
min: 5
max: 100
step: 5
unit_of_measurement: "%"
mode: slider
default: 80
use_bedtime:
name: Charge fully before bed
selector:
boolean:
default: True
bedtime:
name: Bedtime
description: Allow full charge after this hour
selector:
time:
default: '22:00'
bedtime_latest:
name: Latest Bedtime
description: Stop allowing full charge after this hour
selector:
time:
default: '03:00'
charge_full:
name: Full Battery Charge
description: The battery is considered full at this percentage
selector:
number:
min: 75
max: 100
step: 5
unit_of_measurement: "%"
mode: slider
default: 85
variables:
battery: !input battery
charge_min: !input charge_min
charge_max: !input charge_max
charge_full: !input charge_full
use_bedtime: !input use_bedtime
bedtime: !input bedtime
bedtime_latest: !input bedtime_latest
trigger:
- platform: state
entity_id: !input battery
- platform: state
entity_id: !input person
condition:
- condition: state
entity_id: !input person
state: !input state
action:
- variables:
charge: '{{ (states(battery) | float) if is_number(states(battery)) else 1 }}'
is_bedtime: '{{ use_bedtime == True and ( now() >= today_at(bedtime) or now() < today_at(bedtime_latest) ) }}'
c_max: '{{ charge_full if is_bedtime else charge_max }}'
c_min: '{{ charge_full if is_bedtime else charge_min }}'
- choose:
- conditions: >
{{ charge < c_min }}
sequence:
- service: switch.turn_on
target:
entity_id: !input target_switch
- conditions: >
{{ charge >= c_max }}
sequence:
- service: switch.turn_off
target:
entity_id: !input target_switch
Importing Blueprint:
https://community.home-assistant.io/t/about-blueprints