maxwroc / battery-state-card

Battery state card for Home Assistant
MIT License
849 stars 38 forks source link

How to set charging state from a calculated sensor value #391

Closed luebbe closed 2 years ago

luebbe commented 2 years ago

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.

Screenshot grafik > comparison

grafik < comparison

Version V2.1.1

maxwroc commented 2 years ago

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.

luebbe commented 2 years ago

Thanks, this answer was kind of what I expected.

The following parts from your examples:

  - entity: sensor.samsung
    charging_state: # uses is_charging attribute on sensor.samsung entity
      attribute:
        name: "is_charging"
        value: "yes"

and:

filter:
  include: # filters for auto-adding
    - name: entity_id # entities which id ends with "_battery_level"
      value: "*_battery_level"
    - name: attributes.device_class # and entities which device_class attribute equals "battery"
      value: battery
  exclude: # filters for removing
    - name: state # exclude entities above 99% of battery level
      value: 99
      operator: ">"

made me hope that I could also compare a sensor value this way.

luebbe commented 2 years ago

Would you consider this worth a feature request or do you think a separate binary sensor is good enough?

maxwroc commented 2 years ago

It depends how common are such cases and how many users would be interested in such functionality.

There is always a problem of balance between reach functionality and code complexity. If you add new functionality the card is more useful (or easier to setup) but in the same time due to more code there is a higher chance of a bug (any other additional change can introduce a bug too) and all users pay the penality in terms of the card code size which keeps increasing (although IMO it is not very important for HA).

luebbe commented 2 years ago

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!