Google Calendar Future Event Condition?

Hi all,

I am trying to run an automation under certain conditions and I’m having trouble figuring out if it’s even possible. I want my automation to be triggered by a script turning on, with 2 conditions: (1) that I have no alarm set and (2) that my Google Calendar has an entry in my Work calendar tomorrow. I have the following code:

# Alarm Reminder When Going to Bed
- id: alarm_reminder
  alias: "Alexa Alert: Alarm Reminder"
    - platform: event
      event_type: call_service
        domain: script
        service: turn_on
          entity_id: "script.goodnight"
    - condition: and
        - condition: state
          entity_id: sensor.galaxy_s21u_next_alarm
          state: unavailable
        - condition: template
          value_template: "{{is_state_attr('', 'message', 'Work') }}"
    - service: notify.alexa_media
          - media_player.bedroom_echo_dot
        message: "Your alarm has not been set"
          type: announce
          method: all

The only problem I’m having is that I can’t figure out how to make my second condition true if I have work tomorrow. Right now, it only works if my Work Calendar has an entry for today, not tomorrow, as the state attribute only updates in realtime. Is what I want to do even possible?

This to the best of my knowledge. You can configure the Google calendar sensor to search for the word ‘Work’. It will find the current or next occurrence. My thought would be: do not make them all day events, but just half an hour in the morning. After that half an hour, the sensor will no longer show todays appointment but the next one.

Then check if the start time of that event is tomorrow.

I managed to create a calendar in Home Assistant that specifically searches for the next Work event. I have a start_time attribute, but I am trying to figure out how I can use that in my condition.

For example, the current state of start_time is ‘2021-07-12 08:00:00’, as the next time I work is this coming Monday at 8:00 am. How can I check that only the date portion of the start time is tomorrow? I’m not quite sure how to create that comparison, let alone how to isolate the date portion from the time portion.

I’ve figured it out. For anyone wondering how to do this, this is the following code to compare only the date of the state_attr of the calendar to whatever the date is 12 hours into the future (replace 43200 with 86400 for 24 hours).

        - condition: template
          value_template: >-
            {% set next_workday = state_attr('calendar.work_next', 'start_time') %}
            {% set tomorrow = (as_timestamp(now())+ (43200)) | timestamp_custom('%Y-%m-%d', True) %}
            {{ next_workday[:10] == tomorrow}}

Thank you for helping me!


Nice, you figured it out :+1:

1 Like