Add a Google Calendar to openHAB

Posted by ads' corner on Tuesday, 2020-10-27
Posted in [Ansible][Linux][Openhab]

openHAB can integrate Google Calendars. The functionality is kind of limited, it can only see the current and the next calendar event, but in my case that is enough. More about the use case in another blog post.

Calendar
Calendar

There are three different calendar bindings available, let’s have a quick look:

  • CalDAV Personal Binding: this is a v1 binding, which means it will no longer work in the soon-to-be-released openHAB v3. Apparently this works with Google calendars, but has performance issues. It can show the current and next event.
  • Google Calendar Scheduler: also a v1 binding. Needs more work for presense simulation, and additional bindings.
  • iCalendar Binding: v2 binding, should work with v3. It can show the current and next event. That’s the one I’m going to use.

Binding

In order to start using the icalendar binding, it must be installed. In my case that’s a job for Ansible:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
- name: Get list of available and installed extensions
  uri:
    url: "http://{{ ansible_host }}:8080/rest/extensions"
  register: oh2_extensions
  changed_when: false

- name: Install extensions
  uri:
    url: "http://{{ ansible_host }}:8080/rest/extensions/{{ item }}/install"
    method: POST
  when: "not (oh2_extensions.json|byattr('id', item))[0].installed"
  with_items:
    - binding-icalendar
  register: oh2_install_extensions

More about this is described here.

iCal Calendar

Next comes the calendar data. Go to your Google calendar, click on the three dots next to the calendar you want to use.

iCal Calendar
iCal Calendar

And then click on Settings and sharing. In the next window, scroll down to Integrate calendar and copy the URL from the Private address in iCal format field. That is the URL which needs to be added to the openHAB configuration.

openHAB configuration

The configuration needs a Thing and Items. Let’s start with the Thing, which in my case is placed in calendar.things. Since I deploy everything with Ansible, the file is a template:

1
Thing icalendar:calendar:entries "Calendar" @ "openHAB" [ url="{{ lookup('file', playbook_dir + '/credentials/reminders-calendar-ical.txt') }}", refreshTime=15 ]

The file credentials/reminders-calendar-ical.txt holds the iCal URL which was copied from the calendar earlier. The result in /etc/openhab2/things/calendar.things:

1
Thing icalendar:calendar:entries "Calendar" @ "openHAB" [ url="https://calendar.google.com/calendar/ical/...", refreshTime=15 ]

Once the Thing is loaded, Items can be deployed. No templating here, the file /etc/openhab2/items/calendar.items has only a couple lines:

1
2
3
4
5
6
7
8
9
Switch		Event_current_event_present	"current event present"					<calendar> { channel="icalendar:calendar:events:current_presence" }

String		Event_current_event_name	"current event [%s]"					<calendar> { channel="icalendar:calendar:events:current_title" }
DateTime	Event_current_event_start	"current event start [%1$tT, %1$tY-%1$tm-%1$td]"	<calendar> { channel="icalendar:calendar:events:current_start" }
DateTime	Event_current_event_until	"current event until [%1$tT, %1$tY-%1$tm-%1$td]"	<calendar> { channel="icalendar:calendar:events:current_end" }

String		Event_next_event_name		"next event [%s]"					<calendar> { channel="icalendar:calendar:events:next_title" }
DateTime	Event_next_event_start		"next event start [%1$tT, %1$tY-%1$tm-%1$td]"		<calendar> { channel="icalendar:calendar:events:next_start" }
DateTime	Event_next_event_until		"next event until [%1$tT, %1$tY-%1$tm-%1$td]"		<calendar> { channel="icalendar:calendar:events:next_end" }

This defines a Presence switch, which indicates if one event is currently going on. And then the title, start time and end time of the current and next upcoming event. In my case I’m only interested in the presence switch, but I defined the other items anyway.


Categories: [Ansible] [Linux] [Openhab]