Run Automation When Phone Charging

I want to create an automation that, when I place my phone on charge it triggers some entities on and some others to off.

However, I’m not sure how to get the battery state as, I’m led to believe, that the HA companion app needs to be open in the foreground and the device unlocked for the battery_state to update.

Is there a way to get the state of the battery without having to do this?

If states such as occupancy can be detected via the device without having to open the companion app, can the same be done with battery_state too?

1 Like

You could have mentioned if you are using Android or Apple Companion app or even tagged your post with “Companion App for Android” or “Companion App for Apple”.


Did you look? The barrier to verify this for yourself is really really low.


On Android - my phone screen has been off for hours now with the app definitely not in the foreground:
image

Plugged it in, and before I could see the change it read like this:
image

1 Like

I’m guessing people skills aren’t your forte or you’ve had a crap day, judging by your condescending response. Or maybe you’ve used a translation app?

I’m using the iOS companion app and I have tested it with screen off and the status doesn’t change, even when leaving it for 10 minutes or so. Definitely within my skill set!!

The only time the status changes is if the screen is unlocked and the app is running in the foreground, as I mentioned in my OP. This could be due to Apple limitations so I was wondering if there’s a way of referencing the state in the background.

iOS 14 has the ability to trigger an automation any time your phone starts charging.

Check the IOS documentation and let us know if you need help setting it up.

This is preferable (IMO) as the HA app doesn’t update reliably while this route has an instant response and can interact with HA.

1 Like

Should have offered that information during the initial post where you’re asking for assistance!
Haven’t come across mind readers on this forum (yet).

I’ll leave the ad hominem attack down to your frustrations and inability to solve the issue :slight_smile:

2 Likes

Thanks silvrr. I’ve been reading through the iOS documentation as you suggested and the below is stated regarding sensors:

It is important to know that these sensors are only updated when a location is pushed to Home Assistant or the web view is refreshed .

How do I “push” a location to HA to obtain the battery state?

iOS Sensors

Keep in mind that if you want quality help then supply quality information.

I haven’t bothered installing the app on my apple work phone but most of the time it works instantaneous on android.

Perhaps there is a battery option that can be changed so that you allow the app to run when in sleep?

2 Likes

Thanks but I’ve checked, and all background update settings are turned on.

This worked, though not contained wholly in HA. I’m not sure if you meant a HA iOS automation or the iOS automation as I’ve done but thank you for the heads up, silvrr.

In the iOS shortcuts app I created an automation to send the phone location to HAvevery time it’s placed on charge or removed and that solves the issue. The battery state is automatically updated in HA, almost instantaneously.

For anyone else who may be in need of this:

iOS Shortcuts app > Automation then select each of the below

  “+”

  “Create Personal Automation”

  “Charger”

  Enter ticks in “Is Connected”, “Is Disconnected” or both

  “Next”

  “Add Action”

  Search for “Home Assistant”

  “Send Location”

  “Location”

  Choose home location

That’s it.

6 Likes

This is great but fails when charging in the car as it sends Home and triggers all sorta bad things to happen if you’re working a lot with presence detection.