@petro or any other templating guru out there I would like to be able to do the following.
Calculate an estimate in predicted electricity spend. I was thinking of using a calculation for daily average so far so would need a template to tell me how many days into the current month it is.
I would then want to use that average and times it by the number of remaining days left in the current month.
Template one - how many days into current month.
Template two - how many days left in current month.
So, here’s what I would use. Works even during leap years. Gotta use the time_date platform to get a sensor that updates once a day. Using this will create sensor.date.
sensor:
- platform: time_date
display_options:
- 'date'
- platform: template
sensors:
remaining_days:
entity_id:
- sensor.date
value_template: >
{% set this = now().replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
{% set last = this.replace(month=next, day=1) %}
{{ (last.date() - this.date()).days }}
- platform: template
sensors:
past_days:
entity_id:
- sensor.date
value_template: >
{% set this = now().replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
{% set first = this.replace(day=1) %}
{{ (this.date() - first.date()).days }}
Is it by design that remaining_days includes the current day?
For example, today is the 10th of May, a month with 31 days. The template reports there are 22 days remaining this month. Therefore it includes the current day (the 10th) as a ‘remaining day’.
In other words, if today was the 31st of May it would report there is one remaining day left.
I’m not claiming anything is wrong and only asking if this is the desired definition of ‘remaining days’ for this particular application.
EDIT I think I’m overlooking the fact this is looking at the remaining days next month … I see what its doing. It uses the first day of the next month , last.date(), to represent the end of the period.
Probably not, i should have subtracted 1 from this and added 1 to the other template. Really depends on what he wants. The calculation requires using the next months day 1 as that’s the only way to dynamically get to the last day of each month.
{% if is_state('input_boolean.alarmclock_wd_enabled','off') and
is_state('input_boolean.alarmclock_we_enabled','off') %} {% set daytype = 'Not set' %}
{% elif day_delta == '0' %} {% set daytype = 'Today,'%}
{% elif day_delta == '1' %} {% set daytype = 'Tomorrow,' %}
{% elif day_delta == '2' %} {% set daytype = 'The day after tomorrow,' %}
{% elif day_delta > '2' %} {% set daytype = 'Next' %}
{% endif %}
With this:
{% set daytypes = ['Today,', 'Tomorrow,', 'The day after tomorrow,'] %}
{% set disabled = is_state('input_boolean.alarmclock_wd_enabled','off') and is_state('input_boolean.alarmclock_we_enabled','off') %}
{% set daytype = 'Not set' if disabled else daytypes[day_delta] if day_delta < 3 else 'Next' %}
days_current_month:
friendly_name: Days current month
entity_id: sensor.date
value_template: >
{% set this = now() %}
{% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
{% set first = this.replace(day=1) %}
{% set last = first .replace(month=next) %}
{{ (last - first).days }}
{% set timezone = '+04:00' %}
{% set delta = states('sensor.number_of_days_next_alarm_macro') %}
{% if delta == 'Not set' %} {{ delta }}
{% else %}
{% set delta = delta | int + 1 %}
{% set time = now() %}
{% set mapper = [ 'Today', 'Tomorrow', 'The day after tomorrow,' ] %}
{% set disabled = is_state('input_boolean.alarmclock_wd_enabled','off') and is_state('input_boolean.alarmclock_we_enabled','off') %}
{% set daystring = 'Not set' if disabled else mapper[delta] if delta < mapper | length else 'Next' %}
{% set time = now() %}
{% set current = time.strftime('%j') | int %}
{% set next = current + delta if current + delta <= 365 else (current + delta)-365 %}
{% set year = time.year if current + delta <= 365 else time.year + 1 %}
{% set next = '{} {} 00:00:00{}'.format(year, next, timezone) %}
{% set next = as_timestamp(strptime(next , '%Y %j %H:%M:%S%z')) | timestamp_custom('%A, %-d %B') %}
{{ daystring }} {{ next }}
{% endif %}
You gotta add your own timezone instead of '+04:00'