Service to create event with Caldav

I want to create an event with Caldav.

Like: https://www.home-assistant.io/integrations/calendar.google/#service-googleadd_event

Agreed! This would be cool to add and delete To-Dos aswell.

1 Like

Although I have never tried it, apparently itā€™s just a matter of performing an HTTP PUT with content in vCalendar format.

2 Likes

I have already read something like that. But it would be quite good to have this as a service in the Homeassistant.

Unfortunately I am not a good programmer. But could help with testing.

I agree it would be ideal to have a service to do it but you should know that most Feature Requests are never implemented. So if you need something now then HTTP PUT is a possibility.

Then I try to implement HTTP Put via the REST

Hi, die you succeed with creating events?

After the calendar tab appeared in HA, it became very convenient to store long-term information there. Such as the consumption of water and electricity, the distance traveled, and the consumed fuel, triggering of the sensors of the security system, and so on. But unfortunately, adding events to the calendar is only possible for the Google calendar. But I really want this information to be stored in the local calendar (I use the CalDav Synology). On this I join the request.

I have exactly the same requirement.
Unfortunately I have not made it work with REST

I have the same requirement also.

In my old Home Automation system (jeedom) I use a php function to do that, I have some smal progrmaing skills. does sombady know where the dependency are in HA so I can check if they integrate the feature and make a python script to add an event

So currently, adding events to a caldav calendar is not available?

you could use node red if that would help you :slight_smile: ?

Anyone knows if/how I can use this ā€œGitHub - python-caldav/caldavā€ with appdaemon to create calendar events?

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.

2 Likes

Thanks for sharing !
This is what i needed however i canĀ“t make it work.
I have a 401 error (unauthorized) in nextcloud logsā€¦
How do you setup authentication in nextcloud ?
I tried adding Oauth2 client and use the secret from here but I iā€™m a bit clueless here and i may not have it configured rightā€¦

Edit : I tried using app password in Personnal settings but no more luck, still 401 error

Edit2 : I tried using Generate HTTP Basic Auth Header
to put in secrets and now it throw a 404 error

Final edit : 404 error was because calendar name was wrong, url for it is personal but itā€™s named ā€˜Personnelā€™ in french localization, it works now !
Btw i needed to change url to add ā€œ.icsā€ at the end like this :

url: https://zwergencloud.mywire.org/remote.php/dav/calendars/homeassistant/{{ calendar }}/{{ uid }}.ics

Thanks

I was playing around with the REST interface some more and learned how to update and delete CalDav events.
Unfortunately it seems too complicated and not right, to do this the same way as creating events, because for every update or deletion, one would need to query the calendar event to get the uid.
Therefore, it would be best to enhance the official Caldav Integration. The uid could then be stored right away when the events are queried.

I am going to have a look at it, but currently I do not have a lot of time for this.