Solar battery run time till empty

@ziptbm Answers to your questions:

  1. Yes but I have taken out the backup% for lifes but display the total in my display. I can live with the variance - but that’s just me. Below is my templates.yaml file
#--------------------------------------------------------------------------------------------------
# Battery Life at Current Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life"
    unique_id: "solar_battery_life"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set a = states('sensor.powerwall_solar_now') | float(0.0001) %}
      {% set b = states('sensor.powerwall_site_now') | float(0.0001) %}
      {% set c = states('sensor.powerwall_battery_now') | float(0.0001) %}
      {% set current_power = a + b + c %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {{ decimal_hours | int(00) ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Average 1 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_1_hr"
    unique_id: "solar_battery_life_1_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Average 12 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_12_hr"
    unique_id: "solar_battery_life_12_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_12_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Average 24 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_24_hr"
    unique_id: "solar_battery_life_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_24_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Minimum 24 hr Power Draw - longest battery life
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_minimum_24_hr"
    unique_id: "solar_battery_life_minimum_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0.01) / 100.0 %}
      {% set number = states('input_number.number_of_batteries') | float(2) %}
      {% set energy = 13.5 | float(13.5) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.minimum_kw_used_last_24_hour') | float(0.01) %}
      {% if current_power <= 0.0001 %}
        {% set current_power = 0.5 %}
      {% endif %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Maximum 24 hr Power Draw - shortest battery life
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_maximum_24_hr"
    unique_id: "solar_battery_life_maximum_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0.01) / 100.0 %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %} 
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.maximum_kw_used_last_24_hour') | float(0.0001) %}
      {% if current_power <= 0.0001 %}
        {% set current_power = 0.5 %}
      {% endif %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Battery Power Draw - Life at Current Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_battery_power"
    unique_id: "solar_battery_life_battery_power"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set current_power =  states('sensor.powerwall_battery_now') | float(0.0001) | abs %}
      {% if current_power < 0.1 %}
        {% set current_power = 0.1 %}
      {% endif %} 
      {% if states('sensor.powerwall_battery_now') | float(0) < -0.001 %}
        {% set charge = ((1.0 - percent) * number * energy) | float(0) %}
      {% else %}
        {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% endif %}
      {% set decimal_hours = charge / (current_power + 0.0001) %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {{ decimal_hours ~ ': ' ~ minutes }}

#--------------------------------------------------------------------------------------------------
# Battery energy stored currently
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_energy_available"
    unique_id: "solar_battery_energy_available"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(0)) / 100.0 | float(0)%}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {{ charge }}

#--------------------------------------------------------------------------------------------------
# Battery Energy to Full
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_energy_to_full"
    unique_id: "solar_battery_energy_to_full"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(0)) / 100.0 | float(0) %}
      {% set charge = ((1.0 - percent) * number * energy) | float(0) %}
      {{ charge }}
      
#----------------------------------------------------------------------------------------------------------------------------------------------------
#
#  This section is for power and energy based on Tesla gateway.  I convert all negative to positive
#  I set to 0.0 when energy is not flowing in the correct direction for the sensor.
#  Sensors Available
#    sensor.powerwall_solar_now         - solar power being produced
#    sensor.powerwall_site_now          - grid power to or from
#    sensor.powerwall_battery_now       - battery power to or from
#    sensor.powerwall_load_now          - house power usage
#    sensor.grid_to_home_energy_price
#    sensor.solar_to_grid_energy_price
#
#  These sensors in turn drive the following integrations for energy
#  Sensors Available
#    sensor.solar_energy_produced
#    sensor.energy_consumed
#    sensor.energy_to_grid
#    sensor.energy_from_grid
#    sensor.energy_to_battery
#    sensor.energy_from_battery
#----------------------------------------------------------------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------------
# For Grid to Home/Battery pricing - Usage price I pay from the grid
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "grid_to_home_energy_price"
    unique_id: "Grid to Home Energy_Price"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) > 0.001 %}
        {{ state('input_number.kwh_cost_from_grid') }}
      {% else %}
        0.000 
      {% endif %}
    unit_of_measurement: "USD/kWh"

#--------------------------------------------------------------------------------------------------
# For Solar to Grid pricing - Generation price I sell to the grid
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_to_grid_energy_price"
    unique_id: "Solar to Grid Energy_Price"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) < -0.001 %}
        {{ states('input_number.kwh_revenue_to_grid') }}
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "USD/kWh"

#--------------------------------------------------------------------------------------------------
# Power to Grid - Make Positive
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power to Grid"
    unique_id: "Power to Grid"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) < -0.0001 %}
        {{ states('sensor.powerwall_site_now') | float(0) | abs}} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power from Grid - Make Positive 
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power from Grid"
    unique_id: "Power From Grid"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) > 0.001 %}
        {{ states('sensor.powerwall_site_now') }}
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power to Battery  - Make Positive  sensor.powerwall_battery_now
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power to Battery"
    unique_id: "Power to Battery"
    state: >-
      {% if states('sensor.powerwall_battery_now') | float(0) < -0.001 %}
        {{ states('sensor.powerwall_battery_now') | float(0) | abs}} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power from Battery  - Make Positive  sensor.powerwall_battery_now
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power from Battery"
    unique_id: "Power from Battery"
    state: >-
      {% if states('sensor.powerwall_battery_now') | float(0) > 0.001 %}
        {{ states('sensor.powerwall_battery_now') }} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"
    
#--------------------------------------------------------------------------------------------------
# AC Upstairs Energy - sensor.phase_1_a_c_upstairs_energy
# Power and Energy is only being measured on one phase - Need to Double for 230v
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Upstairs AC Energy"
    unique_id: "Upstairs AC Energy"
    state: "{{ states('sensor.phase_1_a_c_upstairs_energy') | float(0) * 2.000 }}"
    unit_of_measurement: "kWh"
#--------------------------------------------------------------------------------------------------
# AC Downstairs Energy - sensor.phase_1_a_c_downstairs_energy
# Power and Energy is only being measured on one phase - Need to Double for 230v
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Downstairs AC Energy"
    unique_id: "Downstairs AC Energy"
    state: "{{ states('sensor.phase_1_a_c_down_energy') | float(0) * 2.000 }}"
    unit_of_measurement: "kWh"

##### ------>  and here are my sensors:

# ---------------------------------------------------------------------------------------------
# Average over the last hour
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 1
  sampling_size: 720
  precision: 3

# ---------------------------------------------------------------------------------------------
# Average over the last 12 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last 12 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 12
  sampling_size: 8640
  precision: 3
  
# ---------------------------------------------------------------------------------------------
# Average over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3
  
# ---------------------------------------------------------------------------------------------
# Minimum over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Minimum kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: value_min
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3  

# ---------------------------------------------------------------------------------------------
# Maximum over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Maximum kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: value_max
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3  
  
# System Monitor Sensors

- platform: systemmonitor
  resources:
    - type: disk_use_percent
      arg: /config
    - type: memory_free
    - type: disk_free
    - type: processor_temperature
    - type: network_in
      arg: enp0s17
#    - type: network_out
#      arg: enps

#History Stats

- platform: history_stats
  name: Study ON Today
  entity_id: light.study_cans_current_value
  state: "on"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

# Aurora Sensors

- platform: command_line
  name: percent
  command: "cat /config/value.csv"
  value_template: '{{ value.split(",")[0] }}'
  scan_interval: 600
  
- platform: command_line
  name: chance
  command: "cat /config/value.csv"
  value_template: '{{ value.split(",")[1] }}'
  scan_interval: 600

- platform: command_line
  name: percent1
  command: "cat /config/value.csv"
  value_template: '{{ value.split(",")[2] }}'
  scan_interval: 600
  
- platform: command_line
  name: chance1
  command: "cat /config/value.csv"
  value_template: '{{ value.split(",")[3] }}'
  scan_interval: 600

# ------------------------------------------------------------------------------------
# Riemann Sum Integrals for specific equipment
# ------------------------------------------------------------------------------------

- platform: integration
  source: sensor.wash_power
  name: washing_machine_energy
  unique_id: "Washing Machine Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.bar_fridge_power
  name: bar_fridge_energy
  unique_id: "Bar Refrigerator Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.coffee_bar_power
  name: coffee_bar_energy
  unique_id: "Coffee Bar Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.storage_fridge_power
  name: storage_frige_energy
  unique_id: "Storage Refrigerator Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.study_monitor_power
  name: study_monitor_energy
  unique_id: "Study Monitor Energy"
  round: 3
  unit_prefix: k
  unit_time: h
  
# ------------------------------------------------------------------------------------
# Riemann Sum Integrals for Solar 
# ------------------------------------------------------------------------------------

- platform: integration
  source: sensor.powerwall_solar_now
  name: Solar Energy Produced
  unique_id: "Solar Energy Produced"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.powerwall_load_now
  name: Energy Consumed
  unique_id: "Energy Consumed By House"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_to_grid
  name: Energy to Grid
  unique_id: "Energy to Grid"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_from_grid
  name: Energy from Grid
  unique_id: "Energy from Grid"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_to_battery
  name: Energy to Battery
  unique_id: "Energy to Battery"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_from_battery
  name: Energy from Battery
  unique_id: "Energy from Battery"
  round: 3
  unit_time: h

# ------------------------------------------------------------------------------------
# Retrieve individual power from sensors via attributes
# ------------------------------------------------------------------------------------

- platform: template
  sensors:
    wash_power:
      friendly_name_template: "washing machine power"
      value_template: "{{ states('sensor.sonoff_100153b37b_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Washing machine power"

    bar_fridge_power:
      friendly_name_template: "bar fridge power"
      value_template: "{{ states('sensor.sonoff_100153c633_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Bar Refrigerator Power"
     
    coffee_bar_power:
      friendly_name_template: "coffee bar power"
      value_template: "{{ states('sensor.sonoff_100153b891_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Coffee Bar Power"
      
    storage_fridge_power:
      friendly_name_template: "storage fridge power"
      value_template: "{{ states('sensor.sonoff_100153a3fc_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Storage Refrigerator Power"

    study_monitor_power:
      friendly_name_template: "study monitor power"
      value_template: "{{ states('sensor.sonoff_100153c279_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Study Monitor Power"

 #-----------------------------------------------------------------------------------------------------------------------
 #
 #  Start the powercalc sensors.  Units are in kWh
 #
 #-----------------------------------------------------------------------------------------------------------------------
- platform: powercalc
  entity_id: light.study_cans_current_value
  create_energy_sensor: true
  energy_sensor_unit_prefix: 'k'
  mode: 'fixed'
  linear:
    min_power: 0.0
    max_power: 0.068
    attribute: input_number.brightness
#- platform: powercalc
#  entity_id: 
#  create_energy_sensor: true
#  energy_sensor_unit_prefix: 'kWh'
#  mode: 'fixed'

#### -----------> and here are my utility meters:


#  ------------------------------------------------------------------------------------
#  Energy Consumed
#  ------------------------------------------------------------------------------------
  daily_energy:
    source: "sensor.energy_consumed"
    name: "Daily kWh Usage"
    cycle: daily

  weekly_energy:
    source: "sensor.energy_consumed"
    name: "Weekly kWh Usage"
    cycle: weekly

  monthly_energy:
    source: "sensor.energy_consumed"
    name: "Monthly kWh Usage"
    cycle: monthly

  yearly_energy:
    source: "sensor.energy_consumed"
    name: "Yearly kWh Usage"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Solar Energy Produced
#  ------------------------------------------------------------------------------------
  daily_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Daily kWh Produced"
    cycle: daily

  weekly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Weekly kWh Produced"
    cycle: weekly

  monthly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Monthly kWh Produced"
    cycle: monthly

  yearly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Yearly kWh Produced"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy to Battery
#  ------------------------------------------------------------------------------------
  daily_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Daily kWh to Battery"
    cycle: daily

  weekly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Weekly kWh to Battery"
    cycle: weekly

  monthly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Monthly kWh to Battery"
    cycle: monthly

  yearly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Yearly kWh to Battery"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy from Battery
#  ------------------------------------------------------------------------------------
  daily_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Daily kWh from Battery"
    cycle: daily

  weekly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Weekly kWh from Battery"
    cycle: weekly

  monthly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Monthly kWh from Battery"
    cycle: monthly

  yearly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Yearly kWh from Battery"
    cycle: yearly
    
#  ------------------------------------------------------------------------------------
#  Energy to Grid
#  ------------------------------------------------------------------------------------
  daily_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Daily kWh to Grid"
    cycle: daily

  weekly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Weekly kWh to Grid"
    cycle: weekly

  monthly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Monthly kWh to Grid"
    cycle: monthly

  yearly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Yearly kWh to Grid"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy from Grid
#  ------------------------------------------------------------------------------------
  daily_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Daily kWh from Grid"
    cycle: daily

  weekly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Weekly kWh from Grid"
    cycle: weekly

  monthly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Monthly kWh from Grid"
    cycle: monthly

  yearly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Yearly kWh from Grid"
    cycle: yearly
  1. yes or make reserve 0%

  2. No I do not.

Here is a screenshot of one of my energy pages:

2 Likes

Interesting. Thank you for sharing this. I just ended up creating two new sensors that for powerwall_charge and powerwall_backup_reserve that use the formula I provided above so it’s using the same value you see in the Tesla mobile app.

I definitely like the time formatting as X:XX better than X hours XX minutes. If it’s a single digit minute, does the lack of leading “0” bother you? I’m a super n00b on templating, so trying to figure out how to force that leading zero.

The leading zero bugs me but not enough to fix it. It would involve getting the length of the string and then an if statement to prepend the “0”. I use it to get an idea of how much time I have on the batteries. It’s better than doing it in my head.

I set up one of the templates to have a leading zero and post it here when I am done.

edit: Add this to the templates and it will have a leading 0.

{% if minutes  <= 9 %}
  {% set min = '0' ~ minutes | string %}
{% else %}
   {% set min = minutes | string %}
{% endif %}
{{ decimal_hours ~ ':' ~ min }}

1 Like

Another option I found through some more research that appears to output the information in X:XX time format - replace the last line of each sensor that currently has “{{ decimal_hours…” with the following:

      {{ '{:2}:{:02}'.format(decimal_hours | int(00), minutes | int(00)) }}

I’m testing both side-by-side on a few sensors to see if there are any issues over the next 24 hours.

@AllHailJ What time value do you get when your Powerwall goes beyond your reserve?

e.g. Mine hit 20% and all of the sensors that I’m testing showed a variation of 0:00. But for some reason, my Powerwall ended up draining to 19%, despite the reserve set at 20%, and now the sensors show a variation of 0:53.

I’m thinking I might need to add some logic that recognizes anything under the reserve should be 0. Based on what you recently shared for the leading 0, I added this logic before the output line and it appears to output 0:00.

{% if states('sensor.powerwall_charge_actual') | float(0) < 20 %}
  {% set minutes = 0 ~ 0 | string %}
  {% set decimal_hours = 0 | string %}
{% endif %}

Anything you’d change or any reason you wouldn’t recommend this modification?

The reserve is pulled from the Gateway interface. I keep my reserve at 0 on the app and
sensor.powerwall_backup_reserve shows up as 5%. When I set it as 20% reserve this entity shows 24%.

It should be 25% but it does not show correctly but when you go to 0% it shows correctly. I think this is a float to int conversion problem and it should be rounded not truncated.

The remaining time should reflect the reserve plus the 5% notwithstanding the rounding error.

I would make the ‘float(0) < 20’ float(0) < input_numbers.percent_tesla_reserve’ so you can modify from the UI.

1 Like

Could someone explain the power sources above, my understanding is as follows:
a = this is current solar production in KW (so KW / 1000 = watts)
b = this is power drawn by the house in KW
c = this is the current battery percentage (e.g 76%)

All of the entities come from the tesla powerwall integration.

a = solar panels current output
b = grid power output or input
c = battery power output or input

All are in KW

This will represent the current house load upon balance (a+b+c)
.

1 Like

Thanks! One other question, are the input and output values both positive? I have two sensors, one measures battery charge and the other measures battery discharge - both provide positive numeric data. I might need to adjust the discharge to a negative value depending on your response.

I am not using a Tesla Powerwall.

The Tesla powerwall reports values as positive and negative. If the individual values are always positive and zero out when necessary then you can just add/subtract all of the values together.

1 Like

How did you get the two power cards next to one another? Working off 2x stats?

I used the horizontal stack card and inserted the card. Notice that the energy (right) is Kwh but it is shown as Kw.

type: custom:power-flow-card
entities:
  battery: sensor.powerwall_battery_now
  battery_charge: sensor.powerwall_charge
  grid: sensor.powerwall_site_now
  solar: sensor.powerwall_solar_now
max_flow_rate: 2
watt_threshold: 0
kw_decimals: 2
title: Power Flow

type: custom:power-flow-card
title: Energy kWh
entities:
  battery:
    consumption: sensor.daily_kwh_from_battery
    production: sensor.daily_kwh_to_battery
  battery_charge: sensor.powerwall_charge
  grid:
    consumption: sensor.daily_kwh_from_grid
    production: sensor.daily_kwh_to_grid
  solar: sensor.daily_kwh_produced
max_flow_rate: 2
watt_threshold: 0
kw_decimals: 2
1 Like

Works well thanks

Great code. I have been trying to apply your code to my setup with a Solar Edge inverter and battery. Aside for the sensor names being different, the A B & C values required KW but my system reports in watts. I have changed the code to the following and am waiting for the sun to go down to test the results. Anyone else got the above code working with Solar Edge?

- platform: template
  sensors:
    solar_remaining_battery_hours:
      value_template: >-
        {% set percent = states('sensor.solaredge_b1_state_of_energy') | float(0) / 100.0 | float(0) %}
        {% set number = 1 | float(0) %}
        {% set energy = 9.7 | float(0) %}
        {% set reserve = (10 | float(5)) / 100.0 %}
        {% set charge = ((percent - reserve) * number * energy) | float(0) %}
        {% set a = states('sensor.solar_panel_production_w') | float(0) / 1000.0 %}
        {% set b = states('sensor.solaredge_m1_ac_power') | float(0) / 1000.0 %}
        {% set c = states('sensor.solaredge_b1_dc_power') | float(0) / 1000.0 %}
        {% set current_power = a + b + c %}
        {% set decimal_hours = charge / current_power %}
        {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
        {{ '{:2}:{:02}'.format(decimal_hours | int(00), minutes | int(00)) }}

Am not familiar with the batteries and hardware/software. a,b,c variables should be +/- in value.

So for powerwall

  1. 13.5 kwh per battery with maximum available of 95%
  2. All entities are positive/negative (except percent) depending on the flow of power.

So for your exampe your would have 1 x 9.7 * (??-0.1) so if you are fully charged your power available would be 9.7 x 0.9 ~= 8.73 kWh

a,b,c are power flow

my guess is the problem is +/- for values. Just a guess

I don’t suppose anyone would be able to help with a “Time to full” sensor?

I’ve been trying to reverse engineer the above codes but clearly don’t know what im doing…

Someone can create a blueprint with this?

That is quite simple and here is the code. This is in my template.yaml file need template: if adding to config.

#--------------------------------------------------------------------------------------------------
# Battery Energy to Full
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_energy_to_full"
    unique_id: "solar_battery_energy_to_full"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(0)) / 100.0 | float(0) %}
      {% set charge = ((1.0 - percent) * number * energy) | float(0) %}
      {{ charge }}

Here is the sensor. I have this in my templates.yaml so if adding it to config your need a templates:

#--------------------------------------------------------------------------------------------------
# Time to Charge Battery to full
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "battery_time_to_full"
    unique_id: "battery_time_to_full"
    state: >
      {% set a = states('sensor.battery_energy_to_full') | float(0.0001) %}
      {% set b = states('sensor.power_to_battery') | float(0.0001) %}
      {% if b > 0.001 %}
        {% set decimal_hours = a / b %}
      {% else %}
        {% set decimal_hours = 0.0 %}
      {% endif %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}

Here is one of my energy displays with time to charge added

Do you have a standalone sensor setup somewhere to measure your daily home energy consumption (the 20.9 kWh on the right)? If so, could you please share how you set that up?

I will share all of my setup around solar. I use multiple files and the initial setup is in configuration.yaml. The value used for KWh is derived from the load_now sensor through a Riemann integral.

In configuration.yaml: ---------------------------------------------------------------------------------------------------

utility_meter: !include utility_meter.yaml
template: !include templates.yaml
sensor: !include sensors.yaml

In my sensors.yaml I have this: …

# ---------------------------------------------------------------------------------------------
# Average produced over the last hour
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Produced Last Hour"
  entity_id: sensor.powerwall_solar_now
  state_characteristic: mean
  max_age:
    hours: 1
  sampling_size: 720
  precision: 3
# ---------------------------------------------------------------------------------------------
# Average over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Produced Last 24 Hour"
  entity_id: sensor.powerwall_solar_now
  state_characteristic: mean
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3

# ---------------------------------------------------------------------------------------------
# Average over the last hour
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 1
  sampling_size: 720
  precision: 3

# ---------------------------------------------------------------------------------------------
# Average over the last 12 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last 12 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 12
  sampling_size: 8640
  precision: 3
  
# ---------------------------------------------------------------------------------------------
# Average over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Average kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: mean
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3
  
# ---------------------------------------------------------------------------------------------
# Minimum over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Minimum kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: value_min
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3  

# ---------------------------------------------------------------------------------------------
# Maximum over the last 24 hours
# ---------------------------------------------------------------------------------------------
- platform: statistics
  name: "Maximum kW Used Last 24 Hour"
  entity_id: sensor.powerwall_load_now
  state_characteristic: value_max
  max_age:
    hours: 24
  sampling_size: 17280
  precision: 3
# ------------------------------------------------------------------------------------
# Riemann Sum Integrals for specific equipment
# ------------------------------------------------------------------------------------

- platform: integration
  source: sensor.wash_power
  name: washing_machine_energy
  unique_id: "Washing Machine Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.bar_fridge_power
  name: bar_fridge_energy
  unique_id: "Bar Refrigerator Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.coffee_bar_power
  name: coffee_bar_energy
  unique_id: "Coffee Bar Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.storage_fridge_power
  name: storage_frige_energy
  unique_id: "Storage Refrigerator Energy"
  round: 3
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.study_monitor_power
  name: study_monitor_energy
  unique_id: "Study Monitor Energy"
  round: 3
  unit_prefix: k
  unit_time: h
  
# ------------------------------------------------------------------------------------
# Riemann Sum Integrals for Solar 
# ------------------------------------------------------------------------------------

- platform: integration
  source: sensor.powerwall_solar_now
  name: Solar Energy Produced
  unique_id: "Solar Energy Produced"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.powerwall_load_now
  name: Energy Consumed
  unique_id: "Energy Consumed By House"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_to_grid
  name: Energy to Grid
  unique_id: "Energy to Grid"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_from_grid
  name: Energy from Grid
  unique_id: "Energy from Grid"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_to_battery
  name: Energy to Battery
  unique_id: "Energy to Battery"
  round: 3
  unit_time: h

- platform: integration
  source: sensor.power_from_battery
  name: Energy from Battery
  unique_id: "Energy from Battery"
  round: 3
  unit_time: h

# ------------------------------------------------------------------------------------
# Retrieve individual power from sensors via attributes
# ------------------------------------------------------------------------------------

- platform: template
  sensors:
    wash_power:
      friendly_name_template: "washing machine power"
      value_template: "{{ states('sensor.sonoff_100153b37b_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Washing machine power"

    bar_fridge_power:
      friendly_name_template: "bar fridge power"
      value_template: "{{ states('sensor.sonoff_100153c633_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Bar Refrigerator Power"
     
    coffee_bar_power:
      friendly_name_template: "coffee bar power"
      value_template: "{{ states('sensor.sonoff_100153b891_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Coffee Bar Power"
      
    storage_fridge_power:
      friendly_name_template: "storage fridge power"
      value_template: "{{ states('sensor.sonoff_100153a3fc_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Storage Refrigerator Power"

    study_monitor_power:
      friendly_name_template: "study monitor power"
      value_template: "{{ states('sensor.sonoff_100153c279_power') | float(0.0) }}"
      unit_of_measurement: W
      unique_id: "Study Monitor Power"

 #-----------------------------------------------------------------------------------------------------------------------
 #
 #  Start the powercalc sensors.  Units are in kWh
 #
 #-----------------------------------------------------------------------------------------------------------------------
- platform: powercalc
  entity_id: light.study_cans_current_value
  create_energy_sensor: true
  energy_sensor_unit_prefix: 'k'
  mode: 'fixed'
  linear:
    min_power: 0.0
    max_power: 0.068
    attribute: input_number.brightness

In my templates.yaml file I have: ---------------------------------------------------------------------

#--------------------------------------------------------------------------------------------------
# Time to Charge Battery to full
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "battery_time_to_full"
    unique_id: "battery_time_to_full"
    state: >
      {% set a = states('sensor.solar_battery_energy_to_full') | float(0.0001) %}
      {% set b = states('sensor.power_to_battery') | float(0.0001) %}
      {% if b > 0.001 %}
        {% set decimal_hours = a / b %}
      {% else %}
        {% set decimal_hours = 0.0 %}
      {% endif %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Current Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life"
    unique_id: "solar_battery_life"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set a = states('sensor.powerwall_solar_now') | float(0.0001) %}
      {% set b = states('sensor.powerwall_site_now') | float(0.0001) %}
      {% set c = states('sensor.powerwall_battery_now') | float(0.0001) %}
      {% set current_power = a + b + c %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Average 1 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_1_hr"
    unique_id: "solar_battery_life_1_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}


#--------------------------------------------------------------------------------------------------
# Battery Life at Average 12 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_12_hr"
    unique_id: "solar_battery_life_12_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_12_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours| int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}


#--------------------------------------------------------------------------------------------------
# Battery Life at Average 24 hr Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_24_hr"
    unique_id: "solar_battery_life_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.average_kw_used_last_24_hour') | float(0.0001) %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Minimum 24 hr Power Draw - longest battery life
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_minimum_24_hr"
    unique_id: "solar_battery_life_minimum_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0.01) / 100.0 %}
      {% set number = states('input_number.number_of_batteries') | float(2) %}
      {% set energy = 13.5 | float(13.5) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.minimum_kw_used_last_24_hour') | float(0.01) %}
      {% if current_power <= 0.0001 %}
        {% set current_power = 0.5 %}
      {% endif %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}

#--------------------------------------------------------------------------------------------------
# Battery Life at Maximum 24 hr Power Draw - shortest battery life
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_maximum_24_hr"
    unique_id: "solar_battery_life_maximum_24_hr"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0.01) / 100.0 %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %} 
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% set current_power =  states('sensor.maximum_kw_used_last_24_hour') | float(0.0001) %}
      {% if current_power <= 0.0001 %}
        {% set current_power = 0.5 %}
      {% endif %}
      {% set decimal_hours = charge / current_power %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}


#--------------------------------------------------------------------------------------------------
# Battery Life at Battery Power Draw - Life at Current Power Draw
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_life_battery_power"
    unique_id: "solar_battery_life_battery_power"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 %}
      {% set current_power =  states('sensor.powerwall_battery_now') | float(0.0001) | abs %}
      {% if current_power < 0.1 %}
        {% set current_power = 0.1 %}
      {% endif %} 
      {% if states('sensor.powerwall_battery_now') | float(0) < -0.001 %}
        {% set charge = ((1.0 - percent) * number * energy) | float(0) %}
      {% else %}
        {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {% endif %}
      {% set decimal_hours = charge / (current_power + 0.0001) %}
      {% set minutes = (decimal_hours % 1 * 60) | round(0) %}
      {% set decimal_hours = decimal_hours | int(0) %}
      {% if minutes  <= 9 %}
        {% set min = '0' ~ minutes | string %}
      {% else %}
        {% set min = minutes | string %}
      {% endif %}
      {{ decimal_hours ~ ':' ~ min }}


#--------------------------------------------------------------------------------------------------
# Battery energy stored currently
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_energy_available"
    unique_id: "solar_battery_energy_available"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(0)) / 100.0 | float(0)%}
      {% set charge = ((percent - reserve) * number * energy) | float(0) %}
      {{ charge }}

#--------------------------------------------------------------------------------------------------
# Battery Energy to Full
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_battery_energy_to_full"
    unique_id: "solar_battery_energy_to_full"
    state: >
      {% set percent = states('sensor.powerwall_charge') | float(0) / 100.0 | float(0) %}
      {% set number = states('input_number.number_of_batteries') | float(0) %}
      {% set energy = 13.5 | float(0) %}
      {% set reserve = (states('sensor.powerwall_backup_reserve') | float(0)) / 100.0 | float(0) %}
      {% set charge = ((1.0 - percent) * number * energy) | float(0) %}
      {{ charge }}
      
#----------------------------------------------------------------------------------------------------------------------------------------------------
#
#  This section is for power and energy based on Tesla gateway.  I convert all negative to positive
#  I set to 0.0 when energy is not flowing in the correct direction for the sensor.
#  Sensors Available
#    sensor.powerwall_solar_now         - solar power being produced
#    sensor.powerwall_site_now          - grid power to or from
#    sensor.powerwall_battery_now       - battery power to or from
#    sensor.powerwall_load_now          - house power usage
#    sensor.grid_to_home_energy_price
#    sensor.solar_to_grid_energy_price
#
#  These sensors in turn drive the following integrations for energy
#  Sensors Available
#    sensor.solar_energy_produced
#    sensor.energy_consumed
#    sensor.energy_to_grid
#    sensor.energy_from_grid
#    sensor.energy_to_battery
#    sensor.energy_from_battery
#----------------------------------------------------------------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------------
# For Grid to Home/Battery pricing - Usage price I pay from the grid
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "grid_to_home_energy_price"
    unique_id: "Grid to Home Energy_Price"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) > 0.001 %}
        {{ state('input_number.kwh_cost_from_grid') }}
      {% else %}
        0.000 
      {% endif %}
    unit_of_measurement: "USD/kWh"

#--------------------------------------------------------------------------------------------------
# For Solar to Grid pricing - Generation price I sell to the grid
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "solar_to_grid_energy_price"
    unique_id: "Solar to Grid Energy_Price"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) < -0.001 %}
        {{ states('input_number.kwh_revenue_to_grid') }}
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "USD/kWh"

#--------------------------------------------------------------------------------------------------
# Power to Grid - Make Positive
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power to Grid"
    unique_id: "Power to Grid"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) < -0.0001 %}
        {{ states('sensor.powerwall_site_now') | float(0) | abs}} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power from Grid - Make Positive 
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power from Grid"
    unique_id: "Power From Grid"
    state: >-
      {% if states('sensor.powerwall_site_now') | float(0) > 0.001 %}
        {{ states('sensor.powerwall_site_now') }}
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power to Battery  - Make Positive  sensor.powerwall_battery_now
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power to Battery"
    unique_id: "Power to Battery"
    state: >-
      {% if states('sensor.powerwall_battery_now') | float(0) < -0.001 %}
        {{ states('sensor.powerwall_battery_now') | float(0) | abs}} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"

#--------------------------------------------------------------------------------------------------
# Power from Battery  - Make Positive  sensor.powerwall_battery_now
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Power from Battery"
    unique_id: "Power from Battery"
    state: >-
      {% if states('sensor.powerwall_battery_now') | float(0) > 0.001 %}
        {{ states('sensor.powerwall_battery_now') }} 
      {% else %}
        0.000
      {% endif %}
    unit_of_measurement: "kW"
    
#--------------------------------------------------------------------------------------------------
# AC Upstairs Energy - sensor.phase_1_a_c_upstairs_energy
# Power and Energy is only being measured on one phase - Need to Double for 230v
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Upstairs AC Energy"
    unique_id: "Upstairs AC Energy"
    state: "{{ states('sensor.phase_1_a_c_upstairs_energy') | float(0) * 2.000 }}"
    unit_of_measurement: "kWh"
#--------------------------------------------------------------------------------------------------
# AC Downstairs Energy - sensor.phase_1_a_c_downstairs_energy
# Power and Energy is only being measured on one phase - Need to Double for 230v
#--------------------------------------------------------------------------------------------------
- sensor:
  - name: "Downstairs AC Energy"
    unique_id: "Downstairs AC Energy"
    state: "{{ states('sensor.phase_1_a_c_down_energy') | float(0) * 2.000 }}"
    unit_of_measurement: "kWh"

In my utility_meters.yaml file I have this: ---------------------------------------------------------------------------------

#  ------------------------------------------------------------------------------------
#  Energy Consumed
#  ------------------------------------------------------------------------------------
  daily_energy:
    source: "sensor.energy_consumed"
    name: "Daily kWh Usage"
    cycle: daily

  weekly_energy:
    source: "sensor.energy_consumed"
    name: "Weekly kWh Usage"
    cycle: weekly

  monthly_energy:
    source: "sensor.energy_consumed"
    name: "Monthly kWh Usage"
    cycle: monthly

  yearly_energy:
    source: "sensor.energy_consumed"
    name: "Yearly kWh Usage"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Solar Energy Produced
#  ------------------------------------------------------------------------------------
  daily_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Daily kWh Produced"
    cycle: daily

  weekly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Weekly kWh Produced"
    cycle: weekly

  monthly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Monthly kWh Produced"
    cycle: monthly

  yearly_energy_produced:
    source: "sensor.solar_energy_produced"
    name: "Yearly kWh Produced"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy to Battery
#  ------------------------------------------------------------------------------------
  daily_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Daily kWh to Battery"
    cycle: daily

  weekly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Weekly kWh to Battery"
    cycle: weekly

  monthly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Monthly kWh to Battery"
    cycle: monthly

  yearly_energy_battery_to:
    source: "sensor.energy_to_battery"
    name: "Yearly kWh to Battery"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy from Battery
#  ------------------------------------------------------------------------------------
  daily_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Daily kWh from Battery"
    cycle: daily

  weekly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Weekly kWh from Battery"
    cycle: weekly

  monthly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Monthly kWh from Battery"
    cycle: monthly

  yearly_energy_battery_from:
    source: "sensor.energy_from_battery"
    name: "Yearly kWh from Battery"
    cycle: yearly
    
#  ------------------------------------------------------------------------------------
#  Energy to Grid
#  ------------------------------------------------------------------------------------
  daily_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Daily kWh to Grid"
    cycle: daily

  weekly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Weekly kWh to Grid"
    cycle: weekly

  monthly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Monthly kWh to Grid"
    cycle: monthly

  yearly_energy_grid_to:
    source: "sensor.energy_to_grid"
    name: "Yearly kWh to Grid"
    cycle: yearly

#  ------------------------------------------------------------------------------------
#  Energy from Grid
#  ------------------------------------------------------------------------------------
  daily_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Daily kWh from Grid"
    cycle: daily

  weekly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Weekly kWh from Grid"
    cycle: weekly

  monthly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Monthly kWh from Grid"
    cycle: monthly

  yearly_energy_grid_from:
    source: "sensor.energy_from_grid"
    name: "Yearly kWh from Grid"
    cycle: yearly

And finally here is the setup on the card - one for the powere and one for the energy: ----------------------

type: custom:power-flow-card
entities:
  battery: sensor.powerwall_battery_now
  battery_charge: sensor.powerwall_charge
  grid: sensor.powerwall_site_now
  solar: sensor.powerwall_solar_now
max_flow_rate: 2
watt_threshold: 0
kw_decimals: 2
title: Power Flow

For the Energy Card: ----------------------------------------------------------------------------------------------------------------

type: custom:power-flow-card
title: Energy kWh
entities:
  battery:
    consumption: sensor.daily_kwh_from_battery
    production: sensor.daily_kwh_to_battery
  battery_charge: sensor.powerwall_charge
  grid:
    consumption: sensor.daily_kwh_from_grid
    production: sensor.daily_kwh_to_grid
  solar: sensor.daily_kwh_produced
max_flow_rate: 2
watt_threshold: 0
kw_decimals: 2
1 Like