Help with calendar trigger

Hi,

I think what i am trying to do is pretty simple however I’ve not been able to achieve what I am looking for and hopefully someone can help me figuring out what I am doing wrong.

what I want to achieve is to execute an action to simply turn on an input boolean based on if there is a specific all day entry in the calendar for tomorrow 6 hours before that all day event which should start at 12AM, so this mean I want this automation to run at 6PM the day before.

here is what I have, but what happen when I execute it is that the boolean switch to on even if there’s no such entry in the calendar for tomorrow.

alias: test
description: "test"
trigger:
  - platform: calendar
    event: start
    offset: "-6:0:0"
    entity_id: calendar.family
condition:
  - condition: state
    entity_id: calendar.family
    attribute: message
    state: 'Ped day'
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.school_ped_day
mode: single

let me know if you would happen to know how I can do this.

thanks !

Create an automation that runs at 6:00 PM and checks if the next calendar event:

  • Is an All Day event.
  • Its message is ‘Ped day’
  • It’s scheduled for tomorrow
alias: test
description: "test"
trigger:
  - platform: time
    at: '18:00:00'
condition:
  - "{{ state_attr('calendar.family', 'all_day') | default(false, true) }}"
  - "{{ state_attr('calendar.family', 'message') | default('', true) | trim == 'Ped day' }}"
  - >
    {% set tomorrow = (now() + timedelta(days=1)).date() %}
    {% set scheduled = (state_attr('calendar.family', 'start_time') | default('1970-01-01', true) | as_datetime | as_local).date() %}
    {{ tomorrow == scheduled }}
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.school_ped_day
mode: single

EDIT

Correction. Replace calendar.test with calendar.family.

thank you for the reply, really appreciated !

I have replaced my automation with the code you send and replaced calendar.test with calendar.family and tested the automation but it seem to have the same result, i do not have the ‘Ped day’ event tomorrow and the boolean was turned on by the automation.




I am assuming I had to replace calendar.test as mentioned above.

I do have many more automations that are working perfectly but its the first automation I am trying to do with the calendar :flushed:

The trace indicates you triggered it manually. That skips the automation’s condition and executes its action.

Yes. I overlooked to enter your calendar’s name (calendar.test is what I was using to test it). I’ll correct the example.

Allright, I did not know that manually triggering an automation would skip the conditions and execute the action. I will test it out by adding an event for tomorrow and see if it execute properly tonight and I will let you know the result.

Really appreciated your help

Thanks again.

Reference: Testing your automation

You can test it today by setting the time to something earlier than 18:00:00. Or you can use Developer Tools > Services, select the automation.trigger service and your automation, and turn off the “Skip conditions” option.

Thank you for the reference. I have created an event for tomorrow (all_day) as well as Friday the 23rd and went to the developer tool to test the template variables

{{ state_attr('calendar.family', 'all_day') | default(false, true) }}

{{ state_attr('calendar.family', 'message') | default('', true) | trim == 'Ped day' }}

{% set tomorrow = (now() + timedelta(days=1)).date() %}
{% set scheduled = (state_attr('calendar.family', 'start_time') | default('1970-01-01', true) |
as_datetime | as_local).date() %}  
{{ tomorrow == scheduled }}

I got this as a result :

False

False


  
False

then replaced 1 with 0 in the following, and that changed the result of the last variable to True

{% set tomorrow = (now() + timedelta(days=1)).date() %}

then I checked if the event of Friday would also match by replacing days=0 with days=2 but I got a False result

not sure what I am doing wrong.

Go to Developer Tools States, find calendar.family and post a screenshot of its attributes.

Whatever values you see there are what is tested by the templates. The templates can only access those values and nothing else.


Here’s the result of my test:

I do see today’s event which is not an all_day event, but I would like to automate on an event which will be the next day

message: <redacted>
all_day: false
start_time: 2022-12-20 18:00:00
end_time: 2022-12-20 19:00:00
location: <redacted>
description: 
offset_reached: false
friendly_name: Family

Not a criticism of your response, more a comment on the state of the calendar integration. Checking to see if the next event is the all-day event in question is not the correct logic. This will fail if I have something in the calendar for later tonight.
I would love to see the calendar integration augmented to allow for these types of automations. the typical pattern here is fire the automation if I have an all-day event today+/-offset called ‘xyz’.

The only visibility the template has is the calendar entity’s state and its attributes. The attributes contain information for the next scheduled event and nothing else.

In your case, the next scheduled event is today. Therefore a template can only see information about that event, not anything else scheduled in the future (like tomorrow’s all-day event or any other events you may have scheduled for tomorrow or in the future).

Once today’s events have passed, the calendar entity’s attributes will contain information about whatever it considers to be the next scheduled event which is likely to be tomorrow’s All Day event. Unfortunately, for your application, that’ll probably be too late (you want 6 hours advanced notice).

Another quirk (reported as an Issue in the GitHub Core repository) is that once you have an All Day event scheduled, any other events scheduled that day will never appear in the calendar entity’s attributes (the All Day event effectively suppresses the display of other events that day). The other events scheduled that day will still serve to trigger a Calendar Trigger but they won’t appear in the attributes.

All this to say that using the calendar entity’s attributes for making decisions has a very narrow window of usefulness and my suggestion doesn’t work for your application. In fact, the developer responsible for Local Calendar recommended not to use State Conditions based on the calendar entity’s attributes. His suggestion was to reference the trigger variable generated when the Calendar Trigger fires.

Try this:

alias: test
description: "test"
trigger:
  - platform: calendar
    event: start
    offset: "-6:0:0"
    entity_id: calendar.family
condition:
  - condition: template
    value_template: "{{ 'Ped day' in trigger.calendar_event.summary }}"
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.school_ped_day
mode: single

If that fails to work, you might want to look at this reported Issue:

negative offset from calender trigger isn’t working · Issue #84255 · home-assistant/core · GitHub

You’re absolutely correct. As mentioned in my previous post, the additional complication is that an All Day event becomes the only event displayed in the calendar entity’s attributes (i.e. any other scheduled events that day are never displayed). This makes using the entity’s attributes an unreliable means of checking information about the next scheduled event.

In another topic in the community forum, the author of Local Calendar recommended to reference the trigger variable as the best means of getting information about the event that just triggered the Calendar Trigger. I’m beginning to realize the wisdom of that advice owing to the way the calendar entity’s attributes aren’t guaranteed to accurately report information about the next scheduled event.

Ok, thank you for the suggestion. I will have a look. It is too bad that we cannot do something like that as of yet because this type of automations would definitely be really beneficial. one thing that i could have a look at is to use something external, perhaps ifttt.

I will update the post if i find something.

Today I tested an automation very similar to the latest example I posted above (employing an offset Calendar Trigger and a Template Condition that checks trigger.calendar_event).

It was set to report an All Day event scheduled tomorrow. It was offset for 11 and 2 hours prior to the event. It was successful and notified me at 13:00 and 22:00.

I recommend you try the following:

alias: test
description: "test"
trigger:
  - platform: calendar
    event: start
    offset: "-6:0:0"
    entity_id: calendar.family
condition:
  - "{{ trigger.calendar_event.all_day }}"
  - "{{ 'Ped day' in trigger.calendar_event.summary }}"
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.school_ped_day
mode: single

It should turn on the Input Boolean at 18:00 if there’s an All Day event scheduled for the following day containing the phrase Ped day.

1 Like

Thank you, I have created a new automation with the above and will check at 6PM tonight if it flips the boolean.

I also checked ifttt yesterday but I didnt find much integration with the ios calendar (as opposed to gcal). There’s the app integration, but I would need a mac for that I think. I just scratched the surface, I didnt investigated deeply.

Finger crossed for the above code and thanks again for spending time on my problem.

If you want to test it sooner, increase the offset value to a larger negative number. -12:0:0 should make it trigger at noon.

Just want to update that it seem to have worked. Both of the conditions matched my all_day calendar entry. I also tested with no all_day event matching this condition and it skipped the test. it didnt even run which I think its expected.

Thank you for your help !!

1 Like