question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set charging state from a calculated sensor value

See original GitHub issue

I’m trying to show a charging state indicator using a calculated value. I have a mini lora weather station based on a Heltec cubecell dev board, which doesn’t provide charging information on it’s own. I could try to calculate the charging information on the board by averaging the last n battery voltage measurements, but then I have to get to it and reprogram it. So I tried to do the calculation in home assistant. For this purpose I’m using a derivative sensor. The sensor itself works. It shows values above zero when the battery is charging from the solar panel and zero or below when it’s is going down over night.

grafik Don’t know what caused the peak at 5am…

YAML configuration

entities:
  - entity: sensor.outdoor_01_battery_level
    name: Wetterstation
    charging_state:
      entity_id: sensor.outdoor_01_derivative
      value: 0
      operator: '>'
title: Batteriestatus
type: custom:battery-state-card

My expectation was that I can compare the sensor.outdoor_01_derivative to a value directly, but this doesn’t seem to work as expected. No matter if I set the operator to < or >, the charging indicator is always shown as in the screenshots below.

  • Is there an error in the configuration?
  • Is the delta (in the range of -0.01V…+0.01V per hour) too small for the comparison?
  • Should I make a binary sensor based on the derivative sensor and use this instead?

Screenshot grafik > comparison

grafik < comparison

Version V2.1.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maxwroccommented, Jan 28, 2022

Hey! There is no such a thing like operator for charging_state. Please look at documentation: https://github.com/maxwroc/battery-state-card#charging-state-object

To solve your problem (as you said) you need to create a separate template (binary) sensor, where you will have your condition, and use it in charging_state config.

0reactions
luebbecommented, Jan 28, 2022

OK, solved it with a combination of derivative/threshold sensor. Just for posterity if anyone comes across this problem too:

In sensors.yaml I defined a derivative sensor that monitors the incoming battery voltages over the past hour. Rounded to three digits in my case, because the changes are very small:

- platform: derivative
  name: "Outdoor 01 Derivative"
  source: sensor.outdoor_01_battery
  round: 3
  unit_time: h
  time_window: "01:00:00"

In binary_sensors.yaml I defined a threshold sensor that returns on when the derivative is above zero (which means the voltage is rising).

- platform: threshold
  name: "Outdoor 01 Charging"
  entity_id: sensor.outdoor_01_derivative
  upper: 0

The card configuration:

entities:
  - entity: sensor.outdoor_01_battery_level
    name: Wetterstation
    charging_state:
      entity_id: binary_sensor.outdoor_01_charging
      state: 'on'
title: Batteriestatus
type: custom:battery-state-card

returns the expected result: grafik

So really no need to add it to battery state card once you get your head around it.

Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

State of charge calculation for batteries - Configuration
I have solar at home and i have recently installed a battery pack. At the moment my battery pack can't send it's state...
Read more >
Monitor the Battery Level and Charging State
Determine the current charging state ... Start by determining the current charge status. The BatteryManager broadcasts all battery and charging ...
Read more >
Calculating the State of Charge of a Lithium Ion Battery ...
The State of Charge (SOC) is like the fuel gauge in your car, telling you how much energy is left in your battery...
Read more >
Battery State of Charge Calculation - Epic Power Converters
In this estimation method it is key to know the initial state of charge of the battery (this value is normally updated when...
Read more >
BU-903: How to Measure State-of-charge - Battery University
Measuring state-of-charge by voltage is simple, but it can be inaccurate because cell materials and temperature affect the voltage.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found