Sensors in my garage

Wednesday, December 18th, 2024 at 08:10pm

For a long time I have been intending on monitoring the state of my garage door with my original idea being to use microswitches on the opener mechanism to determine the fully open and fully closed positions. I couldn’t work out a good way to mount the switches so over the years I have considered other options such as a wireless tilt sensor attached to the door.

Now that I have a 3D printer I realised that I was now able to design a custom bracket to hold the microswitch:

The clips over the rail for the garage door opener to hold the microswitch against the side of the carriage that is attached to the door. I am using microswitches with long arms that I bend to make sure they cleanly slide along the carriage.

Originally I wrote some code for an arduino to send the status of the door over mqtt, but recently I have been playing more with ESPHome. It doesn’t have the same satisfaction as writing it from scratch, but it sure is a lot easier and quicker.

The two microswitches are set up as binary sensors, which update the status of a template cover:

cover:
  - platform: template
    name: "Door"
    id: door
    device_class: garage

binary_sensor:
  - platform: gpio
    id: door_open
    pin:
      number: D1
      mode:
        input: true
        pullup: true
      inverted: true
    filters:
      - delayed_on_off: 10ms
    name: "Door open"
    on_press:
      - cover.template.publish:
          id: door
          state: OPEN
          current_operation: IDLE
    on_release:
      - cover.template.publish:
          id: door
          current_operation: CLOSING

  - platform: gpio
    id: door_closed
    pin:
      number: D2
      mode:
        input: true
        pullup: true
      inverted: true
    filters:
      - delayed_on_off: 10ms
    name: "Door closed"
    on_press:
      - cover.template.publish:
          id: door
          state: CLOSED
          current_operation: IDLE
    on_release:
      - cover.template.publish:
          id: door
          current_operation: OPENING

Over the years I have seen other open source projects for garage doors such as OpenGarage which uses an ultrasonic distance sensor to detect the door position. I don’t like this for door position as I have a single panel tilt door and a distance sensor could tell me if the door is open, but it can’t detect if the door is only slightly open.

But it did get me thinking because another use of the distance sensor is to detect the presence of a car. I picked up some 3.3V sensors (for better long term compatibility over the 5V type) and ESPHome made it simple to add a distance sensor and then a template sensor that uses that distance to know if my car is there or not. It is 2.3m from the sensor to the floor, or 0.9m from the sensor to the roof of the car.

sensor:
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D7
    timeout: 4m
    name: "Ultrasonic Sensor"
    id: ultrasonic_sensor
    update_interval: 10s
    filters:
      - filter_out: nan
      - median:
          window_size: 10
          send_every: 6

binary_sensor:
  - platform: template
    name: "Car"
    device_class: presence
    icon: mdi:car
    lambda: |-
      if (id(ultrasonic_sensor).state < 1 && id(ultrasonic_sensor).state > 0.8) {
        // car is in the garage
        return true;
      } else {
        // no car
        return false;
      }

As is my habit I also included a temperature sensor and then wired it up on a protoboard. To mount it to the garage roof I 3D printed a simple bracket:

You may notice that I am not controlling the door. That is because I am only interested in monitoring, I don’t want or need remote access to open the door.

Also with the D1 mini I think I am out of suitable IO to control a relay. Some of the outputs are not stable at boot so shouldn’t be used to control a relay, and those are the best ones to use for the ultrasonic and temperature sensors. Of course this is an issue that is easily solved by upgrading to an ESP32, but before I consider that I need to tidy up the wires and connect them better, possibly designing a custom PCB to hold these components.

Tagged with: ,