Controlling our car gates with Apple HomeKit and CarPlay

A previous post already describes how I have integrated our Ditec VIVAH car gates with a Shelly Plus 1 and openHAB. This enabled the control of the car gates from our Bticino home automation system and also from our mobile phones via the Shelly app. While the access via the Shelly app and its widget worked, its integration into the iOS user interface was less then ideal. Also we got ourselves Volkswagen ID.4 and ID.Buzz electric vehicles that came equipped with Apple CarPlay Therefore I set out to make the car gates available as Apple HomeKit devices, using openHAB’s HomeKit Add-on.

Have a look at the diagram below for an overview of the new solution. The car gates can be controlled from iOS devices inside and outside of our local network, and also via the Apple CarPlay interface in our vehicles. The access from outside of our LAN works via iCloud and our Apple TV as hub (1), while from inside our LAN the devices talk directly to openHAB (2). OpenHAB then controls the car gates via its Shelly Binding.

Configuring the HomeKit Add-on

As start, I setup the HomeKit Add-on in openHAB according to their quick start instructions. This was straight-forward, so I’ll jump right to the next point.

Enabling Remote Access

To be able to access HomeKit devices from outside your LAN you need either an Apple TV or an Apple HomePod as hub. If outside of the LAN, the iOS device then uses an iCloud service to access HomeKit devices via the hub. Setup of the remote access is easy, you just need your devices being logged into the same iCloud account and enable the Home service.

OpenHAB Configuration of the Car Gates

The most complex aspect of setting up the car gates with HomeKit is the configuration and scripting required in openHAB. For enabling a device as garage door device in the openHAB HomeKit Add-on, you need three Points: one reflecting the current state, one to be set to a target state to initiate movement and one obstruction indicator, which if available can be linked to sensors that show if there is anything in the pathway of the moving doors.  Those points are documented in the HomeKit Add-on documentation of supported accessory types. Metadata is then added to the Item representing the car gates, and in that metadata the three Points are linked.

The following screenshots show this configuration:

Assigning HomeKit metadata to the car gate Item …

… and linking the three points.

The Point for the current state. The HomeKit metadata is automatically added when linking the Point in the Item’s metadata.

The Point for the obstruction indicator.

The Point for the target status.

As next steps, scripts are needed to work with those Points.

First, I amended the script for setting the car gate status introduced in the previous post with some code to also fill the HomeKit current state and target state. Important is here that according to the documentation, the state supports the values OPEN, CLOSED, OPENING, CLOSING and STOPPED. However I found that setting OPENING and CLOSING is not necessary. HomeKit will automatically detect if there is a difference between the current state and target state and then show the gates as opening or closing. You can download the updated script here.

Additionally it is also necessary to process command sent by the HomeKit Add-on to the target state Point. This only requires a very short script that triggers the action of the Shelly Plus 1. You can download the script here.

The following two screenshots show the configuration of the scripts in openHAB:

The scripts for setting the current state and target state …

… and for processing commands send by HomeKit to the target state.


Controlling the Car Gates

With that, the openHAB setup is complete and we can start using the car gates in HomeKit. The neatest way to do that is to include the car gates in the favorites of the Home app, after which it will show up in the iPhone’s control centre. Note that if the phone is locked, you can still access the control center, but when activating the car gates the phone asks to be unlocked, which is a sensible security feature.

The iPhone control center with a short cut to the car gates.

To get it there, the device has to be added to the favorites in the Home app.

The car gates are now also available via CarPlay and can be activated via Siri voice commands. You only need to use the car gates via Siri once, then the CarPlay dashboard will automatically show the car gates if you are close to your home:

Handling Communication Issues

Unfortunately our Shelly Plus 1 has on rare occasions communication issues with our WLAN. In this case, openHAB cannot reach the Shelly device. For this situation I have created a script that resets the HomeKit status fields and notifies the user. As can be seen in the screenshot below, the script uses the Log Reader Binding. You can download the script here.

Final Words

With the HomeKit integration our gates now are neatly accessible from our iOS devices and also from our cars via CarPlay. Again, openHAB has done a great job here to bring together the different technologies, in this case the Shelly controlling our car gates and our Apple devices. As always, if you have any questions, feel free to contact me.