Using Arduino to report status of my garage door to Home Assistant

A while ago (now reading the post, it is more than 3 years ago 😮), I was working on a project to get notified every time the garage door opens or closes. You can read all about it here.

Coming back to that project, I think the solution was/is far from ideal and too complicated. Now that I started to use Home Assistant to make my not so smart home a little smarter, I figured there must be a better way to be able to check if the garage door is still open and to get notified if the garage door opens or closes. The new solution might look like this:

There are a few questions here. How can I let an arduino integrate with Home Assistant? How can I let Home Assistant (based on some state change) notify us of this change, via mail for example? Next to that, I will need some setup and electronics so we can read the state of a switch or sensor.

Electronics

As already mentioned in the other post, I have an MKR WiFi 1010 board. Since I am lazy, I also bought a relay shield. This shield has a number of inputs and also has 2 relays that can be used as output. This means that should I code everything right, I could also use the arduino to open and close the garage door. Neat.

I bought a nice box for this and also bought a second hand Omron limit switch. I have assembled all the components on a wood plank and the result is shown below:

The limit switch is engaged when the garage door is closed, like is shown below:

Seems to work nicely and also looks very clean. Next we need some code to get the state of the limit switch into Home Assistant.

Code

While browsing the Internet, I came across this library. This easily integrates your arduino board with an MQTT broker. You can install mosquitto as an add-on in Home Assistant and let the library connect with it. I have created some generic code you can find here. This code supports all inputs the relay board offers and also provides toggle switches to turn on and off the two relays. I have also foreseen a "momentary" mode for the relays. The momentary mode turns on the relay for a few seconds and then turns it off again. This could then be used to trigger the garage door motor to start opening the garage door or to close it again. The MKR WiFi also has a large RGB LED and the code uses this LED to provide status of the board itself through different color codes. If it's green, the board is connected to the WiFi network and also established a connection with the MQTT broker. Any other color is used to report failures or intermediate states.

When everything is connected, the MKR WiFi should show up in Home Assistant and look like this:

I chose to pull up the inputs (pinMode(A[1], INPUT_PULLUP);) meaning when the limit switch is engaged, it will pull the input to ground, resulting in FALSE.

I have abstracted away the fact that the arduino board is connected to a garage door. Now I need to configure the sensor in Home Assistant and let it know it is a garage door.

Integrating further in Home Assistant

The limit switch is connected with A1 and by using the following snippet in Home Assistant's config, the boolean value is shown as a "garage door":

binary_sensor:
  - platform: template
    sensors:
      garage_door_sensor:
        value_template: "{{ is_state('binary_sensor.mkr_wifi_1010_01_digital_a1', 'on') }}"
        device_class: door

Using this more meaningful name (garage_door_sensor), it is possible to create automations and react upon state changes. One of the automations is shown below:

alias: Garage opening
description: ""
trigger:
  - type: turned_on
    platform: device
    device_id: 1dd8c844def0cf71f5a03429e2f9d9ca
    entity_id: 77d70202d56690aea59a8bbae43c7dc0
    domain: binary_sensor
condition: []
action:
  - service: notify.whatsapp
    metadata: {}
    data:
      message: Poort gaat open
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: Poort gaat open
mode: single

As you can see, once the grage door opens, notifications are sent to the Home Assistant app, but also via WhatsApp. Really? How does that work?

Sending notifications via WhatsApp

The original idea was to send mails once the state of the garage door changes. I could have done that, but then I discovered callmebot. This used to be a "free" service allowing you to send messages to a service like WhatsApp via REST. The "free" service only allows for one receiver, but there's also textmebot for more advanced use cases.

callmebot used to be free, but has now become a paying service. It only costs 40 cents per month (at the time of writing) though, so no issues here.

callmebot integrates in Home Assistant by coding the following YAML in your Home Assistant configuration:

notify:
  - name: WhatsApp
    platform: rest
    resource: https://api.callmebot.com/whatsapp.php
    data:
      source: HA
      phone: <your phone number>
      apikey: <your api key>

Conclusion

So now we have come full circle. The arduino board reports the status of the garage door via state changes over MQTT. Home Assistant captures these state changes and converts the abstract sensor names into a more meaningful name. We then created automations that use the standard and not standard (WhatsApp) notification service to notify a user that the garage door is either open orc closed. Neat!

Comments

Popular posts from this blog

Remove copy protection from PDF documents

The story of the Cobalt Qube

Jori Hulkkonen feat. Jerry Valuri - Lo-Fiction