Google Calendar Event/Message as a condition in Automation

Hello everyone and Merry Christmas!

Due to the current situation, I have a question for you all.

I use an automation to warm up my car before I drive to work. I use “As soon as I get up” as a trigger, conditions are queried.
If it’s a weekend, vacation or “Urlaub” (vacation) in the calendar, then the car should NOT warm up, because I stay at home after getting up.

I use the Workday sensor, which captures weekends and public holidays, and the Google Calendar integration. The word “vacation” is in the calendar all day, but I noticed that if another entry is ABOVE the entry “vacation”, then only the first entry, the first event, the first message is queried and no longer what is below it.

As a result, my car wanted to warm up this morning, as today is a normal working day in Hesse and the calendar did not stop the automation.

In this case, “Urlaub” was not the first event, but the 4th, as an ongoing event started beforehand and is still ongoing.

Example:

Only “1 Toastbrot” is recognized in the template editor:


The condition is:

I would like to know how I can query all events from the calendar, and if one of them contains the word “Urlaub”, it should set the template to “false” and stop the automation.

According to chatgpt, Google Calendar does not output events. But I’m sure you cracks have an idea.

I would be delighted, because I would like to use this calendar entry for automations. If “Vacation” and “Away” then “Fort Knox” or something like that. Thank you!

We can only support one language here, and that language is English. We appreciate that not everybody can read and write it, but there are a wide range of options out there . We chose this language since it’s known to the moderators and developers, and also one of the most widely known languages.

Alternatively there are other places you can get help in other languages.

1 Like

sorry, I was not aware, where I posted. Thanks for telling me.

@Imaduffus
I use two seperate Google Calendars I’ve created (Work and Public Holidays) and have automations check then each night at 22:00 to for events. Any event in the Public Holidays calendar, or any an event matching “Day off” (or others words) on the Work calendar will then turn on switch that I then check and use in other things

I DONT use the workday integration as it wont correctly identify work days according to my job/location.

If you need me to help, shout

1 Like

I’ve created a date helper to use in all my automations
So I can do (in French)

  - if:
      - condition: template
        value_template: >-
          {% import 'date.jinja' as dt %} 
          {{ dt.is_weekend() or
             dt.is_conge_scolaire() or 
             dt.is_ferie() }} 
    then:

And the partial content of the date.jinja (to place in the folder custom_templates of your HA directory

{%- macro is_weekend() -%}
{{- iif(now().isoweekday() in [6, 7],'true','') -}}
{%- endmacro -%}

{%- macro is_conge_scolaire() -%}
{{- iif(is_state('calendar.conges_scolaires', 'on'),'true','') -}}
{%- endmacro -%}

{%- macro is_ferie() -%}
{{- iif(is_state('calendar.jours_feries_en_belgique', 'on'),'true','') -}}
{%- endmacro -%}

If you want to list the event of the day of one calendar, here is an exemple that you can use to inspire yourself

      - data:
          date: "{% import 'date.jinja' as dt %} {{ dt.aujourdhui() }}"
          calendrier: calendar.me_myself_gmail_com
        response_variable: olivier
        action: script.agenda


      - data:
          title: Aujourd'hui
          message: |-
            {% for key, value in olivier.items() %}
            {%- if key[-5:] != "00:00" %}
            {{- key[-5:] + " " + value -}}
            {%- else %}
            {{- value -}}  
            {%- endif %}
            {% endfor %}
        action: notify.mobile_app_iphone

The script in the first action:

alias: Agenda
sequence:
  - data:
      start_date_time: "{{ date }}"
      duration:
        hours: 24
        minutes: 0
        seconds: 0
    target:
      entity_id: "{{ calendrier }}"
    response_variable: evenements
    action: calendar.get_events
  - variables:
      reponse: |-
        {%- set ns = namespace(events={}) %}
        {% for key, value in evenements.items() %}
          {%- for event in value.events %}
            {%- set default_time = "00:00:00Z" %}
            {%- set date_format = "%Y-%m-%dT%H:%M:%S%z" %}
            {%- set datetime_var = strptime(event.start if "T" in event.start else event.start + "T" + default_time, date_format) %}
            {%- set hour = datetime_var.strftime("%d/%m/%Y %H:%M") if "T" in event.start else datetime_var.strftime("%d/%m/%Y 00:00") %}
            {%- set ns.events = dict(ns.events, **{hour:event.summary}) %}
          {%- endfor %}
        {% endfor %}
        {{ ns.events }}
  - stop: Success
    response_variable: reponse
mode: single
icon: mdi:calendar-alert-outline

You can also use the library made by petro here

1 Like