Is there a NR equivalent of the HA Automation 'calendar trigger'?

I’ve found this thread very useful in my attempts to trigger a node-RED flow from calendars. I was using the ical sensor method but this trigger template might hold more promise now that calendars are more tightly integrated with the core.

Here is my current (experimental) template code, which defines a sensor whose state is the ‘name’ of the next calendar event, and which has attributes defining the calendar entry. The ‘start’ attribute indicates whether it’s triggered by the start - offset of the event (true) or the end of the event (false).

The id and idx attributes seem to come through as 0 for start and 1 for end. Probably some redundancy here.

template:
  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.mycal
        offset: "-00:05:00"
      - platform: calendar
        event: end
        entity_id: calendar.mycal
    sensor:
      - name: next calendar event
        state: "{{ trigger.calendar_event.summary }}"
        attributes:
          start: "{{ trigger.event == 'start' }}"
          start_time: "{{ trigger.calendar_event.start }}"
          end_time: "{{ trigger.calendar_event.end }}"
          description: "{{ trigger.calendar_event.description }}"
          location: "{{ trigger.calendar_event.location }}"
          summary: "{{ trigger.calendar_event.summary }} "
          all_day: "{{ trigger.calendar_event.all_day }}"
          id: "{{ trigger.id }}"
          idx: "{{ trigger.idx }}"

image

I use node-RED for all but the most trivial automations and this state change is easily picked up with a state_changed node. The payload is the ‘name’ of the calendar entry and the data object contains the listed attributes.

5 Likes

Thanks for this great example. I tried to follow the instructions, but it is not working, the newly created template sensor is not receiving any input. What am I missing?

The calendar from the google_calendars.yaml:

The calendar event ‘Test afval container’ is properly appearing in the calendar.afval_ophaal_schema

My template.yaml file:

- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.afval_ophaal_schema
    - platform: calendar
      event: end
      entity_id: calendar.afval_ophaal_schema
  sensor:
    - name: next calendar event
      state: "{{ trigger.calendar_event.summary }}"
      attributes:
        start: "{{ trigger.event == 'start' }}"
        start_time: "{{ trigger.calendar_event.start }}"
        end_time: "{{ trigger.calendar_event.end }}"
        description: "{{ trigger.calendar_event.description }}"
        location: "{{ trigger.calendar_event.location }}"
        summary: "{{ trigger.calendar_event.summary }} "
        all_day: "{{ trigger.calendar_event.all_day }}"
        id: "{{ trigger.id }}"
        idx: "{{ trigger.idx }}"

The new sensor is correctly created, but no trigger data is loaded after the ‘start time’ has passed:

@riko Something to watch out for is a foible of either Google calendars or the integration (I’m not sure which exactly) whereby a calendar event needs to be assigned a new identity in order to trigger.

If you move an event to a new date and/or time it seems that it is not given a new ID. Unfortunate really, as this is what I did in testing and you may be doing as well. Instead, I found that I needed to duplicate an event and give a fresh date/time or create a new event from scratch. Either case results in a new event ID which will then trigger. Of course, you can’t re-use that event and you need to duplicate again for the next test.

Thanks, I’ll give it a new try with newly created calendar events and keep you posted!

Again not working, it seems that there is something wrong in my code. Do you recognize this?

Check the history of sensor.next_calendar_event over the last few days. What does it show?

The history of the sensor is ‘Unknown’ for the full period

I think that might suggest that the state of the sensor is not changing rather than the trigger not detecting a change in state. So your automation is probably OK and my advice would be to go over each step of the calendar API setup again in case it’s an authentication issue with your Google account. Maybe the callback URI is wrong.

This solution from @CentralCommand looks sweet. I imported and am using it without a hitch. I like that I can work things out in Node-RED and javascript. Although I guess it means handling a lot of timing issues, vs using the template code from @hunterdrayman. More experiments will tell. But thanks both for sharing, it will save me lots of time.