Toggle switch on/off by battery (phone) level

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 and switch.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)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

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

4 Likes

Hello,

Your blueprint link is not working.

https://git.sr.ht/~cybolic/HomeAssistant/blob/main/blueprints/automation/toggle-switch-by-battery-level.yaml

This is the link to the blueprint when i try to import it. And it gives an error.
Unsupported url.

1 Like

Ah, thanks for pointing that out. I didn’t realise the importer only supports Github.
I’ve updated the link to use this forum post, but Home Assistant is not parsing it correctly when I try to import it and I have no idea why.
If anyone has any idea what the secret to writing the post so the importer will load it is, I’m all ears :slight_smile:

Figured it out :slight_smile: The blueprint has to be at the bottom of the post.
The blueprint import link works now.

It’s working.
Thank you very much. :smiley:

Hello. Thank you for this blueprint. It does almost exactly what i want. But i need some help. Would it be possible to do the following:
-Turn off switch if battery is below 80%
-Turn on switch if battery is above 85%
-Person state: ignore, always run it
How could i accomplish this?
Regards

I am also interested in this as I will be using this for a tablet that usually plugged in at my desk.

1 Like

Hi,

I’m using this blue print and it works nice.
I personally have a additional requirement but I can’t figure auto how to add it.

The idea is pretty simple, since I own many power plugs and shelly’s I want to apply this charging automation (blueprint) to a specific powerplug or shelly based on a helper where I can define in which plug my charger currently is.

This enables me to use different power plugs all around my house, whiteout bounding this automation to one plug specific.

Who is able to hint me in the right direction?

That is exactly why i love Home Assistant. I was looking for a way to automate my wall mounted old smartphone for my living room controlls. I hooked it up with the fully kiosk browser and the plug-in for HA and now it wont charge the whole day but just when its needed. AWESOME. Thank you.

I’d like to have this option too.

Still no update about the Always run it option anyone ?

This looks like the blueprint for me. found a few simeler ones but this seems to have toe most functionality. Are people finding this one workes well or have people been using others?

I can update you,

I opened the Blueprint in the Files (HACS extention), and just got rid of the code relating to persons:

  • Trigger
  • Variable

Saved it, Thats it.

Same same with the person question i assume (have not trried it).

Go to the Files and the Blueprint, Copy paste it and give it another name.

Should work.

First i would try to download the blueprint again and give it another name.

Interesting… I may give it a try. I already have an automation that turns a smart plug off for the power for a Nexus 9 tablet that we use as a photo frame, turning the plug off when tablet battery reaches 80% and back on when it reaches 20%. I resorted to that after replacing the battery after 12 months for the second time. Now, instead of being on power 24 hours a day, it only needs charging about 4 hours a day. I figure since the Nexus 9 doesn’t have adaptive charging, this is a good alternative. However, your blueprint has the bedtime feature which I hadn’t considered. Perhaps I could leverage it to force the tablet to charge at night when it is off instead of during the day (see graph).