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