ETA calculation of battery charging

hello,
is there a easy way to calculate time in minutes when battery will be charged?

i can imagine a algorithm, where I will create a sql sensor, which returns SOC value 15min ago, and calculate the time in template sensor.

any other ideas?

you can check this thread for ideas:

Im using this sql query to get the seconds for one percent charging

select
(select last_updated_ts from states where entity_id = 'sensor.e3dc_battery_percent' and state != 'unknown' order by last_updated_ts desc limit 1)
- (select last_updated_ts from states where entity_id = 'sensor.e3dc_battery_percent' and state != 'unknown' order by last_updated_ts desc limit 1,1)
as time_percent

and a template sensor that estimates the time when battery is charging, else ā€˜unknownā€™.

sensor:
  - platform: template
    sensors:
      e3dc_batt_loading_end:
        value_template: >
          {% set remain = 100 - (states('sensor.e3dc_battery_percent') | int(0)) %}
          {% set timepercent = states('sensor.e3dc_time_batt_percent') | int(0) %}
          {% if states('sensor.e3dc_battery_power') | int(0) > 0 %}
            {{ (as_timestamp(now()) + remain * timepercent) | timestamp_custom('%H:%M', true)}}
          {% else %}
            unknown
          {% endif %}

Simple and not accurate, but iā€™m happy with it. :slightly_smiling_face:

1 Like

here is finally my solution, iā€™m considering percentage increase in last 5min
sql sensors with ā€œlastā€ value and time

sql:
  - name: wattsonic_battery_soc_5min_state
    db_url: !secret URL_MariaDB
    query: >
      SELECT round(state, 1) as state, FROM_UNIXTIME(last_updated_ts) as updated from states
      where entity_id = 'sensor.wattsonic_battery_soc' and 
      state != 'unknown' and
      last_updated_ts <= UNIX_TIMESTAMP(NOW())-5*60 
      ORDER BY last_updated_ts DESC LIMIT 1
    column: "state"
  - name: wattsonic_battery_soc_5min_last_updated
    db_url: !secret URL_MariaDB
    query: >
      SELECT state, FROM_UNIXTIME(last_updated_ts) as updated from states
      where entity_id = 'sensor.wattsonic_battery_soc' and 
      state != 'unknown' and
      last_updated_ts <= UNIX_TIMESTAMP(NOW())-5*60 
      ORDER BY last_updated_ts DESC LIMIT 1
    column: "updated"

sensor

      wattsonic_battery_soc_eta:
         friendly_name: Wattsonic Batter SOC ETA
         value_template: >
            {% set battery_SOC = float(states("sensor.wattsonic_battery_soc"), default=100) %}
            {% set battery_SOC_last = float(states("sensor.wattsonic_battery_soc_5min_state"), default=0) %}
            {% set timediff = (states.sensor.wattsonic_battery_soc.last_updated.timestamp() - as_datetime(states("sensor.wattsonic_battery_soc_5min_last_updated")).timestamp())/60 %}
            {% set percdiff = battery_SOC - battery_SOC_last %}
            {% set percmissing = 100-battery_SOC %}
            {% if battery_SOC>99: %}
                full
            {% elif percdiff<=0: %}
                discharging
            {% else %}
                {% set timemissing = (percmissing / (percdiff/timediff))|round  %}
                {{ as_local(as_datetime(as_timestamp(utcnow()) + timemissing*60)).strftime("%Y-%M-%d %H:%M") }}
            {% endif %}

it looks working

iā€™m only wondering why this doesnā€™t work: state_attr(ā€˜sensor.wattsonic_battery_socā€™, ā€˜last_updatedā€™)
but this yes: states.sensor.wattsonic_battery_soc.last_updated

1 Like