# Battery state calculation, how?

Homies,

I want to make a sensor that displays the battery charge level icon based on numerical values ranging from 0 to 105. The chip’s supply voltage is measured in an 8bit value, one LSB corresponds to 1/32 volt.
So for example, if I want a battery empty icon when VCC is below 1.5V, then the state value for that should be 48 (1.5/0.03125). Lets make that 50. A full battery equals 105.

Looks easy, every 10% the battery-charge icon would change, but since I totally suck at math I have no idea how to do this and translate that in to a HA template.
Anyone willing to help?

Just use the compensation integration using those values and it’ll do the math for you.

Sounds promising, going to look in to it. Thanks!

I’ve tried the compensation, but no idea how to make this work. I understand what it does, but where do I place the code? I’ve tried all sorts of combinations and indentations, but all results in errors. Searching the forum, youtube or google doesnt get me any futher either, there isnt much info on how to use it.
This gives me ‘Property compensation is not allowed’

``````- platform: template
sensors:
ble_battery_status:
friendly_name: Battery status
value_template: >-
{{ ((states ('sensor.bthome_sensor_0001_count_1') | int / 0.03125) / 1000) | round (2) }}
{% set ble_battery = states ('sensor.bthome_sensor_0001_count_1') | int | round(0) %}
icon_template: >-
mdi:battery-{{ble_battery}}
unit_of_measurement: "v"
compensation:
ble_battery_charge:
source: ble_battery
data_points:
-[50, 0]
-[105, 100]
``````

Is there example code or more documentation on how this works?
As you might have guessed already, I’m new to HA

The documentation covers this:

Why are you putting it in the template / sensor section?

Just keep in mind that you’ll want to add a unit_of_measurement.

Why? Because I thought that is the correct way. Obviously its not
I’m new to HA and am trying by means of parts of code from this forum, to figure out how it works

It’s important to look at the documentation and it’s examples. It will tell you where each yaml piece goes. Guessing will only cause you headaches.

Ok, so I’ve put it in configuration.yaml and it is being accepted.

``````compensation:
ble_battery_charge:
source: sensor.bthome_sensor_0001_count_1
unit_of_measurement: "%"
data_points:
- [50, 0.0]
- [105, 100]
``````

Did I understand it correctly that the compensated value of `sensor.bthome_sensor_0001_count_1` would be made available by means of `ble_battery_charge`?
If try to read `{{states ('ble_battery_charge')}}` it either is empty or unknown.
`sensor.bthome_sensor_0001_count_1` currently has a value of 105 and should be changed to 100
What am I missing?

Whenever the source sensor updates the resulting sensor will update. So that 105 will show up as 100 on the compensation sensor.

Strangely enough that never happens. I’ve forced a state change in Developer tools, but the result is always `unknown`.
`{{ble_battery_charge}}` gives `UndefinedError: 'ble_battery_charge' is undefined`
This could be correct as `'ble_battery_charge'` is not a variable but a sensor.

Have you restarted HA after adding the sensor? Try:

``````{{ states('sensor.ble_battery_charge') }}
``````

You need to read more documentation.

You are absolutely right on the reading part.

`{{ states('sensor.ble_battery_charge') }}` is what gives me unknown.

I always restart after a config change.

Anything relevant in the logs? What does this show:

``````{{ states['sensor.bthome_sensor_0001_count_1'] }}
``````

(yes, deliberate square brackets)

`{{ states['sensor.bthome_sensor_0001_count_1'] }}` values 105. This is the physical BLE sensor.

Nothing in the logs

that’s impossible for that template to “value 105”. It will return a state object, which has a very specific format when output from that template. Please paste the results of the template that troon provided.

e.g.

``````<template TemplateState(<state sensor.foyer_switch_illuminance=unavailable; restored=True, state_class=measurement, device_class=illuminance, friendly_name=Illuminance, supported_features=0, unit_of_measurement=lx @ 2023-06-12T06:28:43.182109-04:00>)>
``````

Not exactly sure what you’re asking.
This is what I have:

106 is the value from sensor `sensor.bthome_sensor_0001_count_1`

Troon asked you to put this template in the template editor.

And thats exactly what I did.
Returns a value of 106 or 105

Dude, common, that’s not the same template.

He even specifically said:

never mind, I’ll figure it out myself some day