top of page

Adding Google calendar as your (Thermostat) scheduler



Time to cook: 120min

Difficulty: +++++



Most devices that include a day time scheduler, such as a thermostat, tend to have a complex way to program them. In many cases this is due to the fact that the UI and controls on such devices are limited to a few buttons and a small display.


Often, you need to study the manual in detail to be able to program these kinds of schedulers, which results in most cases in a bad user experience.


But what if you could use a rich UI environment to program your scheduler, and what if this UI environment is something you use on a daily basis?


Your Calendar on your personal computer is such a rich UI which and the learning curve for it is zero. You can use your personal Calendar to schedule many things, such as switching lights on and off during certain days and timezones to simulate a presens when you are on holiday. Or turn on heavy electricity consumers during night tariff plans.


In this recipe we will showcase how to use your personal calendar to program the look alike Nest thermostat.



 

Ingredients


Prerequisites


Tools


 

The Building Plan


For this recipe uses our NodeRed platform to setup the integration with the Google environment. First set up a Google developer account if you don’t have it yet. You need it to access the Google APIs which make it possible to read and write towards your Google calendar.


You can create a Google developer account on: https://console.developers.google.com


Create a project in your Google Developer account


  • On https://console.developers.google.com create a new project

  • Within the new project click on + Enable APIS and Services

  • We need to enable 2 APIs

    • The Google Calendar API

    • The Google+ API


Note: you can scroll or search for the 2 APIs and add enable them.

Next we need to create an Oauth credential.


  • In the menu select credentials

  • In the top click on + create credentials, select Oauth client ID

  • for the Application Type, select Web application

  • Give it a name: example NodeRed (= name of the Oauth client ID)

  • 1. Oauth Client ID

  • 2. Select web application

  • Give it a name (eg NodeRed)

  • Enter the following url in the Authorized redirect URL *


note: This is the path in your application that users are redirected to after they have authenticated with Google. The path will be appended with the authorisation code for access. Must have a protocol. Cannot contain URL fragments or relative paths. Cannot be a public IP address


* For this to work, we first need to define an authorized domain. For the example above it is ‘node-red.example.com’.


If you are running on a private IP address that the Google service cannot reach. you will need to edit your hosts file and add an entry for node-red.example.com that points to the internal IP address of your client (Raspberry Pi).


  • Add node-red.example.com pointing to the RPI internal IP address in /etc/hosts on your local computer (from where you launch the browser)



Set up the Node-RED environment


We will use the Node-Red environment for the Google Calendar integration.


If you have not yet installed Node-RED then follow the instruction on the following page:



To install Node-RED on a Raspberry-Pi, you can execute the following command:


bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)


This will download and run a pre-configured script which takes care of installing all necessary components.


Next, install the node-red-node-google nodes run the following command in your Node-RED user directory - typically ~/.node-red

npm install node-red-node-google


Alternative, you can install the Google nodes via Pallette Within the Node-RED UI.


As a result, you should see the following Google Nodes in Node-RED























Get the next event from your Calendar


Lets create a flow which gets the next event from your Calendar.




The flow above is a simple example of how to achieve this.


  • Drag and drop a Google Calendar in node on a new flow.

  • Edit the Google calendar node

  • click on the Pen to configure the Google account, you should see the following window:





  • Enter the Client Id and Secret from the application you have created in your Google developers account to set up the oAuth authentication.

  • Click on Authenticate with Google


You will be redirected to the accounts.google.com page where you can select or enter the credentials of your Google account.

  • Allow access to your calendar resources


You should get the following message : Authorised - you can close this window and return to Node-RED


  • Update the Google-credentials configuration

  • In the properties window, in the Calendar field, enter the name for the Calendar you want to use (for example, i created an additional calendar in my Google account called ‘HVAC’ in which i will schedule my heating system.

  • Optionally, you can give the node a suitable name.

  • click Done.






Next wire a debug node to the Google- Calendar node to output the payload.


Test the solution


Now it’s time to test the solution.


In your google calendar. Schedule an event in the Calendar you have configured.

For example if you want to use it to schedule your heating, you can add the desired temperature in the title field.





When the start time is reached, you should see the following payload information in your node-RED environment from the debug node:





where:


title : contains the desired temp value which we easily can extract from the payload and use it in our automation.


The above tutorial is an example of how you could trigger based on events in your Google calendar. In the next, let’s see how we can add events to our Google calendar. This might be useful to ‘log’ certain information. This can be debug information, sensor data such as temperatures or any kind of information which you want to store in your calendar.



Add an event to your Calendar


Adding an event in your calendar has a similar approach than getting an event from your calendar.





The flow above is an example flow, where an event is created based on a temperature value.


  • Drag and drop a function node on the flow

  • Edit the function node and copy/paste the following code into it.


msg.payload = { description: "Thermostat setTemp", start: { 'dateTime': new Date().toISOString(), 'timeZone': 'Europe/Brussels', }, end: { 'dateTime': new Date().toISOString(), 'timeZone': 'Europe/Brussels', }, summary: msg.payload, colorId: 8, }; return msg;


This creates a payload which is used to create the calendar event. See the Google calendar API documentation on how to structure it.

  • Next drag and drop a Google Calendar out node on the flow and wire it to the output of the function node.

  • Edit the Google Calendar out node and configure the Calendar (example HVAC) and select the Google Account credentials which were already set up in the previous part.

  • Click Done

  • Drag and drop an inject node on the flow, select number and add a temp value.

  • Wire the inject node to the input of the function node

  • Deploy the flow.



Test the solution


Click on the button of the ingest node. This should result in creating an event in your calendar.


For example, you could log the set temperature, a user has set manually into your calendar and use it to ‘learn’ from it and automate your heating based on your personal habits!





}

}


8 views0 comments

Recent Posts

See All
bottom of page