This is my first attempt at using ESPHome, what I am attempting to do is use a generic ESP32 to determine presence of a vehicle in my driveway and if it is departing or arriving.
Currently the logic I am working on is that the device would be powered from the car, so only being powered when the car is. On boot the device defaults to ‘departing’ mode and attempts to connect to my home wifi and if successful it will connect to home assistant API.
If it fails to connect I want it to wait 5 minutes (to avoid false arrivall indication) and then set the indication to arriving. Which I will then be able to use to trigger automations in home assistant and if the car is running for a while is reverts to departing again.
My proof of concept is currently only ever returning the departing indication, never arriving. I’ve tried playing with the timing and removing the device from wifi range for extended periods but no change. I am guessing I have a problem with my logic?
Any assistance would be appreciated.
esphome:
name: car-presence
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "somekey"
reboot_timeout: 10800s
ota:
password: "somepassword"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: True
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "somessid"
password: "somepassword"
captive_portal:
globals:
- id: api_connection
type: bool
restore_value: no
initial_value: 'true'
interval:
- interval: 5s
then:
- if:
condition:
# check last connection state
- lambda: 'return { (id(api_connection) = false) };'
then:
- if:
condition:
api.connected:
then:
# just connected now
- delay: 5s
- lambda: "id(api_connection) = true;"
else:
- logger.log: "api disconnected, api_connection remains false"
else:
- if:
condition:
not:
api.connected:
then:
# disconnected
- delay: 300s
- lambda: "id(api_connection) = false;"
else:
- logger.log: "api connected, api_connection remains true"
binary_sensor:
- platform: template
name: "Arriving"
lambda: |-
if (id(api_connection) = false) {
// Car away from house for more than 5 minutes.
return true;
} else {
// Car away from house for less than 5 Minutes.
return false;
}
- platform: template
name: "Departing"
lambda: |-
if (id(api_connection) = true) {
// Car has been away from house for less than 5 minutes.
return true;
} else {
// Car away from house for more than 5 minutes.
return false;
}