Stop/start charging based on battery level when home

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.

It is inspired by the blueprint of @vorion (šŸ”‹ Turn off phone charging after the phone is charged) and enriched with ā€œturn onā€ feature and location condition (thanks to @Vansify).

Requirements:

  • a switch or a plug that can be turned on and off
  • 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: []
14 Likes

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 after this everyone wouold be using it! :slight_smile:

1 Like

I would like it if someoneā€™s ā€œcoming homeā€ was optional. There is an error if no entry is made there.

The switch should also be switched off if the ā€œloaded valueā€ is equal or higher. Example: Turn off the switch at 80% or higher.

It does that already.

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.

You are right I didnā€™t think about that, I will just adjust the low battery threshold to something higher like 50%

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

1 Like

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.

Voor de iPhone kun je ook gewoon de automatisering in de woning app gebruiken werkt prima met pug-in stekkers.

Groetjes

Thanks @razvanmdobre for this blueprint! Have you considered adding a battery temperature criterion? High temperatures damage battery lifetimes, too.

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.

Can you share it? And any downsides (like reduced battery life)?

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.

I made two of this, on 80% and 100% to alway notify changes for ā€œlowerā€ or full battery level.

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.

1 Like

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 :slight_smile: