Local Calendar: Is an Event Active? (Condition)

Yes, but it generates events at the start and end of this period. There is no way to check if it is “active” in between these dates, unless you create a triggered template binary sensor.

If you refrain from using concurrent/overlapping events you can use the state of the calendar entity for part of what @Corneloues has described.

From all the questions that have arisen since Local Calendar’s introduction I would like to see a Calendar condition too. But it would need to behave differently depending on the trigger type. For Calendar triggers, you need a condition that references the triggering calendar event. For other triggers it would need to to reference the state of the calendar entity.

1 Like

I too would like to be able to test whether a local calendar event is currently active.

In my case I have a set of kill switches that are activated by a calendar event and sometimes another event triggers an automation that should not change the state of those kill switches while the calendar event is active.
Currently I use an extra binary_sensor that is set/unset with the calendar start/end events, but it feels clumsy doing it this way as it would be better to just test whether the calendar event is active.

3 Likes

No need for convoluted solutions – the new local calendars already have the state ‘on’ (there is an active event) or ‘off’ (there is no active event). I think this is also true of integrated calendars but have not verified this myself.

That’s for the whole calendar. So there is one very big caveat:

To ensure I’m understanding this correctly, if I have an all day event it won’t pick up an event that starts and ends while the all day event is ‘on’?

You need to clarify what you mean by “it”…

I meant that the event starting or ending wouldn’t trigger automations, but after reading this thread I stumbled onto a thread and docs that mentioned using trigger.calendar_event which seems to be doing what I want it to do even if I have multiple overlapping events.

This could be really handy. One can share a calendar with other family members and everybody could note typed events. “No school day”, “Local Holliday”, “Vacations”, … even “Carnival” or “Doctor”. If you could check as condition if an specific “type” of event is running then you can stop or make things happen. Been more specific with an example, I control the heating on each room individually, on week days the rooms are turned off during school hours but if the kids are going to stay home I have to activate a switch to avoid this. If I could just set an event for that day I could check that as condition for stopping the action from been executed.
The local calendar, when its status is on has attributes that can tell the latest occurred event, but if several events are happening only one is shown. It would be nice if all active events are shown as a comma separated list or an array of objects in an attribute.

I think from the above, the answer is sadly still no, but am I right in saying that the local calendar can’t be used as a condition? I setup holiday days (kids off school, Xmas, known vacations etc) as a sensor in yaml, but it’s painful to add and update. However, many many automations use this as a condition because naturally most run when it’s not a holiday day and vice versa.

I’d love to have a GUI option where I can just click holiday days on or off - is there another integration/addon that can do that (or do I need to go the Google calendar route)?

The current way to get something like this is to use event triggers that match a specific event name, and a template binary sensor to hold the on state.

This is how I’ve solved this problem:

template:
  - trigger:
    - platform: calendar
      event: start
      entity_id: calendar.example_calendar
    - platform: calendar
      event: end 
      entity_id: calendar.example_calendar
    binary_sensor:
      - name: "Holiday Test"
        state: '{{ "Holiday" in trigger.calendar_event.summary and trigger.event == "start" }}'

This creates a binary sensor binary_sensor.holiday_test which will be on between the start and the end of a calendar event which contains the word “Holiday” in the event title.

After the event the state will be off.

3 Likes

Thanks for posting this. This is exactly what I’m looking for. But I’m unclear on how to set this up. Is this an automation? Does the trigger change the binary sensor? Do I create this as a helper->template->binary sensor?

No, this is a trigger-based Template binary sensor.

The state of the sensor will only update when the trigger fires.

No, template sensors using advanced features like triggers must be set up in your YAML configuration.

1 Like

Thank you for the quick reply. Will give it a go!

This worked a charm. Thank you @quaec and @Didgeridrew.

One thing I noticed this morning. An all-day event didn’t switch to off. I assumed the end time would be 12am.

Edit:
Looks like the same thing happened for another all-day event. This time, the binary sensor didn’t turn on.

@quaec or @Didgeridrew wondering if you’re aware of an update that needs to be made to the template to account for all-day events?

Is it expected that this template will work with all-day events?

Thanks!

My answer was not an endorsement of Quaec’s sensor configuration…

It sounds like you are using multiple back-to-back all-day events instead of one multi-day event. That could be part of the problem. There will likely be cases where the template presented by Quaec will not work, especially in calendars with concurrent or overlapping events.

Responding services were added in 2023.7 in order to help solve those limitations.

The following is a basic version of such a binary sensor. It fires one minute after midnight every night, gets all the events from a single calendar for the day, and checks if they have “Holiday” in their title/summary.

template:
  - trigger:
      - platform: time
        at: "00:01:00"
    action:
      - service: calendar.get_events
        data:
          start_date_time: "{{ today_at() }}"
          end_date_time: "{{ today_at('23:59:00') }}"
        target:
          entity_id: calendar.YOUR_CALENDAR
        response_variable: agenda
    binary_sensor:
      - name: Is there a Holiday today?
        state: |-
          {% set search_term = "Holiday" %}
          {{ agenda['calendar.YOUR_CALENDAR'].events 
          | selectattr('summary', 'search', search_term) | list | count > 0 }}
2 Likes

Thanks @Didgeridrew - didn’t mean to imply that you were endorsing any solution. Just tagged you since you seem like a person that knows what they’re doing!

Will work with this and see where it goes!

Thank you @quaec and users who have helped, this is very useful indeed!