Attempting to create an Automation Based on my iPhone's Battery State (charging/not charging)

As the title says! I am attempting (and failing) to successfully run a scene based on when I put my phone down to charge. I can see in the log that HA is registering my phone changing state, but no matter what I try to put as a trigger, I can’t seem to figure it out. My goal is to have an automation that triggers based on my phone changing from not charging to charging, at a specific time of day, and then run a scene when the time condition is passed.

I feel like this is a simple answer, but for the life of me, I could not figure it out nor could find any information on it online, as it all seemed to be concerning battery percentage in my searches.

Please post the yaml of the current automation you have.

1 Like
description: ""
mode: single
  - platform: state
      - sensor.shauns_iphone_battery_level
    from: not charging
    to: charging
  - condition: time
    after: "22:30:00"
  - service: scene.turn_on
      entity_id: scene.good_night
    metadata: {}

So if the phone is in a “not charging” state, and switches to charging, after 22:30, turn on the scene “good_night”.

Suggest remove the “from:”. I noticed that on some phones at least on the Android side, the state is not necessarily “not charging”, but might be “discharging” or some other thing like “on”. Also check that the capitalization matches. Again, might be nothing but different devices reported “Charging” so “charging” (lower case C) would never be true.

Obviously this automation will only work between 23:30:00 to 23:59:59, is that what you are looking for?

As a personal paranoia, I also test in the condition that the action is not already accomplished. In your case, what happens if you plug your phone in, then unplug, the replug it? Sometimes that is not an issue, but sometimes you I flood the basement :D.

You can also add a trigger of 23:30, to check if the phone is already plugged in prior to 23:30…

(Also I am far from an expert. so caveat emptor)

1 Like

That’s exactly right on what I want to do. Originally I had just the “to: charging”, and had no condition in there, but wanted to make it clear what my end goal was. During the testing prior to my post, I had it with just the trigger and scene, but I appreciate the advice!

As for the capitalization, I will check that now! I don’t think I tried that earlier. And the time frame was not necessarily meant to be solely between 22:30 and midnight, I was unaware of that interaction. I’ll set that up properly to check later times as well.

I’ll report back if the capitalization has any good effects. Thanks!

If you want to run a scene after sun set, try the “Sun” integration. There you can just select “after sunset”, and such fun stuff.

1 Like

The dang capitalization worked. Thanks a bunch for your help!

Be aware that your automation probably will fail often.
This is because the modern phones are using smart charging and the newer ones even AI.

They will sometimes not charge immediately because they have mapped your routines and found out that your phone is not used at night, so they will wait and plan a charging so your phone reach 100% just before you normally grabs it in the morning.
This is done to avoid a full charging when you connect it in the evening and then one or more top ups during the night.
The top ups will wear out the last few cells on your battery and it will therefore lose faster capacity over time.

This means you might not have a state of charging before late at night/early morning.
Sometimes you might have other states instead like connected.
Sometimes there are extra sensors that can help to determine charger connection, but it vary from phone to phone.

Hi - I want to add iphone charging as a condition to an automation, but while I find the device, I cannot choose the entity (sensor.pez_s_iphone_13_pro_battery_state) in the dropdown menu. Can someone help me how to add it? Thanks