Adding a calendar event "next Monday", "next Tuesday", etc

Hello,
I’m quite new to HA and currently trying to recreate my IFTTT automations, but I’m stuck on a function concerning calendar events.

In IFTTT I had a button that would create a calender event with the date “next monday”, a function I can’t get to work in HA. Is there a a simple way to do this or would it involvecalculating the current weekday and then the offset to “next monday”? If so I’d be totally out of my depth. :face_holding_back_tears:

Background: I don’t have a regular schedule for my home-office days and usually plan them only a week ahead. In IFTTT I had a button for every day of the week and on the weekend I’d click the buttons for say Monday and Wednesday. That would create an event on Monday and Tuesday morning, and this event would trigger certain things like turn up the heating etc.

Thank you for your help!

something like this to get that next day of the week?

{% set delta_days = 7 - now().timetuple().tm_wday + next_dow %}
{{ now() + timedelta(days = delta_days) }}

next_dow == the day of week you want. 0 based monday. so sunday == 6.

i don’t know what you want for the calendar, but you can use that in

  start_date_time: >
    {% set delta_days = 7 - now().timetuple().tm_wday + next_dow %}
    {{ now() + timedelta(days = delta_days) }}

for example.

Thank you for your reply! :slight_smile:

I don’t completely understand how it works, but I think that would’t work within the week? If I used it on Monday for Tuesday the output wouldn’t be tomorrow but the Tuesday the week after, wouldn’t it? I suppose if delta_days is more than 7, 7 would have to be substracted…? No idea how to write that. :laughing:

And another question: How would I add a fixed time, for example always at 09:00?

Currently my Automation doesn’t yet create an event on at all, but I guess that’s because of my fixed end_date_time, because the error is “Expected all values to have the same timezone”?

Here’s what I have so far. I’m using a toggle, not sure if that’s the best way either:

alias: Event on Sunday
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.cal_toggle_sun
    from: null
    to: null
condition: []
action:
  - service: calendar.create_event
    metadata: {}
    data:
      summary: Testevent
      start_date_time: >
        {% set delta_days = 7 - now().timetuple().tm_wday + 6 %} {{ now() +
        timedelta(days = delta_days) }}
      end_date_time: "2024-03-17 00:10:00"
    target:
      entity_id: calendar.home
mode: single

wrt your monday asking for next tuesday, yes it does the right thing.

that’s the purpose of the 7 - now() part. i did that to get to the end of this week.
so if you asked for next tuesday (wday == 1) and it’s monday (wday == 0),
then it does 7 - 0 (monday). then adds 1 (tuesday). so you get 8. so delta_days == 8 days from now.

on, for setting your time, just pull the date out and add on your time… so for example, change that last part to this to set it at 13:00 (note the leading space is important:

{{ (now() + timedelta(days = delta_days)).date() | string + " 13:00" }}

I world make a generic script, and then you can change the parameters for the script in the front end.

alias: Create event
sequence:
  - variables:
      appt_datetime: >
        {% set map = dict(mon=0, tue=1, wed=2, thu=3, fri=4, sat=5, sun=6) %}
        {% set weekday_no = map[weekday|lower] %}
        {% set add_days = (weekday_no - 1 - today_at().weekday() ) % 7 + 1 %}
        {{ today_at(time) + timedelta(days=add_days) }}
      duration_args: >
        {% set h, m = duration.split(':') | map('int') | list %}
        {{ dict(hours=h, minutes=m) }}
  - service: calendar.create_event
    metadata: {}
    data:
      summary: "{{ name }}"
      start_date_time: "{{ appt_datetime }}"
      end_date_time: "{{ appt_datetime | as_datetime + timedelta(**duration_args) }}"
    target:
      entity_id: calendar.home
mode: single

Then your button on the dashboard can be customized however you see fit:

- type: button
  tap_action:
    action: call-service
    service: script.create_event
    data:
      name: Working from home
      weekday: Thu
      time: '09:00'
      duration: '08:00'
1 Like

What I meant was that I don’t want it to necessarily be next week. If today is Monday and I want it to set it on next Tuesday, then it should be 1 day from now, not 8. I’m sorry, I didn’t make that clear in my description. So if delta_days is more than 7, I want it to substract 7, if it’s less, I don’t want to substract 7.

And thanks for the example for the set time!

Thank you for your script! I tried it out, but I get an error: UndefinedError: ‘dict object’ has no attribute ‘’

grafik

Update: OK, the problem was that when creating the script the entity id “script.1709…” was assigned. I changed it manually to “script.create_event” but the button didn’t recognize the new entity ID and suggested “script.17…”. But using this resulted in this error.
So I started over and assigned the “script. create_event” as entity ID on creation, and now it’s working, thanks a lot!

Is there any particular reason why you chose to get the current weekday that way instead of directly from now() like this?

now().weekday()

FWIW, if you use the ISO standard for weekdays (Monday=1, Sunday=7) it helps to simplify the calculation.

  - variables:
      appt_datetime: >
        {% set days = dict(mon=1, tue=2, wed=3, thu=4, fri=5, sat=6, sun=7).get(weekday|lower, 1)
          - now().isoweekday() %}
        {{ today_at(time) + timedelta(days = iif(days <= 0, days+7, days)) }}
1 Like