# Monthly Power Bill Estimator - Mathematics Formula help please

Hi all,
I’d like to estimate my monthly power bill but I can’t figure out the calculation or template formula to do that?
I’m using the integration “Dynamic Energy Cost” https://github.com/martinarva/dynamic_energy_cost/ to calculate the Daily cost of my home/grid power sensor.

My billing period is from the 27th to the 27th of each month.

Daily Sensor idea → eg. sensor.house_dynamic_daily_energy_cost (resets daily)
I wonder if a calculation can be performed on the “Dynamic Energy Cost” daily sensor where, for example, using historical data, if it is the 20th of the month (meaning there is only 7 days until the period ends) then an average cost for each day from the 27th to the 20th is calculated and then multiplied by 30 (assuming there is 30 days in this month)?

Monthly Sensor idea → eg. sensor.house_dynamic_monthly_energy_cost (resets 27th of each month)
Alternatively, there is also a Monthly sensor from the “Dynamic Energy Cost” integration… perhaps, using the same example as above, if the Monthly sensor has 23 days of total cost, this cost could be divided by 23 to get the average and then multiply that by 30?

Not sure if I’m on the right track here… any assistance would be greatly appreciated

For anyone interested… I think I figured it out
I’ve added the variables to the attributes part of the sensor. The code is a bit repetitive, but it works…

• It checks how many days between the 27th of each month (i.e some months have 30 days some have 31 etc)
• identify how many days have passed the 27th
• calculates the average daily cost
• calculates the monthly supply cost
• calculates an estimate monthly bill
``````  - platform: template
sensors:
energy_bill_estimate:
friendly_name: "Energy Bill Estimate"
unit_of_measurement: "\$"
value_template: >
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{% set days_remaining = ((current_date - this_month_27th).days) | abs %}
{% set days_past = days_in_billing_cycle - days_remaining | abs %}
{% set sensor_value = states('sensor.shelly_em_1_house_kwh_dynamic_monthly_energy_cost') | float %}
{% set average_daily_cost = sensor_value / days_past if days_past > 0 else 0 %}
{% set daily_supply_cost = states('input_number.energy_daily_supply_cost') | float %}
{% set total_supply_cost = daily_supply_cost * days_in_billing_cycle %}
{% set total_estimated_cost = (average_daily_cost * days_in_billing_cycle) + total_supply_cost %}
{{ total_estimated_cost | round(2) }}

attribute_templates:
days_in_billing_cycle: "
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{{ days_in_billing_cycle | round(2) }}"
days_remaining: "
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{% set days_remaining = ((current_date - this_month_27th).days) | abs %}
{{ days_remaining | round(2) }}"
days_past: "
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{% set days_remaining = ((current_date - this_month_27th).days) | abs %}
{% set days_past = days_in_billing_cycle - days_remaining | abs %}
{{ days_past | round(2) }}"
average_daily_cost: "
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{% set days_remaining = ((current_date - this_month_27th).days) | abs %}
{% set days_past = days_in_billing_cycle - days_remaining | abs %}
{% set sensor_value = states('sensor.shelly_em_1_house_kwh_dynamic_monthly_energy_cost') | float %}
{% set average_daily_cost = sensor_value / days_past if days_past > 0 else 0 %}
\${{ average_daily_cost | round(2) }}"
total_monthly_supply_cost: "
{% set now = utcnow() %}
{% set current_year = now.year %}
{% set current_month = now.month %}
{% set next_month = (current_month % 12) + 1 %}
{% set next_year = current_year + (1 if current_month == 12 else 0) %}
{% set current_date = now %}
{% set this_month_27th = current_date.replace(day=27) %}
{% set next_month_27th = this_month_27th.replace(year=next_year, month=next_month) %}
{% set days_in_billing_cycle = ((next_month_27th - this_month_27th).days) | abs %}
{% set days_remaining = ((current_date - this_month_27th).days) | abs %}
{% set sensor_value = states('sensor.shelly_em_1_house_kwh_dynamic_monthly_energy_cost') | float %}
{% set average_daily_cost = sensor_value / days_remaining if days_remaining > 0 else 0 %}
{% set daily_supply_cost = states('input_number.energy_daily_supply_cost') | float %}
{% set total_supply_cost = daily_supply_cost * days_in_billing_cycle %}
\${{ total_supply_cost | round(2) }}"
``````