Stop/start charging based on battery level when home

Tags: #<Tag:0x00007fc410a366b0> #<Tag:0x00007fc410a365c0> #<Tag:0x00007fc410a36110>

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: []
7 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:

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.