Here’s a solution I’ve recently built so that when I come home in my car, the garage door would automatically open.
- Added a wifi AP near the driveway. This was originally meant to boost wifi signal for my lawn mowing robot, but helps for the auto garage opening too
- connected an esp32 supermini (these things are very cheap, only around $2 each!) into my car’s USB port. The USB ports only power on when the car is turned on / engine running. I programmed it with esphome. Its task is very simple: on mqtt connection, send its current uptime. That’s all!
- Added a rule in openhab to receive the mqtt uptime sent above, and open the garage door if all these conditions are true:
- uptime > 15 minutes
- garage door is closed
- car esp32 Thing is online (optional) - I have payloadAvailable using mqtt LWT on all my mqtt Things.
- the garage door state lastStateChange was older than 5 minutes ago (prevent rapid closing / opening) - this means if someone at home recently opened the door then closed it, it won’t open for me. I’m not sure if this is a good idea - subject to further tweaking. Maybe I could reduce this to 30 seconds or less.
So now as soon as I pull into my driveway, the garage door opens up automatically.
It means if I happened to park in the driveway, turning the car on wouldn’t open the garage door - the esp32 would connect to mqtt and sends a low uptime (less than 15 minutes). I would need to start the car somewhere 15 minutes away for this scheme to work.
I could further add some encrypted handshake in the mqtt messaging but I feel that it’s unnecessary.
The implementation is very easy that you would probably do this using your own way, but here’s mine:
esp32 esphome config:
substitutions:
device_name: car
friendly_name: "Car Garage Opener"
esp32:
board: esp32-c3-devkitm-1
framework:
type: esp-idf
packages:
# contains common basic esphome setup, wifi ssid, mqtt connection details, etc.
common: !include common/common.yaml
wifi:
# don't keep rebooting, that'd slow down the reconnection. Just keep retrying
reboot_timeout: 0s
power_save_mode: none
on_disconnect:
then:
- light.turn_off: statusled
on_connect:
then:
- light.turn_on: statusled
mqtt:
on_connect:
then:
- delay: 1s # this may not be necessary
- mqtt.publish:
topic: ${device_name}/THIS_TOPIC_WILL_TRIGGER_YOUR_RULE
payload: !lambda 'return to_string(static_cast<int>(id(uptime_sensor).state));'
light:
- platform: status_led
name: Status LED
id: statusled
pin:
number: GPIO8
inverted: true
sensor:
- platform: uptime
name: Uptime
id: uptime_sensor
The openhab rule (jruby)
rule "Car offline" do
changed "mqtt:topic:car", to: :offline
run do |event|
Car_Wifi_Connected.update nil
end
end
rule "Car arrived" do
changed Car_Wifi_Connected, to: (15.minutes.to_i..)
only_if { Car_Wifi_Connected.thing.online? }
only_if { Garage_Door.closed? }
only_if { Garage_Door.last_state_change.nil? || Garage_Door.last_state_change < 5.minutes.ago }
run do |event|
logger.info "Car arrived, online for #{event.state} seconds"
Garage_Door_Press.on # or however you would open your garage door
Voice.say "Car arrived, opening the garage door automatically" if Alarm_Armed.off?
end
end