Simple automation for phone battery level

Hi all,
Beginner here.
I am experimenting with simple automation for phone battery level notification.

For the purpose of learning I want to create simple automation which should notified my Android phone device when it’s battery level reaches threshold value during phone charging.

I try to do that by creating automation with trigger like so:

platform: numeric_state
entity_id: sensor.redmi_note_8_pro_battery_level
above: '90'

Put a condition to trigger this only in case phone is charging:

condition: state
entity_id: sensor.redmi_note_8_pro_battery_state
state: charging

Than I simply define an action to call notify service on phone.

When I trigger automation manually I get notification on my phone and that is the only thing that works here.

For the purpose of troubleshooting I try to completely remove condition statement and have battery level above 90% just to trigger notification in any case when battery level is above 90%, but nothing happens. Notification is never triggered.
Could you please try to explain why is that so and this suppose to work?

Another question related with phone notification in HA phone app.
I noticed that it can pass a several minutes from time I triggered notification manually to receive it on phone (I am not setting any offset).
If I open the phone app while I am waiting notification to arrive, it arrives immediately. Which makes me think that phone OS(Android) prevents notification or sets some lower prio for it. I allow background access of the HA phone app, it didn’t help. I try to play with Android notifications for HA phone app but didn’t manage to get results either.
This is the case for few automations I create. They just have delay and I am guessing it depends if the HA phone app is running on phone or it’s sleeping but that’s just me guessing.

The automation only trigger when the value goes from below the threshold to above the threshold and then only trigger again when it goes below the threshold and above the threshold again, so it wiöl not trigger when battery changes from 91 to 92 or from 95 to 94.
Did you try to remove the quotes around 90? Can you please ahow the full automation code.

Thanks for answer.
Your advice helped.
It gets triggered only when threshold value is crossed.
So in this example:

  trigger:
  - platform: numeric_state
    entity_id: sensor.redmi_note_8_pro_battery_level
    above: '90'

Should be triggered only when state is changed from any value <= 90 to value > 90.
But it won’t be triggered if the value changes from 91 to 92 for example, like you said.

I tested this for discharging case

trigger:
  - platform: numeric_state
    entity_id: sensor.redmi_note_8_pro_battery_level
    below: '90'

didn’t try charging case, but should be the same.

Original code:

- id: '1607367241915'
  alias: Notification for phone charging level
  description: Notify when battery level reaches threshold during phone charging
  trigger:
  - platform: numeric_state
    entity_id: sensor.redmi_note_8_pro_battery_level
    above: '90'
  condition:
  - condition: state
    entity_id: sensor.redmi_note_8_pro_battery_state
    state: charging
  action:
  - service: notify.notify
    data:
      title: Battery level test
      message: 'Battery level: {{states("sensor.redmi_note_8_pro_battery_level")}}%'
  mode: single

But I noticed that phone won’t update sensors frequently and thus it happens that my notification arrives too late while the threshold is already crossed a while ago.
Is there a way to improve this?

There’s multiple thibga that can cause this behaviour. Try disabling battery optimization, allow the app to run in the background and try this:

- service: notify.notify
    data:
      title: Battery level test
      message: 'Battery level: {{states("sensor.redmi_note_8_pro_battery_level")}}%'
      data:
        ttl: 0
        priority: high

As stated here.

1 Like

I configured notifications according to your suggestion and completely removed battery saving on HA phone app. App was already configured to run in background so that part wasn’t an issue I would say.
I’ll test behavior and get back. I need to see real life scenario for full day or so.
Usually once phone is often used sensor updates are more frequent but once I leave it in peace for a while sometimes hours are needed for sensors update.

Regarding notification, I try to call service manually using Developer Tool with suggested settings:

title: test
message: "test123"
data:
  ttl: 0
  priority: high

and it really does trigger notification instantly.
Looks promising.

Thanks for help!

1 Like

Here is my feedback after I tested automation for a while.

Notifications works flawlessly.

Battery sensor updates not quite good as I would like, but for sure it is better that my first attempts and I can make some use of it.
I noticed that I have to start HA app and it should always remain in the background (should not be cleared from background). That mostly worked ok, battery was slowly discharging and HA was able to follow battery level with minor deviations.
However, even with app in background, it happens sometimes that sensor won’t be updated for a long time… by that I mean hours… I don’t know why that is, but it happens sometimes during morning . Than I would have to open HA app on the phone again and I would be ok with sensor updates for rest of the day.
I removed restrictions for HA app for saving battery.

I implemented a different solution to this problem. Might not be the best one ou there. But in my case i needed to know when the battery is low so that i can start the charging plug ( this is used with my wall tablet dashboard), otheriwse if the level update didnt come, my battery would die out.

I implement a 10 minute check, and nd based on that i turned the plug on.


alias: Turn On Sonoff plug when Tablet battery Empty
description: ''
trigger:
  - platform: time_pattern
    minutes: /10
condition:
  - condition: device
    type: is_off
    device_id: 46cebac79c3e8869a3cb97e4e874d063
    entity_id: switch.sonoff_plug_tablette
    domain: switch
  - condition: numeric_state
    entity_id: sensor.galaxy_tab_a_battery_level
    below: '20'
action:
  - type: turn_on
    device_id: 46cebac79c3e8869a3cb97e4e874d063
    entity_id: switch.sonoff_plug_tablette
    domain: switch
mode: single
1 Like