Battery state calculation, how?

I know you’re getting frustrated, but holy moly, you’re frustrating to help. You literally need to copy/paste the template he asked you and you aren’t doing that.

You’re pasting

{{ states('sensor.bthome_sensor_0001_count_1') }}

He asked you to paste

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

Do you see the differences???

It’s taken 8 posts to get you to literally copy and paste the template he asked you to post. 8!

Yes, you’re right, sorry but it can get frustrating sometimes
Didnt see the brackets…here’s the output:

<template TemplateState(<state sensor.bthome_sensor_0001_count_1=106; state_class=measurement, friendly_name=Battery state @ 2023-06-16T13:07:49.708612+02:00>)>

Ok, can you also please paste this in the template editor?

{{ states.sensor.ble_battery_charge }}

returns null

Ok, the entity is not being created at all. Are you restarting after you add compensation? Secondly, there will be an error in your logs if it’s not set up properly.

Also, are you sure the file you’re adding the compensation: to is the correct one? We’ve had cases before where people accidentally edit a backup file or something.

Make sure you only have one compensation: heading in the file.

@petro I also came to that conclusion, ble_battery_charge isnt being created, even though its declared in compensation

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

My configuration.yaml is totally default and configuration isnt used in any other yaml file.
I’m using a freshly installed HA instance.

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

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

I always restart after a config change and find anything related to this in any log.
If you want I can send any log you want, just tell me which one.
Thanks for your help and patience so far.

Just post the startup section of the logs for home assistant core. I.e. homeassistant.log file

Logs are disappointingly empty.
This is from home-assistent.log after a restart, which is the same as the HA Core log:

2023-06-16 15:09:16.595 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-06-16 15:09:18.320 WARNING (MainThread) [homeassistant.components.ssdp] Could not set up UPnP/SSDP server, as a presentation URL could not be determined; Please configure your internal URL in the Home Assistant general configuration
2023-06-16 15:09:32.409 WARNING (MainThread) [homeassistant.config_entries] Config entry 'GW' for devolo_home_network integration not ready yet: Unable to connect to 192.168.10.183; Retrying in background
2023-06-16 15:10:54.086 WARNING (MainThread) [homeassistant.components.http.ban] Login attempt or request with invalid authentication from 192.168.10.195 (192.168.10.195). Requested URL: '/auth/login_flow/d021aeed6aaa369699f13e6b7c65e97b'. (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36)

Are you filtering your logs to only show warnings?

EDIT: Are you 100% sure there isn’t a compensation entity? Have you looked in your developer tools → states page? It does not build the compensation entity off the slug used in your configuration. I’m starting to recall that the main development team wouldn’t let me have the user set the name. Looking at the code, the name will be:

Compensation sensor.bthome_sensor_0001_count_1,

so the entity_id will probably be something stupid like sensor.compensation_sensor_bthome_sensor_0001_count_1

1 Like

Like I said, default settings :wink:
Got it on debug now, but that gives me a 1.7mb log. You want this whole log, or some filtering?

Please see my edit in my last response, you’re looking for the wrong entity_id. Look in Developer tools → states tab. Search for compensation.

1 Like

1 Like

that would be it.

If you add unique_id to your config you can edit the entity from the UI to change the entity_id.

{{ states('sensor.compensation_sensor_bthome_sensor_0001_count_1') }} returns 101.82, so I need to adjust the data points, correct?

Well 106 is above your last number. Unfortunately, it doesn’t truncate results, that’s a pending PR. You could increase the degree and add more data points to try to keep it between 0 and 100%.

compensation:
  ble_battery_charge:
    unique_id: ble_battery_charge
    source: sensor.bthome_sensor_0001_count_1
    unit_of_measurement: "%"
    degree: 11
    data_points:
      - [0, 0]
      - [10, 0]
      - [20, 0]
      - [30, 0]
      - [40, 0]
      - [50, 0]
      - [105, 100]
      - [115, 100]
      - [125, 100]
      - [135, 100]
      - [145, 100]
      - [155, 100]

FYI this will also allow you to change the entity_id from the frontend.

Great! sensor.compensation_sensor_bthome_sensor_0001_count_1 shows up with a value of 99.3,
ble_battery_charge unfortunatly, does not.
I’m experimenting with some code now to round it off in steps of 10 so I can make a battery icon depending the battery level.

Thanks again for your help and patience

I’m waiting for a PR to be added so I can put in another PR to allow device classes for these entities. It’s taking forever, maybe it’ll be done within the next couple months.

1 Like

For now, create a template sensor that’s a direct copy of the compensation sensor, and give that device_class: battery. That’ll do your icons for you.

@petro
Something strange happened. After a restart other compensation sensors appeared out of nowhere.
What makes it even more confusing is that after a restart one of the 3 has a value, the others are unavailable. Do you have any idea what causes this?
Is there also a way to get rid of the trailing .0 after 100? I added precision: 0 to the compensation, but no effect.