Create an event in local calendar based on template

Hello Guys,

I have an input datetime that i change when i do for exemple pool maintenance.
i want to automate an event created in 7 days.

i tried in automation :

alias: Rappel piscine
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ (as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d')) -
      state_attr('input_datetime.date_de_la_derniere_maintenance_de_la_piscine','timestamp')
      >= 86400 * 7) == 1 }}
action:
  - service: calendar.create_event
    data:
      summary: Entretien Piscine
      description: Faire entretien piscine
      start_date_time: "{{ now() }}"
      end_date_time: "{{ now() + timedelta(minutes=600) }}"
    target:
      entity_id: calendar.piscine
mode: single
initial_state: true

But if i test, it create event. Event if the template is not equal to 1.
I also tried with this template as condition and time as trigger bu it doesn’t work.

Anyone has done it? or maybe it can’t be done based on input datetime

the result of template in dev tools :

Thanks for your help

What do you mean by this? How are you testing? Using the “Run” menu option or the automation.trigger service tests only the actions… the trigger and any conditions are skipped.

To accurately test the trigger you need to set the input datetime to a date less than 7 days ago (like today) and then set it to a date 7 or more days ago.

Instead of converting to timestamps via strptime(), just use datetime objects and methods.

alias: Rappel piscine
description: ""
trigger:
  - platform: template
    value_template: >-
      {% set plus_seven = (states('input_datetime.date_de_la_derniere_maintenance_de_la_piscine') | as_datetime | as_local + timedelta(days=7)).date() %}
      {{ now().date() >= plus_seven }}
action:
  - service: calendar.create_event
    data:
      summary: Entretien Piscine
      description: Faire entretien piscine
      start_date_time: "{{ now() }}"
      end_date_time: "{{ now() + timedelta(hours = 10) }}"
    target:
      entity_id: calendar.piscine
mode: single

Thanks for your help.
Unfortunately if i execute the automation it create the évent.


It should not as template IS false or i’m wrong?

Also tried automation trigger. With and without condition and évent IS created

Describe what you mean by this…

The automation I posted has no conditions so it will behave the same whether you use skip conditions or not… as explained in my previous post automation.trigger skips the trigger and just executes the actions.

HA Docs: Testing Automations

By execute i mean in automation three dot and execute

I need the template to be the condition : seven day After datetime input.
Thanks again

To accurately test the trigger you need to set the input datetime to a date less than 7 days ago (like today) and then set it to a date 7 or more days ago. The template’s evaluation must change from false to true in order to fire.

Thanks it seems ok today it was created automatically.
If i had in trigger a Time for exemple 9am will it create that évent a 9 or will it consider it’s already true?

As it is currently configured, the automation will create a new “Entretien Piscine” event every time it is triggered. It is possible to use the calendar.list_events service to check if an event already exists within a given time span. That would let you prevent creating multiple events, but there aren’t currently services to remove or edit existing calendar events. This makes it less useful for chores.

You may want to hold off on putting a lot of effort into this.

Based on the beta release notes, the To Do integration that was added at the beginning of November will be adding the ability to set due dates in next weeks release. To-dos already have services to add, update, and remove items, so they may be a better avenue for this kind of thing going forward.