Automation condition for Multiple all day events in Local Calendar?

Only one of the datetimes will be used for any given run of the action, based on the trigger ID. The “on” trigger fires 6 hours before the all-day calendar events OP is using, so the event query can’t look at events on the calendar at the current time, it needs to look 6 hours in the future. The additional minute is to avoid an issue that occasionally will happen with calendar.get_events and all-day events around midnight.

The “on” trigger should retrieve calendar events that are active during the period 00:01-00:02, that will pick up any all-day events. Similarly the “off” trigger should retrieve calendar events that are active during the period 23:58-23:59 (Note: I have adjusted the post above, I should have used -7 min for the timedelta to avoid midnight)

Yes, I see. That’s what I thought it was doing. Just wanted to double check.

But just because an event is listed from get_events during the “off” trigger (e.g. the brown bin), how do you know that this particular brown bin event is ending (and therefore know to set the brown bin binary sensor to “off”)?

In the context of the original post all of these are single-day, all-day events, so they can’t be starting at 23:58 … anything that had been active at that time of night will have ended.

If the context were different, for example if you have the same bin color days back-to-back or the calendar included events that were not all-day, you would need to alter the method and the time span being queried.

Yes okay I see. Anyway, same color bin days back to back is probably unlikely in most practical cases for anyone.

I was thinking more of a situation where this calendar was not just used for bins, but say it was a general “home” calendar. Let’s say for example that just like the OP you indeed had a single all-day event called “brown bin”. But then you added “Driving Lesson” from 10am until 11am.

In that case, wouldn’t the brown bin binary sensor turn off when “Driving Lesson” ended, event though the brown bin event is still running for the rest of that day?

If you agree (if i understand properly), do you know another way to make sure the binary sensor is only on when the corresponding event is truly running? It would be nice to use this method for a calendar that has lots of other event types in it (rather than limiting it to only all-day events.

Thanks for your insight with this.

Yes, in a different context the sensor needs to be configured differently.

One of the options added to trigger-based template sensor since this thread was started is the ability to include a condition block. We now have the full gamut of conditional logic available to limit when sensors will be updated.

In this case, I think a single condition should be able bring the context from the original back into the proposed situation… Require that the trigger be an all-day event in order to update the sensors’ states.

condition:
  - "{{ trigger.calendar_event.all_day }}"

Additional conditions or more complicated logic could be used for each sensor’s state template, for example selecting events by the content of their summary, then comparing their start and end times to the current time (or some offset from the current time)… but that doesn’t seem necessary for this case.

1 Like

Lovely, thank you for bringing this option to my attention, and for your time and advice.

Thank you for updating the template sensor solution, I was not sure how to use calendar.get_events in the template sensor.

I tried to test the template sensor with what you suggested and changed the offset to trigger for testing and it was successful in turning on the bins for collection on the day in question.

However they failed to turn off 5 minutes after midnight on the day of the all day events?

- trigger:
    - platform: calendar
      event: start
      offset: '-1:40:00'
      entity_id: calendar.calendar
      id: 'on'
    - platform: calendar
      event: end
      offset: '00:05:00'
      entity_id: calendar.calendar
      id: 'off'
  action:
    - action: calendar.get_events
      target:
        entity_id: calendar.calendar
      data:
        start_date_time: |
          {% if trigger.id == 'on' %}
            {{ now() + timedelta(hours=1, minutes=40) }}
          {% else %}
            {{ now() - timedelta(minutes=6) }}
          {% endif %}
        duration:
          minutes: 1  
      response_variable: agenda
    - variables:
        events: "{{ agenda['calendar.calendar']['events'] }}"
        brown_bool: "{{ events | selectattr('summary', 'search', 'Brown bin collection') | list | count > 0 }}"
        blue_bool: "{{ events | selectattr('summary', 'search', 'Blue bin collection') | list | count > 0 }}"
        black_bool: "{{ events | selectattr('summary', 'search', 'Black bin collection') | list | count > 0 }}"
  binary_sensor:
    - name: Brown Bin
      state: >
        {% set current = this.state | default('off', 1) %}
        {{ trigger.id if brown_bool else current }}
    - name: Blue Bin
      state: >
        {% set current = this.state | default('off', 1) %}
        {{ trigger.id if blue_bool else current }}
    - name: Black Bin
      state: >
        {% set current = this.state | default('off', 1) %}
        {{ trigger.id if black_bool else current }}


from what i see and understand it should have turned off 5 minutes after midnight on the 31/01/25 but it didn’t. I can’t see why?

Without seeing the calendar it’s hard to diagnose this cause… Note that I updated the automation yesterday to switch the 6 minute timedelta() in the start_time to 7 minutes. This could be the root of what you are seeing, but that’s a guess. There’s an annoying thing that happens at midnight with all-day events where the events from both days are retrieved by the calendar.get_events action.

Thank you for your reply.

After checking this is correct and expected behaviour offset for event: end of + 5 minutes turns it off 5 minutes after the all day events has finished so this is correct behaviour and always has been. So the bins stay on for 24 hrs pus any offset either side of the all day event.

The bins turned off correctly at 00:05:00 01/02/25.

Thank you again for your knowledge and perseverance I now have a fully working solution for the calendar template sensor.

:smiley: