Create an whole day event in calendar via automation

Hey guys,

I´ve created an automation which creates an calendar event when I´m entering a special zone.
I call this service:

service: calendar.create_event
target:
  entity_id: calendar.myCalendar
data:
  summary: Work at Nordhorn
  location: Nordhorn
  start_date_time: "{{ now().strftime('%Y-%m-%d 9:00') }}"
  end_date_time: "{{ now().strftime('%Y-%m-%d 14:00') }}"

This works great, but I want to create an whole day event. I´ve tested start_date_time and end_date_time without time, I`ve tested

start_date: "{{ now().strftime('%Y-%m-%d') }}"

and always run into an error.

Does anyone know, how the correct syntax is for an whole day event?

Thanks.

To create an All Day event today, set start_date to today’s date and end_date to tomorrow’s date. In other words, there’s no time specified, just the date, and the end date must be the next day.

service: calendar.create_event
target:
  entity_id: calendar.myCalendar
data:
  summary: Work at Nordhorn
  location: Nordhorn
  start_date: "{{ now().strftime('%Y-%m-%d') }}"
  end_date: "{{ (now() + timedelta(days=1)).strftime('%Y-%m-%d') }}"
1 Like

Great! Thank you! That´s what I`m looking for.

1 Like

Hi
I am trying to do something very similar - a button to add a calendar event to a local calendar. Your post was helpful in getting so far, but I am struggling with data types in the template.

I have created an input.select drop down helper for the summary field and an input. date for the start date,
I am passing these from a button to a script as fields (code below).
Then I am trying to calculate the end date (start day plus 1) ready to create the calendar event.

Button code:

type: custom:button-card
name: Add reminder
label: null
show_label: true
tap_action:
  action: call-service
  service: script.001_add_event_to_reminders_calendar
  service_data:
    item: input_select.cal_item
    remind_start: input_datetime.calevent
styles:
  card:
    - height: 70px
    - font-size: 20px
    - background: rgb(20,140,200)
  name:
    - color: gainsboro
  label:
    - color: gainsboro
    - font-size: 18px
  icon:
    - color: gainsboro

Script code:

alias: "==001 Add event to reminders calendar"
description: ""
fields:
  item:
    selector:
      text: {}
    name: item
    description: Event item summary
  remind_start:
    selector:
      date: {}
    name: remind start
    description: Start date of reminder
sequence:
  - action: calendar.create_event
    metadata: {}
    data:
      summary: "{{ item }}"
      start_date_time: "{{ remind_start }}"
      end_date_time: >
        "{% set y = (strptime(states('remind_start'), '%Y-%m-%d','') + timedelta(days=1)) %}"   
        "{{y.strftime('%Y-%m-%d')}}"
    target:
      entity_id: calendar.reminders

I have looked at many forum posts, tried many combinations of template syntax and data typing and now feel too close to the problem, so it is probably something simple (I hope) that I have missed. If you can make a suggestion it would be most helpful please.

Hope this makes sense. With thanks

Try this version.

alias: "==001 Add event to reminders calendar"
description: ""
fields:
  item:
    selector:
      text: {}
    name: item
    description: Event item summary
  remind_start:
    selector:
      date: {}
    name: remind start
    description: Start date of reminder
sequence:
  - action: calendar.create_event
    metadata: {}
    data:
      summary: "{{ states(item) }}"
      start_date_time: "{{ states(remind_start)[:10] }}"
      end_date_time: >
         {{ (states(remind_start) | as_datetime + timedelta(days=1)).date() | string }}
    target:
      entity_id: calendar.reminders

EDIT

Correction. Fixed template for end_date_time. Replaced %} with }}

@123 Thanks for the prompt reply.

When I paste that into the script, it will not let me save it, giving the error shown below:

Similar to some of the previous errors I have seen. I think I understand your logic on line 21 to convert the variable to and then back from date type.

My primitive understanding suggests an issue with the first item of data under sequence, but I may well be wrong. (I tried without states and got the same) (I guess it could also be something missing elsewhere that makes the first item wrong - hence my maze-like journey so far.)

Does the [:10] slice the front/back from the string to give a date (without time)? The helper was set up to gather just a date so do I need this?

I tried adding a dummy 3rd field/parameter called remind_end, in case it was expecting it, but it made no difference.

I made a small mistake in the template for the end_date_time option.

While converting from your template, I forgot to change the final %} to }} so that’s what caused the error message.

I fixed my example posted above.

Thanks - that is great - and apologies I probably should have spotted that.

It has now added an event to the calendar (hooray!), though it is not showing in the same way as one entered manually on the sidebar Calendar page.

image

The manual one was entered by ticking ‘all day’ and I get the solid block colour for the day 6th Feb, whereas by entering start and end times there is only a timed entry style - 5th Feb.

Not the end of the world, but do you know if there is a way to enter an event as all day? (I thought the calendar might work that out automatically) Cannot see it on the calendar doc page, but may have missed it.

Incidental question - Local calendar has allocated white text on yellow (ugh!) for the calendar - do you know where this is configured? (I cannot find how to get to the properties of the calendar itself - no 3 dot menu nor any setting in the entity as far as I can see)
Thanks again.

According to the documentation, you use the start_date and end_date options to specify whole day events (not the start_date_time and end_date_time options).

start_date
The date the whole day event should start.

Yes - the helper is set to input date (only)

I also added a [:10} to the end result of calculating the end date in case it had added a time by default, but no change.

sequence:
  - action: calendar.create_event
    metadata: {}
    data:
      summary: "{{ states(item) }}"
      start_date_time: "{{ states(remind_start)[:10] }}"
      end_date_time: >
         {% set x = (states(remind_start) | as_datetime + timedelta(days=1)).date() | string %}
         {{ x[:10] }}
    target:
      entity_id: calendar.reminders

is there a way to see the value of the variables in the script during execution - like a trace? to see if a time has been added somewhere

The YAML code you just posted is still using start_date_time and end_date_time.

In addition, the modification you made to the end_date_time template will result in an invalid date string.

You can test templates in the Template Editor.

Developer Tools → Template

Duh!

Thanks for pointing out the obvious. (I said I was too close to this :upside_down_face:)

This now works as an all day event - many thanks again for your help - and patience!

Here is the working script code in case anyone comes across this …

alias: "==001 Add event to reminders calendar"
description: ""
fields:
  item:
    selector:
      text: {}
    name: item
    description: Event item summary
  remind_start:
    selector:
      date: {}
    name: remind start
    description: Start date of reminder
sequence:
  - action: calendar.create_event
    metadata: {}
    data:
      summary: "{{ states(item) }}"
      start_date: "{{ states(remind_start) }}"
      end_date: >
         {{ (states(remind_start) | as_datetime + timedelta(days=1)).date() | string }}       
    target:
      entity_id: calendar.reminders

Just need to change that colour now …

Is there a particular reason for making end_date’s template longer than what was suggested?

No - it was out of my attempt to strip out the time - I will change it back to your more elegant version. (edit: done) Thanks again

Meanwhile - apparently calendar colours are allocated by HA and cannot be changed AFAIK. There are a few posts about this
https://github.com/home-assistant/architecture/discussions/883
https://community.home-assistant.io/t/wth-cant-i-assign-colors-to-calendars/802220/6
https://github.com/home-assistant/frontend/discussions/11262

Some discussion around whether it involves assigning colours to all entities or just some kind of calendar attribute. I will try and use some kind of card to display what I need and hope the theme or card-mod maybe will help out.