Service to create event with Caldav

I wanted to achieve the same and created a solution that is working. Although I agree this should be implemented in the CalDav integration. For now one can use the following solution:

  1. Create an entry in your configuration.yaml
    rest_command: !include rest_commands.yaml
  2. Create the rest_commands.yaml file in the same directory were your configuration.yaml file is and paste the following commands
create_calendar_event:
    url: https://nextcloud.domain.org/remote.php/dav/calendars/homeassistant/{{ calendar }}/{{ uid }}
    headers:
      authorization: !secret nextcloud_rest_credentials
    method: put
    content_type: "text/calendar"
    payload: |
      BEGIN:VCALENDAR
      VERSION:2.0
      CALSCALE:GREGORIAN
      BEGIN:VEVENT
      UID:{{ uid }}
      SUMMARY:{{ summary }}
      DTSTART{{ whole_day }}:{{ dtstart }}
      DTEND{{ whole_day }}:{{ dtend }}
      END:VEVENT
      END:VCALENDAR

create_calendar_event_duration:
    url: https://nextcloud.domain.org/remote.php/dav/calendars/homeassistant/{{ calendar }}/{{ uid }}
    headers:
      authorization: !secret nextcloud_rest_credentials
    method: put
    content_type: "text/calendar"
    payload: |
      BEGIN:VCALENDAR
      VERSION:2.0
      CALSCALE:GREGORIAN
      BEGIN:VEVENT
      UID:{{ uid }}
      SUMMARY:{{ summary }}
      DTSTART:{{ dtstart }}
      DURATION:{{ duration }}
      END:VEVENT
      END:VCALENDAR
  1. Add the following script
alias: Create REST Calendar Event
sequence:
  - variables:
      whole_day: "{{ ';VALUE=DATE' if whole_day else '' }}"
      dtstart: |-
        {%- if whole_day -%}
          {{- startdate | as_timestamp | timestamp_custom("%Y%m%d") -}}
        {%- else -%}
          {{- startdate | as_timestamp | timestamp_custom("%Y%m%d") + "T" +
              startdate | as_timestamp | timestamp_custom("%H%M%S") -}}
        {%- endif -%}
      dtend: |-
        {%- if whole_day -%}
          {{- enddate | as_timestamp | timestamp_custom("%Y%m%d") -}}
        {%- else -%}
          {{- enddate | as_timestamp | timestamp_custom("%Y%m%d") + "T" +
              enddate | as_timestamp | timestamp_custom("%H%M%S") -}}
        {%- endif -%}
  - action: rest_command.create_calendar_event
    data:
      calendar: "{{ calendar_name }}"
      uid: "{{ now() | as_timestamp }}"
      summary: "{{ event_name }}"
      dtstart: "{{ dtstart }}"
      dtend: "{{ dtend }}"
      whole_day: "{{ whole_day }}"
    response_variable: rest_response
  - stop: ""
    response_variable: rest_response
fields:
  calendar_name:
    selector:
      text: null
    name: calendar_name
    required: true
  event_name:
    selector:
      text: null
    name: event name
    required: true
  startdate:
    selector:
      datetime: {}
    name: startdate
    required: true
  enddate:
    selector:
      datetime: {}
    name: enddate
    required: true
  whole_day:
    selector:
      boolean: {}
    required: true
    default: false
    name: whole day
description: ""
icon: mdi:calendar-plus

Now you can use the script to create calendar events using home assistant default data types. Feel free to adjust the script or commands to your likings.
I also added a REST command, which works with a start date and a duration. This is currently not used, as I prefer using start and end dates as input. If you want to use a duration you need to adjust the script accordingly. ISO_8601 notation for timespans must be used for the duration parameter.

4 Likes