Script to track all devices with a battery_level

A minor fix for your code after browsing it.
The icon for battery 50% is missing the number for the icon.
There maybe a more elegant way to write that section using rounding and concatenation.
But what you have is functional so I wouldn’t worry unless you are asked to change it when merging with HA.

It can definitely be written more elegant, but as you say it works.
I don’t think this is something to be merged with HA. It’s just a helper not a new component all this can be done using regular sensor.template, this is template sensor for lazy people and for those who like the code clean.

I have corrected missing 50%. If you feel this is usable for more people than here in this thread I can create a special repo and you can contribute.

Nice work, @pilotak! I’ll try it out the next time I update my HA install. I haven’t actually had as much of a need for my original battery script due to the zwave component creating battery_level sensors on its own now, but this is definitely still helpful for non-zwave sensors.

I agree with @tinglis1 that there may be a cleaner method to handle the icon template, but I also wouldn’t worry about it if it’s working for now.

Your example works but gives me multiple sensors with the friendly name battery and no way to know which battery relates to which entities. Anyone good at templating and manage to show the friendly name & battery for each of the specified entities so rather than “battery” it would show “myslipo_1_0 battery” or whatever the friendly name is set to.

sensor:
  - platform: attributes
    friendly_name: "Last changed"
    attribute: last_triggered
    icon: 'mdi:clock'
    time_format: '%d, %m - %H:%M:%S'
    entities:
      - automation.temp_changed

  - platform: attributes
    friendly_name: "Batteries"
    attribute: battery_level
    unit_of_measurement: "%"
    entities:
      - sensor.myslipo_1_0
      - sensor.myslipo_2_0
      - sensor.myslipo_3_0
      - sensor.myslipo_4_0

I’ve tried templates it but just end up breaking it.

@stevenhorner I though about doing name tepmlate, but it came up pretty complicated, so i started with this easier variant. The solution is quite simple, you have two options:

  • have multiple entities for attributes sensor and change friendly name for each one with customize
homeassistant:
  customize:
    sensor.myslipo_1_0_battery_level:
      friendly_name: 'Living room batteries'

    sensor.myslipo_2_0_battery_level:
      friendly_name: 'Bedroom batteries'

sensor:
  - platform: attributes
    friendly_name: "Batteries"
    attribute: battery_level
    unit_of_measurement: "%"
    entities:
      - sensor.myslipo_1_0
      - sensor.myslipo_2_0
  • or you create an attribute sensor for each temp sensor:
sensor:
  - platform: attributes
    friendly_name: "Living room batteries"
    attribute: battery_level
    unit_of_measurement: "%"
    entities:
      - sensor.myslipo_1_0

  - platform: attributes
    friendly_name: "Bedroom batteries"
    attribute: battery_level
    unit_of_measurement: "%"
    entities:
      - sensor.myslipo_2_0

I tried both and they do not break anything.

It’s up to you to decide which variant suits you.

1 Like

This sounds interesting.

Do you by chance know where is the documentation for this attributes’ platform?

I cannot seem to find it and I was interested in knowing if I would be able to specify icon_template for it, to fully replace my current setup (using template sensors).

Thanks in advance.

You could not find documentation because it did’t exist. :grin:

I have set up a new repo for my custom components with appropriate documentation in case somebody wants to contribute and maybe later can be merged into HA.

1 Like

@pilotak this looks useful. Could you make a PR for the main repo?

1 Like

@andrey So I pushed it into the main repo, but it was refused. We should use sensor.template was answered.
Never mind I still think it is useful and will continue using it as it’s easier and looks cleaner in config.

1 Like

I made a PR to @pilotak repo recently which gets the friendly name of the original entity, if you don’t specify any friendly_name in the attributes config.

merged yesterday…

Creating a template for each battery is a ballache.
Creating a value template for icon is even more… especially with sensors with batteries. I have 20-30 batteries to track.
Thanks for this I will give it a try…

1 Like

@pilotak maybe make a small note that it has to go into custom_components/sensor/ i bumped my head into this thing, since it was my first component and the first topic it was also in custom_components/ directly. Could not find install instruction of the .py itself in your repo so not sure if you wanted it there.

@riemers i have updated repo readme so it’s clear to everybody

Is this script suppose to get battery from device_trackers to?

I have this:

- platform: attributes
  friendly_name: "Batteries"
  attribute: battery
  unit_of_measurement: "%"
  entities:
    - device_tracker.matte_api
    - device_tracker.asa_api
    - device_tracker.cod_api

The device_tracker have this attributes:

{
  "friendly_name": "Asa_Api",
  "latitude": xx.xxxxxx,
  "battery": "41",
  "source_type": "gps",
  "longitude": xx.xxxxxx,
  "gps_accuracy": 0.2,
  "entity_picture": "/local/tasker.png"
}

But my sensor.asa_api_battery gets this:

State: unknown
Attribute:

{
  "unit_of_measurement": "%",
  "friendly_name": "Batteries"
}

@pilotak: A nice adition to this is Howto create battery alert without creating a template for every device although the nice side of that template is that it automaticly picks up all the battery powered devices. I don’t know if that is possible in your script too? That would save creating a list of devices for the attribute. Just a thought, might help others too, alerting if low is nice to have.

P.s. using templates to create what you made imho i don’t see how that would be done anyways.

I had a quick look at the automations, what a clever way to do! anyway i’m working on different custom component at the moment so i don’t have much time to alter attribute sensor. But it is set in Github so anyone willing to contribute is welcome.

How do i exclude an entity?

Hi Awesome component been using for a while, I have added about 40 battery powered sensors since so was glad to see you have modified the original code to add a whitelist.

I updated the code and added to my config.yaml but I get this error…

Invalid config for [battery_state]: required key not provided @ data['platform']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/battery_state/

snippet of config

battery_state:
  include:
    - sensor.door_window_sensor_158d0001a6469f_battery
    - sensor.door_window_sensor_158d0001b704f9_battery
    - sensor.door_window_sensor_158d0001b70953_battery
    - sensor.door_window_sensor_158d0001b7acc5_battery
    - sensor.door_window_sensor_158d0002019ae3_battery
    - sensor.door_window_sensor_158d0002019b2a_battery

Hi guys… This is my code, acording to this post - I am using it to show Ikea Tradfri dimmer battery… I have the battery state but in badge, and I would like to have it in a list and remove the badge…

But I am getting the error below… Any suggestion?

Unable to find component sensor.attributes

sensor:
- platform: yr
- platform: attributes
friendly_name: “Batteries”
attribute: battery_level
unit_of_measurement: “%”
entities:
- sensor.boravak
- sensor.blagovaona
- sensor.kuhinja
- sensor.hodnik_ulaz
- sensor.kupaona