# Calculate estimated monthly kWh value

I just figure out the formula for calculate the estimated monthly kWh as below

(estimated monthly kWh) = [ (last period of kWh/day) x (remaining day) ] + (accumulated kWh/month)

But to run it on HA, I encounter some problem on calculating/extract some value on the formula.

Detail of the formula description:

• “estimated monthly kWh” is for calculate the estimated month end kWh value even before reach the billing cycle

• last period = yesterday kWh/day value (I have this value in helper sensor but not sure how to extract it)

• remaining day = as my electric billing cycle is falling on 20th every month, which HA helper or similar sensor can calculate this.(for example, current date is 5th, I need something that can detect current date & 20-5=15 days. But if current date is 26th, 30-6=24 days. Another problem is some month have 30, while other have 31, how to compensate this?

• accumulated kWh/month = I have this value in helper which I can extract out

Is that any other method to calculate the estimated monthly kWh? your help is appreciated.

Don’t know if this helps, but I calculate the amount of gas used so far in the month with:

``````  - sensor:
- name: "gas used this month"
state: '{{ (states("input_number.gas_daily") | float(0)) * (now().day) }}'
unit_of_measurement: "m³"
device_class: gas
state_class: total_increasing

``````
1 Like

I just calculate an average for the current month, forecasted, like this:

``````    monthly_energy_consumption_forecast:
unit_of_measurement: "kWh"
value_template: >
{% set now = now() %}
{% set month = now.month %}
{% set year = now.year %}
{% set days = None %}
{# feb: leap years #}
{% if month == 2 and year is divisibleby 4 and not year is divisibleby 400 %}
{% set days = 29 %}
{# jan, mar, may, jul, aug, oct, dec #}
{% elif month in [1,3,5,7,8,10,12] %}
{% set days = 31 %}
{# apr, jun, sep, nov #}
{% elif month in [4,6,9,11] %}
{% set days = 30 %}
{# feb #}
{% else %}
{% set days = 28 %}
{% endif %}
{{ ((states('sensor.monthly_energy_consumption') | int) / now.day * days) | round(3) }}
``````

The sensor `sensor.monthly_energy_consumption` is a utility meter, which is based on a power meter I have.

1 Like

hey,
couldnt resist to share an alternative template to calculate days of the current month, which would make this a bit shorter:

``````      - unique_id: days_current_month
name: Days current month
state: >
{{ ((now().replace(day=1) +
timedelta(days=31)).replace(day=1) - timedelta(days=1)).day }}
``````

had the day counter like you have before, but then Petro jumped in

2 Likes

Ooh, excellent, thanks Marius. I was thinking of using Petro’s new template macro package, but saw he doesn’t have that macro in there. I’ve asked for it now.

1st of all, thanks @Stiltjack @parautenbach @Mariusthvdb for suggestion. I really appreciated.

@Mariusthvdb

``````  - unique_id: days_current_month
name: Days current month
state: >
{{ ((now().replace(day=1) +
timedelta(days=31)).replace(day=1) - timedelta(days=1)).day }}
``````

may I know what platform should I declare for the above command? if 20th is day of the billing cycle start, should I replace the “day=1 to day=20” ? another thing is, does timedelta command including the leap year ?

remark:
I just notice your code is to calculate the total days of current months.

btw, I able to retrieve the last period value using value template

``````# last period value of overall kWh/day
- platform: template
sensors:
last_period_overall_kwhperday:
unit_of_measurement: "kWh"
unique_id: 4e18e33d-09f1-4273-918f-c6bd2567810b
value_template: "{{ state_attr('sensor.overall_kwh_day', 'last_period') }}"
``````
1 Like

yes this is a `template:` sensor, and as such, could replace the section in Pieters template for monthly_energy_consumption_forecast that calculates the amount of days in the month. The `days` variable

1 Like

problem solved, I have the code for calculate the remaining days as below

``````{% set x = 1 if now().day >= 20 else 0 %}
{{ (today_at().replace(month= now().month + x,
day = 20) - today_at()).days }}
``````

@Didgeridrew thanks again.

the final result as below

correction, I change description from overall kWh/month to current kWh/month as it is more appropriate.