Template always sets the value on the next day at 12:00 pm and not at 00:00 when the new day starts

Hi there,
i have the following template to use an input_datetime field to calculate the days that have passed since the last change of the input_datetime field in relation to the current date. It works so far, however, the value is only increased at 12:00 pm, but I would like to have it updated immediately at 00:00 when the new day begins. Do you have any idea how I could solve this?

I got this code from another forum, but nobody could help me here.

Home Assistant is also correctly configured as far as time zones etc. are concerned.

sensor:
  - platform: template
    sensors:
      pflanzen_giessen:
        value_template: '{{ ((as_timestamp(now())-(states.input_datetime.pflanzen.attributes.timestamp)) | int /60/1440) | round(0) }}' 
        unit_of_measurement: 'Tage'
        entity_id: input_datetime.pflanzen,sensor.time

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

input_datetime:
  pflanzen:
    name: Pflanzen gießen
    has_date: true
    has_time: false

Use your date sensor to trigger an update by including it as an entity:

sensor:
  - platform: template
    sensors:
      pflanzen_giessen:
        value_template: '{{ ((as_timestamp(now())-(states.input_datetime.pflanzen.attributes.timestamp)) | int /60/1440) | round(0) }}' 
        unit_of_measurement: 'Tage'
        entity_id:
          - input_datetime.pflanzen
          - sensor.date

This will cause an update at 00:00 when the date changes.

It’s actually updated every minute. It’s only updated at 12pm because you’re using ‘round(0)’ which means the number will flip at the half day mark. At 11:59am your value is 0.49 which rounds to 0, then at noon it switches to 0.5 which rounds up to 1. This happens every day at the half value. This is why you shouldn’t use round, but it won’t matter if you take @tom_l’s advice and use sensor.date.

Thank you @tom_l @petro for your detailed explanation. I just tested it and it works great.

@petro Now that I’ve removed the “round 0” in the code, my sensor shows me a very long number (0.51804 … day). Is there a possibility to only show the first position, in my case currently “0 days”?

With the “round (0)” command he made me “1 days” again when I restarted hassio.

No, you don’t need to remove it by following @tom_l’s solution because the date is resolved at the beginning of the day.

Yes I did it after @tom_l solution but the sensor shows me “1” and not" 0" as requested.
I think this is the solution for that:

sensor:
  - platform: template
    sensors:
      pflanzen_giessen:
        value_template: '{{ ((as_timestamp(now())-(states.input_datetime.pflanzen.attributes.timestamp)) | int /60/1440) |int }}' 
        unit_of_measurement: 'Tage'
        entity_id:
          - input_datetime.pflanzen
          - sensor.date

That gives me the “0” in my sensor

Yeah that will work

After updating Home Assistant to version 0.115, he told me that I should replace endity_id to unique_id.
I did that, but now the sensor.date no longer works because it no longer counts automatically. Do I have to change anything else? Hope you can help me again @tom_l @petro

sensor:
  - platform: template
    sensors:
      pflanzen_giessen:
        value_template: '{{ ((as_timestamp(now())-(states.input_datetime.pflanzen.attributes.timestamp)) | int /60/1440) |int }}' 
        unit_of_measurement: 'Tage'
        unique_id: input_datetime.pflanzen,sensor.date

There are some solutions here:

Probably the simplest would be:

value_template: >
  {% set x = states('sensor.date') %}
  {{ ((as_timestamp(now())-(states.input_datetime.pflanzen.attributes.timestamp)) | int /60/1440) |int }}

Thank’s for the quick support @tom_l :slightly_smiling_face:

1 Like