Local Calendar: Is an Event Active? (Condition)

For automations, it would be great to be able to base a condition on whether an event is active or not.

This would mean being able to retrieve a calendar event by name and then to be able to query attributes such as, for example, event_start and event_end as date/time. Or state attributes such as event_started, event_running or event_ended.

For recurring events, it would be the “on the day” event in the series that would be tested.

Calendars generate events. Events do not have a state. They occur. So you get a start event, and an end event. If you want a state in between these times you would have to create a triggered template sensor - for each event you want to track.

The schedule helper does have a state (on during scheduled times, otherwise off). Perhaps that would suit your requirements better?

So if I wanted a recurring schedule from the 1st of December through to the 6th of January every year is that possible?
At the moment, I use two date/time helpers and a template sensor that returns true if the current date/time is between those two dates.
It works great, but I have to remember to manually update the helpers between the 7th Jan and the 30th November :wink:
So what I am looking for is a recurring schedule that can recur outside of a day a week.

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!