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!
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.
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
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.
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!
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).
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: firstname.lastname@example.org 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: email@example.com entities: - device_id: calendar_gmail_com ignore_availability: true name: firstname.lastname@example.org 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: email@example.com entities: - device_id: calendar_gmail_com ignore_availability: true name: firstname.lastname@example.org 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!
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.
- 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: email@example.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.
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!
- Movie night every friday? Get Home Assistant to automatically dim the lights and control your media player.
- Link your Home Assistant thermostat integration so you can schedule your heating and air conditioning with Google Calendar.
- Automate your lights to come on when you get home from work or when you begin your morning routine.
- Schedule any automation or script with the Google Calendar interface, such as boiling the kettle in the morning or watering the plants.
- Schedule any trigger in IFTTT along with your favorite automations using webhooks.
- Use Apple Home and Siri to add events to your Google calendar.