Time critical calendar sensor

Hello together,

I’ve had a working automation which remembered me to put out my trash cans. Therefore I had a “trash calendar” with all-day-events (i.e. start-time = 00:00h). If the actual time is 6 hours (21.600 seconds) before the start-time (so, if it’s 18:00h) I should get a telegram message. It worked in 2018, it did not now. I get the messages at 00:00h. Here is my sensor:

- platform: template
  sensors:
    tonnen_raus:
      friendly_name: "Mülltonnen raus"
      value_template: >
        {% if as_timestamp(states.calendar.muellabfuhr.attributes.start_time) - as_timestamp(now()) < 21600 and as_timestamp(states.calendar.muellabfuhr.attributes.end_time) > as_timestamp(now()) %}on{% else %}off{% endif %}

Anyone has any idea? :frowning:

Thanks in advance!

Add an entity_id to your sensor that changes frequently or update your sensor with an automation.

From the docs:

The template engine will attempt to work out what entities should trigger an update of the sensor. This can fail, for example if your template loops over the contents of a group. In this case you can use entity_id to provide a list of entity IDs that will cause the sensor to update or you can run the service homeassistant.update_entity to update the sensor at will.

Thank you, this solved the problem. If anyone has the same problem: I added an automation to manually update the entity at 18:00h.

- id: muelltonnencheck
  alias: "Muelltonnencheck"
  trigger:
    - platform: time
      at: '18:00:00'
  action:
    service: homeassistant.update_entity
    entity_id: sensor.tonnen_raus

Thanks