Sure. Its basically an implementation of the following:That battery runtime calculator you have there on the right is quite interesting.
Care to share details?
YAML:
- platform: template
sensors:
soc_battery_time_left:
friendly_name: "Battery Depletion Seconds"
unit_of_measurement: Seconds
value_template: >
{% set state = states('sensor.battery_output_power') | int %}
{% if state == 0 -%}
{{ ((((states('sensor.battery_soc') | float - 20) /100) * 10640) / 60 * 60 ) | timestamp_custom('%s', 0) }}
{%- else -%}
{{ ((((states('sensor.battery_soc') | float - 20) /100) * 10640) / (states('sensor.battery_output_power') | float) * 60 * 60 ) | timestamp_custom('%s', 0) }}
{%- endif %}
soc_battery_time_left_friendly:
friendly_name: "Battery Depletion Time"
value_template: >
{% set state = states('sensor.battery_output_power') | int %}
{% if state > 0 -%}
{%- set time = states('sensor.soc_battery_time_left') | int %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set minutes = '{} minutes'.format(minutes) if minutes > 0 else '' %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set hours = '{} hours, '.format(hours) if hours > 0 else '' %}
{%- set days = (time // 86400) %}
{%- set days = '{} day, '.format(days) if days > 0 else '' %}
{{ 'Less than 1 minute' if time < 60 else days + hours + minutes }}
{%- else -%}
{{ 'Running on solar power' }}
{%- endif %}
The "20" is the battery shutdown or minimum SOC
10640 is the total battery energy. in my case 3 x 5.32 kWh batteries.
Both of these attributes can be defined in the card config.
The first condition checks the battery output power. If this is negative the battery is charging and the assumption is I am running off grid or have sufficient solar. .
If positive calculates available battery energy and divides this by the current battery power being used and then converts to seconds
The java script code looks like this:
JavaScript:
if (stateObj13.state > 0 && config.battery_energy !== "hidden") {
let totalSeconds = ((((parseInt(stateObj12.state) - config.battery_shutdown_soc) / 100) * (config.battery_energy || 15960) ) / (stateObj13.state || 1)) * 60 * 60;
const days = Math.floor(totalSeconds / (60 * 60 * 24));
const hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60));
const minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
if (days > 0) {
duration += `${days} days, `;
}
if (hours > 0 || days > 0) {
duration += `${hours} hrs, `;
}
duration += `${minutes} min`;
}
if (stateObj13.state <= 0) {
duration = "Charging";
}




