Battery_level attribute from ESP

Tags: #<Tag:0x00007f780e490808> #<Tag:0x00007f780e490538>

Hello everyone!
I’m learning my way into ESPhome and HA and I just found something I’d like to do.
Is there a way to make the ESP report it’s battery level so it shows up on the battery section of the config --> devices --> dashboard the same way a cellphone battery does?

I have tried the following yaml, but it didn’t seem to make any effect on the dashboard.

  - platform: adc
    pin: VCC
    id: "battery_level"
    name: "esp.2.vcc"

Any idea?

I don’t think you can do it in ESPHome. It only supports binary sensor device classes.

You can customise the sensor in home assistant though.

device_class: battery

I think it has to be something related with some tricky naming convention in HA.

ESPhome has many more than just binary sensors. That yaml itself is an example of an analog sensor sending a float.

I suspect there’s gotta be something I can add to make HA render the value just like it does with my cellphone. :smirk: :smirk:

Yes but only binary sensors support device classes in ESPHome. Sensors in ESPHome do not. Sensors in Home assistant do.

So format your battery sensor on the ESPHome device to output a % instead of a voltage. Then in home assistant customise that sensor by applying the battery device class. It has icons relating to battery state.

1 Like

There is a provision in the ESPHome documentation, that talk to the possibly of measuring VCC. Perhaps you can try the abbreviated Yaml provide in their example. Pin used needs to be A0 only.

ESP8266 Measuring VCC

On the ESP8266 you can even measure the voltage the chip is getting. This can be useful in situations where you want to shut down the chip if the voltage is low when using a battery.

To measure the VCC voltage, set pin: to VCC and make sure nothing is connected to the A0 pin.

sensor: - platform: adc pin: VCC name: “VCC Voltage”

While that’s great for getting the battery voltage it does not answer the question:

Only adding the device class will do that.

Ok understood. It’s just strange that ESPHome can create ‘Wifi signal strength’ in HA using - platform: wifi_signal and does not for the - platform: adc Vcc value as both are usually considered as attributes. But Ok point taken.

That’s exactly right!!! Thank you @tom_l!!!
I did it on my esp_2 and it works!

Here is the final portion of that yaml:

  - platform: adc
    pin: VCC
    id: "VCC"
    internal: true
  - platform: template
    name: "esp.2.battery_level"
    unit_of_measurement: '%'
    update_interval: 5s
    lambda: |-
      return ((id(VCC).state /3.30) * 100.00);

Thank you!!!


Yeah, that was my thought as well, so I thought HA would just take it based on the ID or name, but nope… @tom_l was right, you have to override the class manually and make sure the value is in % and not V as I had it.

I hope this is a useful sample code for other people :slight_smile:

1 Like

I’m getting the warning message when compiling it: warning: multi-line comment [-Wcomment]. Is a way arround the error message not to show up?


It’s just a warning, forget it. I think that this small issue will be solved with next release

ok, thanks!