# Simple Math ... Part II

Well another simple math question that I can’t find out…

I want to have a sensor that shows me the power usage of my home every day.

I believe I need the Energy Import from my P1 meter (sensor.stroommeter_totaal_verbruik) minus the Energy Export from my P1 meter (sensor.stroommeter_totaal_teruglevering).

Using the Utility Meter (Utility Meter - Home Assistant) I can reset that amount every 24h and with that information I can create a graph that shows me the usage every day.

Is this a correct tought?

If you set up the energy tab in HA it will automatically give you the daily energy usage. It also provides hourly, weekly, monthly, and yearly energy usage and a few more statistics. It will also help if you ever install batteries.

To use I created these sensors.

``````# ------------------------------------------------------------------------------------
# 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
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

# ------------------------------------------------------------------------------------
# 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

``````

and these 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"
cron: "0 0 1 1 *"

#  ------------------------------------------------------------------------------------
#  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"
cron: "0 0 1 1 *"

#  ------------------------------------------------------------------------------------
#  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"
cron: "0 0 1 1 *"

#  ------------------------------------------------------------------------------------
#  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"
cron: "0 0 1 1 *"

#  ------------------------------------------------------------------------------------
#  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"
cron: "0 0 1 1 *"

#  ------------------------------------------------------------------------------------
#  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"
cron: "0 0 1 1 *"
``````

I also created a few templates to make input fit the system

``````#--------------------------------------------------------------------------------------------------
# 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 %}
{{ states('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 then because Tesla batteries only discharge 95% of the nameplate I added this

``````#--------------------------------------------------------------------------------------------------
# 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(2) %}
{% 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(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.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(2) %}
{% set energy = 13.5 | float(0) %}
{% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 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(2) %}
{% set energy = 13.5 | float(0) %}
{% set reserve = (states('sensor.powerwall_backup_reserve') | float(5)) / 100.0 | float(0) %}
{% set charge = ((1.0 - percent) * number * energy) | float(0) %}
{{ charge }}
``````

Hope this helps