Battery_level attribute from ESP

Hey There,
thank you for this workaround. I hope ESPHome will have that sooner than later implemented itself.
I did what you wrote and created the costumization:

sensor.esp32_test1_battery_level:
  device_class: battery

It did show up as a battery under devices correctely for a short time (until some variable updates in HASS i guess) and then it turned back to the default “Eye” Icon.
Does someone know why that happens?
Best regards
Florian

1 Like

Did you ever get this figured out?

I am using a Wemos D1 Mini- do you simply connect A0 to 5V on the board?

Assuming your aim is to measure Vcc, then yes.

thanks for sharing your settings, was very useful.
May I ask you a question? why did you add the templeate voltage if you already had the adc sensor for it?

What I mean, was this not enough?

sensor:
  - platform: adc
    pin: A0
    name: "A0 Voltage x 4.2"
    update_interval: 15s
    accuracy_decimals: 3
    filters:
      - multiply: 4.2
    id: battery_$devicename

    entity_category: diagnostic
    unit_of_measurement: "V"  
    device_class: battery

  
  - platform: template
    name: "D1003_Battery_Percentage"
    unit_of_measurement: '%'
    update_interval: 15s
    accuracy_decimals: 0
    lambda: |-
      return ((id(battery_$devicename).state-3) /1.2 * 100.00);

    entity_category: diagnostic
    unit_of_measurement: "%"  
    device_class: battery

thank you

2 Likes

Well I probably added it while testing and never removed it… so yeah haha! There are always easier ways to do it, but I am just too lazy to make the code as efficient as possible :slight_smile:

Edit: I actually never figured it would also be in Home assistant… Appears like they do! Now I got 2 sensors with the same value in Home Assistant. Oops!

1 Like

Great, I tought there was a hidden reason I didn’t know, that’s why I asked. :slight_smile:

1 Like

HI @raidnet-ms, I have some question regarding your code:

  1. how you defined twice the id id: "LIION" and id: battery_$devicename, I’m not able to compile the code
  2. if I try to use id: battery_$devicename, I receive this error

regards

Never mind for point 2, I just learned how substitution work :smiley:

1 Like
  1. You are right, that was an error . I just edited. :slightly_smiling_face:

Super,

Can I ask for help with the formula? I’m using an ESP32 board with an 18650 lipo battery
The max voltage should be around 4.2, and the suggested min voltage should be 2.95
I adjusted the formula to this

return ((id(ESPBat).state-2.95) /1.25 * 100.00);

it returns values as 0.4xxx, it seems that I’m doing something wrong with the multiplier

  - platform: adc
    pin: A3
    id: "ESPBat"

    filters:
      - multiply: 4.2

regards

3v is the minimum voltage for the esp chip to work so you should consider that value as a 0%
However you can make a test to see at what voltage the chip will shutdown, but consider that is not reccomended to drain the battery voltage to its limit.

Keeping the voltage between 20%-80% will make the battery life last longer.

1 Like

Understood, but I changed the formula because this is the result

[12:13:55][D][sensor:113]: 'sim7600e Battery Percentage': Sending state -199.52148 % with 0 decimals of accuracy
[12:13:56][D][sensor:113]: 'sim7600e Battery Voltage': Sending state 0.44808 V with 2 decimals of accuracy

image

to have a sense it should be 4.4 V, not 0.44V

Check your spaces between the multiplier *

Do you guys see anything wrong with this configuration?
I am planning to use a 3.7V LiPo battery but in the meantime I am using a 3.3V power supply. So I am considering the battery range to be min voltage 3.0V and max 3.7V
With the following code I was expecting a 42% result for a 3.3V input, but I am getting 100%.

- platform: adc
    pin: A0
    name: "A0 Voltage of 3.7V"
    update_interval: 15s
    accuracy_decimals: 3
    filters:
      - multiply: 3.7
    id: server_battery_voltage
    entity_category: diagnostic
    unit_of_measurement: "V"  
    device_class: battery
  - platform: template
    name: "Server_Battery_Percentage"
    update_interval: 15s
    accuracy_decimals: 0
    lambda: |-
      return (((id(server_battery_voltage).state-3) /0.7) * 100.00);
    entity_category: diagnostic
    unit_of_measurement: "%"  
    device_class: battery

The strange part is that the measured voltage is 3.7V and I know the power supply I am using is 3.3V and used a voltmeter to check it.
image

if I set the -multiply to 3.5 then the output voltage measured is 3.5V. It doesn’t look like any ADC of the A0 is done.

Any idea what could be going on?

I figured out the problem. I had connected the A0 straight to the 3.3V instead of connecting it through a voltage divider such that 3.7V supply corresponds to 1V ADC.

1 Like

the 3,7v is 4,2v when completely charged. check with the multimeter at its maximum capacity. that’s why you should not modify the lambda.

Hey guys,

thanks for sharing of all these snippets but how did you wired your ESP board???

E.g. i’m using a battery shield. It gives me exactly 5V. Perfect to power my esp. I’m created my own circuit divider with 2 resistant and wired it directly to the battery and A0 of my ESP.

Did you do it differently??

Because, using “VCC” gives me always 100% and wiring A0 over the bat. shield the same! VCC is the internal power isn’t it? And wiring about the battery out pols to A0 is also always 100% because it ensure, that the battery always gives the right power supply.

Neither VCC nor A0 wired over bat. shield decrease over the time…different when I wired A0 over the circuit divider to the battery directly!

So, what does you circuit look like???

Thanks,
Isicom

I am really late to the forum, but I have a question.

I recently received a gift of dozens of components from a friend. Among the pile of stuff is like 30 adafruit LC709203F Battery Fuel Gauge. I have played with the dev board and it seems to be a little more precise than setting up a pin to the battery with voltage divider. My problem, is I have no idea how to use it with all my devices that are all ESP32 based.

Could someone who knows hook me up with a suggestion of what to do? I see that adafruit’s “whipper snapper” or AFIO has usage for this so it has to be possible…

Maybe a custom sensor…but again I don’t know how to do that, and I can’t find a how to that is specifically adding a i2c sensor that does not have a pre-built platform.

Any insights is appriated.

Hi… I also have bought some ESP32 devices (from EzSBC modell ESP32_Bat_Pro) with LC709203F battery sensor.

They have a Github repository with code sample for how to use the sensors.

I also could not find the sensor on the esphome documents.

Is somebody able to assist in adding the sensor to esphome?