šŸ”‹ Turn off phone charging after the phone is charged

This automation blueprint will switch off your smart plug/relay after your phone is charged to a given percent.

Requirements

  • A switch that can be turned off with switch.turn_off
  • Battery level sensor

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: Turn off charging after device is charged
  description: Turn off the smart plug/relay after the device is charged to a given percent.
  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: Charge percent
      description: Charge device to this percent
      selector:
        number:
          min: 0
          max: 100
          unit_of_measurement: "%"
          mode: "box"
      default: 100

variables:
  phone_battery: !input phone_battery
  charged_percent: !input charged_percent

trigger:
  - platform: state
    entity_id: !input phone_battery

condition:
  condition: template
  value_template: "{{ (states(phone_battery)|int) >= (charged_percent|int) }}"

action:
  - service: switch.turn_off
    entity_id: !input switch

Importing Blueprint:
https://community.home-assistant.io/t/about-blueprints

24 Likes

This is good, thank you.
I got few sonoff basic in somewhere, this can be a good for this.

1 Like

If you can add the function to start charging under a configureable battery percentage i will actually use this.

7 Likes

+1, I will very appreciate start of charging when phone battery is 10% below configured treshold.

3 Likes

can work for laptop battery ?

Hello, @vorion

In order not to create a copy of your blueprint, maybe you can change the current one and add the requested ā€œturn onā€ feature.
You have just to:

  1. define another input named discharged_percent similar with charged_percent and an associated variable
  2. remove the condition block

condition:
condition: template
value_template: ā€˜{{ (states(phone_battery)|int) >= (charged_percent|int) }}ā€™

  1. replace the action block as following:
action:
- choose:
  - conditions:
    - condition: template
      value_template: '{{ (states(phone_battery)|int) >= (charged_percent|int) }}'
    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: []
5 Likes

Haha, nice.
I was thinking of this too, but then with a slightly different approach.
My idea was/is to store the charging time to full charge in a helper and set a ā€œwake-up timeā€, so it would start charging ā€œjust in timeā€ to be fully charged at the moment I get out of bed. Off course it should always start charging when my phone hits 1%.

Nice work though!

2 Likes

Is it possible to fork/change this blueprint to individual needs?

IĀ“m thinking of just notifying when threshold is reached instead of switching off the power plug - because on that power plug more than one device is charged at the same time.

Great work! I added an additional feature though. If Iā€™m at work charging my phone, I donā€™t want my plug turning off since something else might be plugged in at that time. So to solve this, I added the condition that I should be home for the plug to turn off.
The code:

blueprint:
  name: Turn off charging after device is charged
  description: Turn off the smart plug/relay after the device is charged to a given
    percent.
  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: Charge percent
      description: Charge device to this percent
      selector:
        number:
          min: 0.0
          max: 100.0
          unit_of_measurement: '%'
          mode: box
          step: 1.0
      default: 100
    person_home:
      name: Person
      description: Person that should be home for the plug to turn off
      selector:
        entity:
          domain: person
  source_url: https://community.home-assistant.io/t/turn-off-phone-charging-after-the-phone-is-charged/253939
variables:
  phone_battery: !input 'phone_battery'
  charged_percent: !input 'charged_percent'
  person_home: !input 'person_home'
trigger:
- platform: state
  entity_id: !input 'phone_battery'
condition:
  condition: and
  conditions:
    - condition: template
      value_template: '{{ (states(phone_battery)|int) >= (charged_percent|int) }}'
    - condition: state
      entity_id: !input 'person_home'
      state: home
action:
- service: switch.turn_off
  entity_id: !input 'switch'

3 Likes

would love to see this added, currently I have to turn the switch back on to charge my phone which is not very intuitive.

1 Like

I think I have this working pretty reliably in a blueprint/automation. However, it seems my phone, a OnePlus 6t, is going several % over and under before HA knows to turn off/on the switch/plug. Iā€™ve read the polling for updating entities is about 30 seconds. However, this is several minutes, sometimes 10 minutes before it updates and either starts charging or stops. Any thoughts on a more frequent update of a battery level entity?

Same issue especially with my iDevices: they fall in deep sleep on WiFi and HA sensors are not updated at all or after hours. Canā€˜t really use that automationā€¦ -.-

1 Like

Hi, Iā€™m new here and just learning.
I have tried to do as you have said, but the extra option for turning on is not showing up in HA.
Can you share Full Blueprint please, just getting my head around this coding, then i can compare the 2 and see where i have gone wrong

Hi there and happy 2021!

See the improved blueprint here: Stop/start charging based on battery level when home .

3 Likes

Update: meanwhile because of this for all my charging stations I use smart plugs with power measurement and another blueprint. That way e. g. my iPhone can sleep with flight mode active during the night and the smart plug stops charging automatically. Works great for months now.

1 Like

Thanks! This works for me (unfortunately only with the Shortcuts App in IOS (If I close the HA App the sensors are not updating)

But I have a question. If the iPhone is charged at 85% the power goes off, I unplugged the iPhone and then the power plug should be active again or not?!

I canĀ“t import i become a Timeout, what can i do?


Logger: homeassistant.components.websocket_api.http.connection
Source: components/websocket_api/connection.py:140
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 17. September 2022 um 20:44:34 (6 occurrences)
Last logged: 08:51:23

[546902370448] Error handling message: Timeout (timeout)
[546871966064] Error handling message: Timeout (timeout)
[546872399360] Error handling message: Timeout (timeout)
[546978884016] Error handling message: Timeout (timeout)
[546992215408] Error handling message: Timeout (timeout)