Current week from a given sensor date

Hello, I’ve managed to set in my dashboard a sensor that outputs the number of weeks that have passed from a given input.datetime like so:

    sensor:
      current_week_of_grow:
        friendly_name: "Current Week of Grow"
        icon_template: mdi:calendar
        value_template: '{{ ((as_timestamp(now()) - (states.input_datetime.grow_start_date.attributes.timestamp)) | int /60/1440//7 +1)  | round(0) }}'
        entity_id: input_datetime.grow_start_date,sensor.time

I got myself this nice card with general information about my cucumber plants:
img

Except, what I really need is not to use the input_datetime.grow_start_date but the sensor.planned_veg_start_date, which will give me the accurate result for the number of the weeks that have passed. Both the input_datetime and the sensor.planned_veg_start_date are in the image.

I’m trying to play around with code and I reached this:

sensor:
       current_week_of_grow:
        friendly_name: "Current Week of Grow"
        icon_template: mdi:calendar
        value_template: '{{ ((as_timestamp(now()) - states('sensor.planned_veg_start_date')) | int /60/1440//7 +1)  | round(0) }}'
        entity_id: sensor.planned_veg_start_date,sensor.time

But, it is not event correct from the syntax point of view. I’m sure this would be evident for anyone with Yaml basics, as opposed to me, with no clue of what is happening.

Can someone help me fixing this? Thank you in advance.

I think it would be:

#------------------------------------------------------------------------------------------------------
#
# Sensor - sensor.current_week_of_grow.  604800 = 86400*7 or the number of
# seconds in a week.  This is setup to add to configuration.yaml.  Will default to 0 
# if the entities are unavailable.  Convert now and planned start date to unixtime 
# to get seconds. 
#
#-------------------------------------------------------------------------------------------------------
sensor:
  - platform: template
    sensors:
      current_week_of_grow:
        friendly_name: "Current Week of Grow"
        icon_template: mdi:calendar
        value_template: "{{ ((as_timestamp(now()) - as_timestamp(states('sensor.planned_veg_start_date'))) | int(0) / 604800 + 1)  | round(0, default=1) }}"
sensor:
  current_week_of_grow:
    friendly_name: "Current Week of Grow"
    icon_template: mdi:calendar
    value_template: >
      {{ ((now() - states('sensor.planned_veg_start_date') | as_datetime | as_local).days / 7) | round(0, 1) + 1}}

You should know that the entity_id option was eliminated from the Template Sensor integration a long time ago. Unless you are using an extremely old version of Home Assistant, you can remove it from the sensor’s configuration.

Thank you very much for giving me an hand, and sorry for the late reply. After re-installing my HA due to another issue, I finally was able to test it, and it is working almost perfectly. I just don’t know why it gives “week 13” as a result, because, since the Veg start date is 2022-06-05, it was supposed to still being week 12. Week 13 should start at the 28th August. Any idea why this is happening?
Thank you very much!

Here is my yaml for sensor.planned_veg_start_date:

      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() }}

This also works, thanks, but the same issue pervails, it gives “week 13” and it should give “week 12” until the 28th August. Any ideas?

And thank you for the hint about the deprecated code, this is one of the reasons I’m trying to renovate it :slight_smile:

Here is my yaml for sensor.planned_veg_start_date:

      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() }}

The formula you posted adds one week to the computed result. I included it in my example because that’s what you had. However, why are you adding an extra week to the calculation?

| int(0) / 604800 + 1)  | round(0, default=1)
                  ^^^
                  |||
               extra week 
1 Like

Thank you again for hanging on :slight_smile:
I dont even remember why the " + 1" is there, but I tried to take it out and it now shows Week “12”, and it was supposed to show week “13”.
So, in order to make it more concrete, since the Veg start date is 2022-06-05, the outputs should be as follows:

  • “Week 1” - from 2022-06-05 to 2022-06-11
  • “Week 2” - from 2022-06-12 to 2022-06-18
  • “Week 3” - from 2022-06-19 to 2022-06-25
  • “Week 4” - from 2022-06-26 to 2022-07-02
  • “Week 5” - from 2022-07-03 to 2022-07-09
  • “Week 6” - from 2022-07-10 to 2022-07-16
  • “Week 7” - from 2022-07-17 to 2022-07-23
  • “Week 8” - from 2022-07-24 to 2022-07-30
  • “Week 9” - from 2022-08-31 to 2022-08-06
  • “Week 10” - from 2022-08-07 to 2022-08-13
  • “Week 11” - from 2022-08-14 to 2022-08-20
  • “Week 12” - from 2022-08-21 to 2022-08-27
  • “Week 13” - from 2022-08-28 to 2022-09-03

If I use the code with the “+ 1” I still get the “week 13” - which is correct at the moment, but started earlier than expected (at least in 25th August, instead of 28th August). What could be causing this issue?

My guess is probably due to the way fractional weeks are rounded.

I suggest you use round(0, 'ceil', 1) to force the round filter to always round up.

sensor:
  current_week_of_grow:
    friendly_name: "Current Week of Grow"
    icon_template: mdi:calendar
    value_template: >
      {{ ((now() - states('sensor.planned_veg_start_date') | as_datetime | as_local).days / 7) | round(0, 'ceil', 1) }}
1 Like

This is the solution, I tried it out and it is giving me the “13” as supposed. I tested the Veg date and now it works all around.
Thank you so much for giving me an hand!

1 Like