Hello, I am trying to create an automation whereby it triggers at 9am and checks my calendar to see if I have any events scheduled within a 2 hour time frame. If not, it will notify me via phone app to go for a walk, if so nothing will happen. Based on some posts I have seen and doing some reseach here is the code I came up with but it always renders “false” even when I have no events on the calendar. Any help would be greatly appreciated!
alias: "Trigger if Calendar is Free Between 9 AM and 11 AM"
description: "This automation triggers only if there are no calendar events between 9 AM and 11 AM."
trigger:
- platform: time
at: "09:00:00" # Start at 9 AM
condition:
- condition: template
value_template: >
{% set start_time = now().replace(hour=9, minute=0, second=0, microsecond=0).timestamp() %}
{% set end_time = now().replace(hour=11, minute=0, second=0, microsecond=0).timestamp() %}
{% set event_start = as_timestamp(state_attr('calendar.d', 'start_time')) %}
{% set event_end = as_timestamp(state_attr('calendar.d', 'end_time')) %}
# If the event's start or end time overlaps with the window between 9 AM and 11 PAM, calendar is busy.
{{ (event_start is none or event_start > end_time) and
(event_end is none or event_end < start_time) }}
action:
- service: notify.ds_iphone
data:
message: "No calendar events between 9 AM and 11 AM. Time to take a walk!"
mode: single