Phone charger on/off by battery level

Needs a phone that reports battery level to HA and a switch controlling the charger. Automatically turns off charger when the battery level reaches the defined high value and turns back on when the battery level drops to the low value.

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

blueprint:
  name: Charge phone
  description: Charges phone until a percentage and then turns off until percentage
    drops below a defined point.
  domain: automation
  input:
    phone_battery:
      name: Phone Battery level
      description: This sensor will be used to determine the charge level of the phone
      selector:
        entity:
          domain: sensor
          device_class: battery
    phone_charger:
      name: Phone Charger
      description: Entity that controls supplying power to the phone
      selector:
        target:
          entity:
            domain: switch
    low_battery:
      name: Low Battery
      description: Battery below which the phone should be charged
      selector:
        number:
          min: 10.0
          max: 95.0
          unit_of_measurement: percentage
          mode: slider
          step: 1.0
    charged_battery:
      name: Charged Battery
      description: Battery above which the phone should stop charging (should be greater
        then Low Battery)
      selector:
        number:
          min: 80.0
          max: 100.0
          unit_of_measurement: percentage
          mode: slider
          step: 1.0
  source_url: https://github.com/apollo1220/blueprints/blob/39a53c112c215be2ffd2968cadd192d6a3acc261/phone_charger.yaml
mode: single
max_exceeded: silent
trigger:
- platform: numeric_state
  entity_id: !input 'phone_battery'
  below: !input 'low_battery'
- platform: numeric_state
  entity_id: !input 'phone_battery'
  above: !input 'charged_battery'
action:
- choose:
  - conditions:
    - condition: numeric_state
      entity_id: !input 'phone_battery'
      above: !input 'charged_battery'
    sequence:
      - service: homeassistant.turn_off
        target: !input 'phone_charger'
  default:
    - service: homeassistant.turn_on
      target: !input 'phone_charger'
    - wait_for_trigger:
      - platform: numeric_state
        entity_id: !input 'phone_battery'
        above: !input 'charged_battery'
      continue_on_timeout: false
    - service: homeassistant.turn_off
      target: !input 'phone_charger'
6 Likes

I don’t see how to specify the charger controlling switch using the UI. Am I missing something?

It’s the “Phone Charger”, second item in the list.

1 Like

Ah, got it, thanks. The row of buttons rather than a drop down threw me. I read “Area” and stopped looking at that line :wink:

is it broken ? since update i must unlock phone and open HA to get relay on / off

Yeah something has messed it up. It’s been seemingly randomly working and not working for me lately.

Maybe you want to test Wallpanel Battery Management - #15 by seamus65 which is basically doing the same (and there are no reports it doesn´t work anymore).

1 Like

still a noob to all this, I have 2 automations for my wallpanel one for on and one for off for charging, but that still works so glad I did not install any of these as I thought it would be cleaner ill try to adapt it to a blueprint to help out

It isn’t universal. There isn’t actually anything wrong with the blueprint itself. It is certain phones not sending their current charge level to Home Assistant in a timely matter that makes them “not work”. There is nothing that can be done from a blueprint or an automation to get the battery level correctly, they can only read the value Home Assistant makes available. So it still completely works, just there is a delay at the Home Assistant level in some cases.

1 Like

On IOS, you can use Shortcuts to: When: “battery level is 80%” Do: “Home Assistant - Update Sensors”.

2 Likes

For info … Android 12 now has this functionality built in: Settings - Battery and Device Care - Battery - More Battery Settings - Protect Battery

3 Likes

after doing something along those lines i thought maybe trying to making it a little more complex but figured i should see if anyone already done it.

Right now I’ve only added zone to my automation that only work if i’m at home.

Thinking of adding a condition to only work between hours so i don’t accidently wake up with my phone on 30% or something. Not really a huge deal since it’s plugged during the drive to work and i can always charge it at my desk but i tend to leave it unplugged if i can help it.
The last thing is to validate it’s actually charging and if not turn off(maybe in a 3rd automation).

Haven’t jumped to node red yet, still verry new to the whole thing of home assistant but i think it’ll be easier there where you can do it in one script.(maybe?)

So, since the blueprint says:

above: !input 'charged_battery'

I’m guessing that if you set the “Charged Battery” to “100” , the charger will never turn off, since the value of the battery will never be above 100 % ?

I wonder if this is a bug? :thinking:

The intent is to not set to 100 as not is not generally good for phone batteries and there can also be delays in your phone updating HA with the current battery level. I personally set to 90%

Thanks for creating this (Im a new HA user) - Im using it to build an automation to charge my boat battery.

Question - do you have to run this automation or time trigger or something else ?

This blueprint was setup to be triggered on and off by a battery level within HA (typically a phone’s battery level). If your boat’s battery is registered in HA then you can use this blueprint, but if it isn’t you might want to look at other options.

1 Like

thanks for the reply - Ive learned more about how automations trigger and work and the one Ive made based on yours is working great (it monitors the SOC on my house bank and enables a Shelly plug to turn on my charger until it reaches target SOC then shuts off (and sends notifications for Charging started and Done)

Works well with my “Honor Magic5 Pro”, many thanks for this automation!
I have set 60% as the lower value and 80% as the upper value. In the history (of the automation), the switching of the plug to “On” appears (below 60% was reached), but not the nightly switching of the plug to “Off” (over 80% was reached). Also does not appear in the automation log, strange.
However, the automation had worked. I can see this in the charge status of the mobile phone in the morning and also in the progress of the plug status.
Any idea what the problem could be? It’s not really important, the automation is working. But I’ve only had it running since yesterday…maybe I should wait a few days first.

The automation is dependent on HA getting your battery status from your phone. When not in use, to save battery, phones go into a sort of sleep state and won’t let background updates, like sending HA the current battery level, as often.

HA receives the required information from the phone, even in the middle of the night. Otherwise the automation would not work and the charging plug would not be switched off when 80% is reached.
Last night, the behaviour was exactly the same as before. Only the last time the plug was switched on is documented in the history, at below 60%. See the following screenshot:

But: If you now click on this message on the screenshot, you will see a different display where the nightly switch-off is shown, see the following screenshot:

So it just seems to be a “problem” with the display in HA as shown in the first screenshot. I would have expected the night-time running of the automation to be displayed there as well. So you just have to search properly to find a place in HA where it is displayed.
So everything is OK, the automation works fine and I have also found a place in HA where this is documented. Thanks again!