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.
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.
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.
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
---
platform: template
name: Level Battery ${devicename}
#device_class: battery
unit_of_measurement: '%'
accuracy_decimals: 0
id: ${devicename}_battery
update_interval: ${update_interval}
lambda: |-
return ((id(${devicename}_voltage).state /4.2) * 100.00);
// todo map only 3.0V to 4.2V as 0 to 100%, and up to 5V USB as 100%
can you help me to solve last line as lambda?
why is device_class not allowed?
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:
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