Calendar automation with offset before calendar event does not work (but one with offset afterwards does)

Hello Community!

Unfortunately I do not know why my automation is not triggered. I have two automations which automatically switch my home office socket on and off. Unfortunately, only the automation that has the offset set to 15 or 30 minutes after the end of the calendar event triggers.

The actually more important, i.e. switching on with the offset 30 minutes before the event starts, is never triggered. I have checked the states of the calendar and in my opinion they should actually trigger.

The following is the automation that is not triggered:

alias: "Kalender | Arbeitsbeginn in 30 Minuten "
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:31:0"
    entity_id: calendar.arbeit
condition: []
action:
  - type: turn_on
    device_id: 111c34e0b08c29aa6adb9974b6930c54
    entity_id: switch.childrens_room_socket
    domain: switch
mode: single

Now the automation, which is triggered and works:

alias: Kalender | Arbeitsende seit 15 bzw. 30 Minuten
description: ""
trigger:
  - platform: calendar
    event: end
    offset: "0:15:0"
    entity_id: calendar.arbeit
  - platform: calendar
    event: end
    offset: "0:30:0"
    entity_id: calendar.arbeit
condition:
  - type: is_power
    condition: device
    device_id: 111c34e0b08c29aa6adb9974b6930c54
    entity_id: sensor.childrens_room_socket_power
    domain: sensor
    below: 30
action:
  - type: turn_off
    device_id: 111c34e0b08c29aa6adb9974b6930c54
    entity_id: switch.childrens_room_socket
    domain: switch
mode: single

BIG thank you in advance!

I’ve had the same problem. The documentation for the calendar trigger isn’t great.

I could only get the negative offset to work in combination with event: end, which works for me, because I only use this for all day events. If you don’t know the length of the event, this isn’t helpful though.

Maybe this is a bug.

I just wanted to add that I’m struggling with the same problem/bug.

Thank you @thedoginthewok for mentioning your event: end approach; this works for me because I’m also just using all day events for now.

I had the same issue. You have to use this format -hh:mm:ss and not -h:mm:ss. You are missing a leading and tralling 0 in your example (try “-00:31:00”)

However, the UI uses this format -h:m:s… so… the bug might be there.

1 Like

Hmmm tried it. Still not working :confused:

Opened negative offset from calender trigger isn’t working · Issue #84255 · home-assistant/core · GitHub

The problem still persists, only using full day events, though.

Calendar log if the event has a fix start and end time:

2023-03-17 19:15:45.074 INFO (MainThread) [homeassistant.setup] Setting up calendar
2023-03-17 19:15:45.077 INFO (MainThread) [homeassistant.setup] Setup of domain calendar took 0.0 seconds
2023-03-17 19:15:45.233 INFO (MainThread) [homeassistant.components.calendar] Setting up calendar.caldav
2023-03-17 19:15:45.696 INFO (MainThread) [homeassistant.components.calendar] Setting up calendar.waste_collection_schedule
2023-03-17 19:15:59.517 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 18:20:59.517065+00:00, 2023-03-17 18:36:00.517065+00:00 (offset=-1 day, 23:55:00)
2023-03-17 19:15:59.517 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 18:20:59.517816+00:00, 2023-03-17 18:36:00.517816+00:00 (offset=-1 day, 23:55:00)
2023-03-17 19:15:59.520 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 18:10:59.519928+00:00, 2023-03-17 18:26:00.519928+00:00 (offset=0:05:00)
2023-03-17 19:15:59.520 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 18:10:59.520504+00:00, 2023-03-17 18:26:00.520504+00:00 (offset=0:05:00)
2023-03-17 19:15:59.864 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list [(datetime.datetime(2023, 3, 17, 19, 40, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), CalendarEvent(start=datetime.datetime(2023, 3, 17, 19, 30, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), end=datetime.datetime(2023, 3, 17, 19, 45, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None))]
2023-03-17 19:15:59.865 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Scheduled alarm for 2023-03-17 19:40:00+01:00
2023-03-17 19:15:59.930 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list [(datetime.datetime(2023, 3, 17, 19, 25, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), CalendarEvent(start=datetime.datetime(2023, 3, 17, 19, 30, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), end=datetime.datetime(2023, 3, 17, 19, 45, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None))]
2023-03-17 19:15:59.930 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Scheduled alarm for 2023-03-17 19:25:00+01:00
2023-03-17 19:15:59.942 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list []
2023-03-17 19:15:59.983 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list []

Automation for it, all do trigger:

alias: Calendar Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:5:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: end
    offset: "-0:5:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: start
    offset: "0:5:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: end
    offset: "0:5:0"
    entity_id: calendar.test_calendar
condition:
  - condition: state
    entity_id: calendar.test_calendar
    attribute: message
    state: Test1
action:
  - device_id: xxx
    domain: mobile_app
    type: notify
    message: Test
    title: Test
mode: queued
max: 10

Calendar log if the event is all day (I added 3 all day event at 16/03/23, 17/03/23 and 18/03/23 to test for triggers:

2023-03-17 21:26:48.674 INFO (MainThread) [homeassistant.components.calendar] Setting up calendar.caldav
2023-03-17 21:26:48.825 INFO (MainThread) [homeassistant.components.calendar] Setting up calendar.waste_collection_schedule
2023-03-17 21:27:03.744 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 22:42:03.744203+00:00, 2023-03-17 22:57:04.744203+00:00 (offset=-1 day, 21:45:00)
2023-03-17 21:27:03.744 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-16 22:37:03.744887+00:00, 2023-03-16 22:52:04.744887+00:00 (offset=21:50:00)
2023-03-17 21:27:03.745 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-17 22:32:03.745453+00:00, 2023-03-17 22:47:04.745453+00:00 (offset=-1 day, 21:55:00)
2023-03-17 21:27:03.746 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Fetching events between 2023-03-16 22:27:03.746004+00:00, 2023-03-16 22:42:04.746004+00:00 (offset=22:00:00)
2023-03-17 21:27:04.118 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list []
2023-03-17 21:27:04.477 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list []
2023-03-17 21:27:04.483 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list [(datetime.datetime(2023, 3, 17, 22, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), CalendarEvent(start=datetime.date(2023, 3, 16), end=datetime.date(2023, 3, 17), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None))]
2023-03-17 21:27:04.484 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Scheduled alarm for 2023-03-17 22:00:00+01:00
2023-03-17 21:27:04.500 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Populated event list [(datetime.datetime(2023, 3, 17, 21, 55, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')), CalendarEvent(start=datetime.date(2023, 3, 17), end=datetime.date(2023, 3, 18), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None))]
2023-03-17 21:27:04.501 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Scheduled alarm for 2023-03-17 21:55:00+01:00

trigger:
2023-03-17 21:55:00.005 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Calendar event @ 2023-03-17 20:55:00+00:00
2023-03-17 21:55:00.006 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Event: CalendarEvent(start=datetime.date(2023, 3, 17), end=datetime.date(2023, 3, 18), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None)
2023-03-17 21:55:00.018 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Calendar event @ 2023-03-17 20:55:00+00:00
2023-03-17 21:55:00.019 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Event: CalendarEvent(start=datetime.date(2023, 3, 17), end=datetime.date(2023, 3, 18), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None)


2023-03-17 22:00:00.012 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Calendar event @ 2023-03-17 21:00:00+00:00
2023-03-17 22:00:00.013 DEBUG (MainThread) [homeassistant.components.calendar.trigger] Event: CalendarEvent(start=datetime.date(2023, 3, 16), end=datetime.date(2023, 3, 17), summary='Test1', description=None, location=None, uid=None, recurrence_id=None, rrule=None)

Automation used to trigger at 21:45, 21:50, 21:55 and 22:00 UTC+01:

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-2:15:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: start
    offset: "21:50:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: end
    offset: "-2:5:0"
    entity_id: calendar.test_calendar
  - platform: calendar
    event: end
    offset: "22:0:0"
    entity_id: calendar.test_calendar
condition:
  - condition: state
    entity_id: calendar.test_calendar
    attribute: message
    state: Test1
action:
  - device_id: xxxxxxx
    domain: mobile_app
    type: notify
    message: Test
    title: Test
mode: queued
max: 10

As the logs stated, only 21:55 and 22:00 worked.