Adding a input number of weeks to an input starting date

Hello,
I’m sure this is very simple, maybe someone can help me.
I need to display in my dashboard a value that is the SUM of an input given date + an input given number of weeks (or days). To the input date, I’m calling start_date. To the input weeks I’m calling 2nd_stage. It would be convenient that HA could give a default value for both the start_date and for the 2nd_stage.
I managed to create the a functional input for the start date (yeah!)
But I’m stuck in how can I create the input for the week/days (coded in bold)
In my config file, I’ve created these inputs:

input_datetime:
  start_date:
    name: Start Date
    has_date: true
    has_time: false
  2nd_stage:
    name: 2nd Stage
    **has_date: true**
    **has_time: false**

And these sensors:

sensor:
# TEMPLATE SENSORS    
  - platform: template
    sensors:
      days_since_the_start:
        friendly_name: "Days Since the Start"
        icon_template: mdi:calendar
        value_template: '{{ ((as_timestamp(now()) - (states.input_datetime.start_date.attributes.timestamp)) | int /60/1440) | round(0) }}'
      2nd_stage_date:
        friendly_name: "2nd Stage Date"
        icon_template: mdi:calendar
        value_template: '{{ ((as_timestamp(states.input_datetime.start_date.attributes.timestamp) **+ (x week or days) | int /60/1440//7 +1)  | round(0) }}'** 
# TIME/DATE SENSOR
  - platform: time_date
    display_options:
      - 'time'
      - 'date'

The bold chunks are the ones I’m aware that are messy. Can someone help me figuring this out?

Please edit your post and format it correctly. You want the code blocks format </> not the quote format.

I think its good now…sorry, still learning about how to post.

I prefer to use datetime objects intead of timestamp math since timedelta() was added a while back.

sensor:
# TEMPLATE SENSORS    
  - platform: template
    sensors:
      days_since_the_start:
        friendly_name: "Days Since the Start"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      2nd_stage_date:
        friendly_name: "2nd Stage Date"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.start_date') | as_datetime | as_local %}
          {{ (start + timedelta( days = X )).date() }}

Thank you very much for your kind help. I already learned something very important: timedelta makes this stuff easier.
I tried your suggestion but I still seem not able to put it to work.
So, I refurbish a bit my draft, and here is what I have now.
Helpers:


My config sensors:

# TEMPLATE SENSORS  
  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(days = 'input_number.total_germination_time')).date() }}
      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('planned_veg_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(days = 'input_number.total_vegative_time')).date() }}

And finally my dashboard with an indication of what I need:

In summary, I need to get an automatic value for Veg Start Date (planned) as a date, by adding the Grow Start Date and the Total Germination Time (with a default value of 3 days).
Also I need to get an automatic value for Flower Start Date (planned) as a date, by adding the Veg Start Date (planned) and the Total Vegetative Time (with a default value of 3 weeks).
Still struggling!

You just have a couple issues:

First, timedelta() needs a number value for your unit (hours, days, weeks, etc.). In the following:

'input_number.total_germination_time' is not a number, it’s a string. To get the value of that entity’s state as a whole number (integer) you need to use:

states('input_number.total_germination_time') | int(0)

Second, in the “flower start date” sensor, you have also set the unit for timedelta() to “days”, but your Input number helper is in weeks. You could do math in the template, but it is easier to just change the unit.

{{ (start + timedelta(weeks = states('input_number.total_vegative_time') | int(0) )).date() }}

Putting it all together:

# TEMPLATE SENSORS  
  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {% set germ_days = states('input_number.total_germination_time') | int(0) %}
          {{ (start + timedelta(days = germ_days)).date() }}
      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('sensor.planned_veg_start_date') | as_datetime | as_local %}
          {% set veg_weeks = timedelta(weeks = states('input_number.total_vegative_time') | int(0)) %}
          {{ (start + veg_weeks).date() }}

EDIT: Modified to show two ways of setting up timedelta template, use whichever you prefer.
Corrected missing domain.

1 Like

Dear @Didgeridrew I think I’m almost there, thanks to you! Thank you.

Now the Veg Start Date (planned) works, but the Flower Start Date (planned) stills unavailable.

Here’s what I’ve copy/pasted from your suggestion, plus some a small typo correction “vegative”:

planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('planned_veg_start_date') | as_datetime | as_local %}
          {% set veg_weeks = timedelta(weeks = states('input_number.total_vegetative_time') | int) %}

You’re missing the final line of the template:

  value_template: >
    {% set start = states('sensor.planned_veg_start_date') | as_datetime | as_local %}
    {% set veg_weeks = timedelta(weeks = states('input_number.total_vegetative_time') | int(0)) %}
    {{ (start + veg_weeks).date() }}      # THIS LINE IS MISSING

If I go like this, both of the “Planned” variables turn out as unavailable:

  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(days = states('input_number.total_germination_time') | int)).date() }}
          {{ (start + timedelta(days = germ_days)).date() }}
      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('planned_veg_start_date') | as_datetime | as_local %}
          {% set veg_weeks = timedelta(weeks = states('input_number.total_vegetative_time') | int) %}
          {{ (start + veg_weeks).date() }}

If I go like this, the first one outputs ok but the second one renders as unavailable:

  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(days = states('input_number.total_germination_time') | int)).date() }}
      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('planned_veg_start_date') | as_datetime | as_local %}
          {% set veg_weeks = timedelta(weeks = states('input_number.total_vegetative_time') | int) %}

If I go with this last one and add the final line, I still get the 2nd “Planned” as unavailable.

If the two line version like I showed for planned_veg_start_date is working for you, use that for the flower start sensor as well… but both should work, and what you posted in your last comment was still missing the last line. That last line, which is wrapped in {{ }}, is the expression. The expression is the part of the template that is printed to the template output. Without it, there is no output for the sensor, which is why it would come up as constantly “unavailable”.

In the last example you are also missing a domain. (I am partly to blame for that one because I missed that you had left it out in your original post.)

states('planned_veg_start_date') is meaningless. If you want to get the state of the “Veg Start Date (planned)” sensor you need to tell the template it’s a sensor: states('sensor.planned_veg_start_date')

  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}

      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ ( start + timedelta(days = states('input_number.total_germination_time') | int(0)) ).date() }}

      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('sensor.planned_veg_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(weeks = states('input_number.total_vegetative_time') | int(0)) ).date() }}
1 Like

@Didgeridrew , it is beautifully working now! thank you very much for your kind help, you’re my first contact with this community, I won’t forget that :slight_smile:

1 Like