Template does not update on midnight

Hi all

I’m using the integration of my grabage collector to acquire dates of collection dates. These update correctly and display correctly. However, I try to use an template to convert it to how many days from now the collection of garbage is. I’m pretty new to templating and home assistant. I’m using the template posted here: Template Guide: Days away from calendar event

My configuration file contains the lines below in the sensors section:

  - platform: time_date
    display_options:
      - 'date'
  - platform: template
    sensors:
      grijze_bak:
        friendly_name: Grijze bak
        entity_id: sensor.date
        icon_template: 'mdi:delete-empty'
        value_template: >
          {% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {% set event = states('sensor.non_recyclable_waste_pickup') | as_timestamp %}
          {% set delta = ((event - midnight) // 86400) | int %}
          {% if delta == 0 %}
            Vandaag
          {% elif delta == 1 %}
            Morgen
          {% elif delta == 2 %}
            Overmorgen
          {% else %}
            Over {{ delta }} dagen
          {% endif %}

If I reboot home assistant it updates correctly. However, it does not update on midnight. I though it would update on midnight because I incorporte the time_date platform with sensor.date as entity_id within my template. I have 3 comparable templates directly after the these for the different types of garbage. All of these update on reboot but not midnight.

Can anyone help me find out what I’m doing wrong?

entity_id is deprecated, so just plop the date in the template.

  - platform: template
    sensors:
      grijze_bak:
        friendly_name: Grijze bak
        icon_template: 'mdi:delete-empty'
        value_template: >
          {% set dump = states('sensor.date') %}
          {% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {% set event = states('sensor.non_recyclable_waste_pickup') | as_timestamp %}
          {% set delta = ((event - midnight) // 86400) | int %}
          {% if delta == 0 %}
            Vandaag
          {% elif delta == 1 %}
            Morgen
          {% elif delta == 2 %}
            Overmorgen
          {% else %}
            Over {{ delta }} dagen
          {% endif %}

A qualified guess is that you are from the Netherlands and thus GMT+1 (+1 for DST).
You use as_timestamp and this uses UTC timezone meaning +0 hours.
That means it will change at 2 in the morning for you. (I assume).

You can probably solve this by removing one hour and another if it’s DST using a template to find out if it’s DST, but is it worth it?

I originally built the template to ignore timezones on purpose so people didn’t have to muck with that. That’s not the problem. It’s most likely due to the deprecation of entity_id, which is no longer used to update the template. That’s the component in that post that makes this update at midnight.

Thank you all. I updated the template with the suggestion above. Now we have to wait till tonight. I’ll let you know if it works.

Sorry for the late reply. It worked now. Thanks all for the help.