How to get the battery level?

I’m having similar issues again, where my location is not being updated and my logs are now showing a “received invalid webhook payload” error in my home assistant logs. The mobile app appears to be trying to report a negative value for my altitude. I wonder if that has something to do with it

It is possible to get the sensor out of this entity, I mean, I would like to have a separate sensor to have kind of alarm when the battery is low?

Yes, you create a template sensor and point it to the attribute battery_level.

2 Likes

Create a Template Sensor (https://www.home-assistant.io/integrations/template/).

sensor:
  - platform: template
    sensors:
      phone_battery:
        friendly_name: "Phone Battery"
        unit_of_measurement: '%'
        value_template: "{{ state_attr('device_tracker.sm_g975u_2', 'battery_level') }}"
2 Likes

thanks flamingm0e
I’m trying to do that, but i’m quite new and its the first sensor i’ll create, I’m following this:

but I don’t know how to include that on the configuration

at the moment i have this but is not working

sensor:
  - platform: template
    sensors:
      phonebatt:
        friendly_name: "bateria telefono"
        unit_of_measurement: '%'
        value_template: "{{ states_sensor.battery_level.attributes['Battery Level']}}"

what i’m doing wrong???
thanks

You can also get it using:

- platform: template
  sensors:
    phone_battery:
      friendly_name: "Phone Battery"
      unit_of_measurement: "%"
      value_template: "{{ states.device_tracker.oneplus_a6003.attributes.battery_level | round(0) }}"
      device_class: battery
3 Likes

You have to tell it what sensor to read the attributes of. You can’t be so generic and expect it to know what sensor you want the battery level from.

1 Like

yeah, that is was I suspect, but I don’t know where to include that info
my device is call sm-g930F, where I can include this on my actual config??

There are 2 examples, literally right there…

You should follow the user documentation, and not the developer documentation.

ok, a lot to learn here yet,
after reading this


and this

I think my problem is on the line
value_template: "{{ states_sensor.battery_level.attributes['device_tracker.sm_g930f', Battery Level']}}"
I’m having a syntax error, but my knowledge on this is not enough yet

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘Level’) for dictionary value @ data[‘sensors’][‘phone_battery’][‘value_template’]. Got “{{ states_sensor.battery_level.attributes[‘device_tracker.sm_g930f’, Battery Level’]}}”. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/template/

Are you really not reading the comments here?

Seriously. 2 of us gave you perfectly working examples, but it’s YOUR responsibility to change the device tracker to be named correctly.

thanks flamingm0e
just trying to learn how its works, of course if I just copied that it will work, but thats not my point here.
I was trying to follow home assistant companion documentation to make it works. Probably its my lack of knowledge in any type of programing.
Anyway, its working now. I’m using
value_template: "{{state_attr('device_tracker.sm_g930f', 'battery_level')}}"

`

I don’t see how the companion docs are accurate for any of this though. It appears to be geared 99.99% toward the iOS implementation.

I thought that in a recent version of HA that the system was supposed to automatically create sensors for battery powered devices as long as the entity had either “battery” or “battery_level” as an attribute.

I’m not showing a battery sensor for my android phone from the mobile app either but all of my other battery powered devices have one.

Any idea this wouldn’t work in this case?

I’m not seeing any battery sensors for ANY devices at all, except for the template sensors I created.

Maybe it’s only for zwave & zha devices since those are the only battery powered devices I have and they all were created by the system.

That would explain why the mobile battery sensor isn’t being created.

I’ll have to see if I can find that announcement again.

Maybe. I don’t have any battery powered zwave devices, but I have lots of device trackers with battery_level attributes.

I just noticed another issue…

my battery level from the app only reports when the app is open/active.

I checked the app settings and the only “background” setting is for location tracking.

Is there something I’m missing or is that the way it’s supposed to work?

If so, does anyone know where to report bugs/feature requests?

I just noticed the battery level only updates when the GPS is enabled (I leave mine off most of the time). That restriction doesn’t seem necessary does it?