Home Assistant

Home Assistant & Google Calendar (8 step Guide With Images)

Wouldn’t it be great if you could control all of your Home Assistant automation with Google Calendar? With just a few simple steps we can link Google Calendar to our entire home automation setup!

In this tutorial we will learn how to connect Google Calendar with Home Assistant and use it as a way to schedule any event in our home automation setup. This is a super way to control a thermostat or schedule lighting!

super

Prerequisite

You should have Home Assistant installed and running and a basic understanding of YAML including how to edit configuration.yaml. You should also understand how to use a service and entity within Home Assistant.

If you are new to Home Assistant or you are not yet familiar with editing YAML files then you should definitely check out my beginners tutorial on YAML first. I would also recommend taking a look at my other tutorials on automation and scripts.

You will also need to sign up for a free Google account if you don’t already have one.

Setup Google

In order to link our Google calendar to Home Assistant, we will need to obtain a user ID and key from Google Developer Console.

This can seem a little daunting at first but we will skip past mostly all of the complex options so we can simply grab the client ID and client secret. All you need to do is follow the simple steps in this guide using your standard Google account.

Create a new project

First we need to head over to the Google Developer Console and create a new project. Just click this link to open it (on a new page), leave the drop-down box set to create project and click continue.

On the next screen, simply accept the terms and conditions, then click agree and continue.

You will then be given a confirmation screen, go ahead and click go to credentials.

On the next screen you can simply ignore everything and click cancel at the bottom of the page.

Setup credentials

From the left-hand sidebar, click on OAuth consent screen and then click the create button.

You will then be presented with the Create OAuth client ID screen. Set the application type to other and enter a name. You can choose anything you like but I would recommend something to reference Home Assistant.

Next we need to click on create and the following pop-up will be displayed, showing you your client ID and client secret.

These are the details we need for Home Assistant, so copy and paste them into a text file to store them temporarily. Once you have made a note of them, click ok.

Back on the main page we should check to see if the calendar API is enabled. Click library on the left-hand sidebar and type “calendar” into the search box.

Click on Google Calendar API and you will be presented with the following screen. You should see that the API is enabled, but if not then you should enable it.

Awesome! We are done with Google Developer console so you can go ahead and close it. Now we can move on to the fun part, setting up Home Assistant!

Bitmoji Image

Setup Home Assistant

Now we are ready to configure Home Assistant so go ahead and log in to the browser user interface.

Add Google Calendar integration

First we need to add the Google calendar integration to the configuration.yaml file. Go ahead and open the file in the file editor and add the following code to the end of the file.

You should replace the client ID and client secret with your own values that we just got from Google Developer Console.

google:
  client_id: YOUR_CLIENT_ID
  client_secret: YOUR_SECRET

Make sure you save the file, then restart Home Assistant. If everything worked correctly, the google_calendars.yaml file will have been added to your config directory (the same directory that configuration.yaml is stored in).

Edit google_calendars.yaml

Go ahead and open up google_calendars.yaml from your config directory. You should notice that at least one entry should have been added to the file. A basic entry will look something like this.

- cal_id: username@gmail.com
  entities:
  - device_id: sensor_name
    name: Sensor Name
    track: true

Each calendar can have a list of device_id attributes, which correspond to a sensor in Home Assistant. We can then use these sensors in the usual manor to link the calendar to any automations or scripts that we want to write.

Don’t worry if it sounds a little confusing, it will make sense when we cover an example.

Note that you should not change the value cal_id as this is the reference used to link the sensors to the calendar.

The device_id is the name that we will use to reference the sensor and it falls within the calendar entity. The name attribute is the desired friendly name and the track attribute tells Home Assistant whether or not to create a sensor for the particular item.

To link a particular event from our calendar to a Home Assistant automation, we can create a binary_sensor for the calendar event using the search attribute. This allows us to search for a particular calendar event and produce an ‘on’ state during the event.

Let’s say for example that on Friday at 8pm it is pizza night. First we will create an entry in our calendar for “pizza night.”

Now we can add the following device_id to our google_calendar.yaml with a search attribute that will test for this event. Note that we should include a hash symbol before the search term as well as using quotes.

- cal_id: username@gmail.com
  entities:
  - device_id: calendar_gmail_com
    ignore_availability: true
    name: name@gmail.com
    track: true
  - device_id: test_pizza
    name: Pizza Night
    track: true
    search: "#Pizza Night"

This will create a binary_sensor in Home Assistant called calendar.test_pizza. The state of this binary sensor will be ‘off’ unless the calendar reaches an event containing the search term “pizza night.”

Let’s create a second binary sensor for an additional event. As I am currently writing an article, I will add a binary sensor to test for “write article.”

- cal_id: username@gmail.com
  entities:
  - device_id: calendar_gmail_com
    ignore_availability: true
    name: name@gmail.com
    track: true
  - device_id: test_pizza
    name: Pizza Night
    track: true
    search: "#Pizza Night"
  - device_id: test_write_article
    name: Write Article
    track: true
    search: "#Write article"

Now if we click developer tools from the sidebar and goto the states tab, we can search for calendar entities. Here we can see the two binary sensors that we just created.

As you can see the binary sensor calendar.test_write_article is ‘on’ as this event is currently active in my calendar.

Sadly the binary sensor calendar.test_pizza is currently ‘off’ because it is not yet pizza night. However when my calendar reaches this event, the state will change to ‘on’ and I will be ordering pizza! If you live in the USA you can even automate ordering a Domino’s!

Bitmoji Image

Add event automation

So now that we have our calendar integrated into Home Assistant and we have learnt how to link an event, how do we make use of it? We simply create an automation using any of the calendar sensor entities.

When I write an article in the office, I like to set the mood by turning off the main light and switching on the lamps. I also like to play a particular playlist on Spotify whilst I work.

We can automate all of this using our new calendar entity, so in my example the office prepares itself automatically each time I am scheduled to write.

I already have a script setup called script.office_writing which triggers my Spotify playlist and sets the desired lighting. So let’s write an automation to trigger these events from the calendar.

  - alias: 'Calendar event: write article'
    trigger:
      platform: state
      entity_id: calendar.test_write_article
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.office_writing

Adding this under the automation integration or to the automation.yaml will cause the script to be triggered once the event is reached in Google Calendar!

This example is of course unique to my routine but it should give you the necessary information to build your own desired event trigger.

Adding a calendar event

It is also possible to add an event to Google calendar by using the service google.add_event. As a minimum we need to specify the cal_id of our calendar and also a sumary, which acts as the description.

However we can also specify other attributes that will appear in the calendar event. Let’s look at an example script which can add an event to our calendar.

add_to_calendar_house_party:
    alias: Schedule a house party
    description: 'It's time to party!'
    sequence:
        - alias: Trigger calendar add event
          service: google.add_event
          data:
            cal_id: username@gmail.com
            summary: title of the event
            description: description of the event
            start_date_time: 2020-03-10 20:00:00
            end_date_time: 2020-04-10 02:00:00
            start_date: "2020-03-10
            end_date: "2020-03-10
            in: "days":2

Note that you should use start_date_time and end_date_time, or start_date and end_date, or specify in as the number of days from now that you would like to add the event.

Conclusion

The Google Calendar integration in Home Assistant gives us a powerful yet simple way to schedule anything in Home Assistant. In this tutorial we have learnt all of the necessary basics to integrating the calendar and utilising events.

There are endless possibilities for combining the calendar integration with your Home Assistant setup. Here are just a few ideas you may want to consider!

plans?

Join the discussion

  1. Bill Schatzow

    I had already setup the google calendar and followed your write up to add a similar sensor and it worked. What I don’t find is good documentation on it (https://www.home-assistant.io/integrations/calendar.google/). I assume what every is allowed in sensors is allowed here? (https://www.home-assistant.io/integrations/sensor/ does give much info.) Were you able to find more information on setting up sensors? What is allowed and what isn’t? On the search again, I do know where to find how to do. Why is #needed? Are wildcards allowed? I.e “#Pizza*”. Can tasks also be added?

    • Siytek

      Hi Bill, I have only read the documentation that you mentioned. I assume that the sensor created is the same as any other standard sensor but with the associated attributes for calendar (pulled from the developer tools > states tab)

      message: Pizza Night
      all_day: false
      start_time: 2020-03-27 20:00:00
      end_time: 2020-03-27 21:00:00
      location:
      description: Pizza Night
      offset_reached: false
      friendly_name: Pizza Night

      I am not sure about why the hash symbol is required, but if I remember correctly it did not work without it. I didn’t try a wildcard either but if I get chance I will look into both of these a little further, would be good info to add to this article. Thanks for your comment and for reading the article!

  2. Bill Schatzow

    I always get concerned on undocumented things working / not working. This means they can change at anytime without any notification (i.e. breaking changes). My biggest issue with HA has been the lack of documentation and the lack of control of the documents. As an open source software system it is understandable, but adding revisions and revision dates on all releases should be a minimum.

    • Siytek

      I totally understand, and also the platform also changes so quickly! I share your concern as I worry about information I provide here going out of date quickly, although I am keeping an eye on the Github releases page. I do however believe that HA will reach a point in its level of maturity where things settle a bit, I think maybe when more functionality has moved to GUI. I guess we will have to wait and see, and for now at least there is great community support!

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top