I’m using ESPHome (2022.12.3) on home assistant in order to remotely monitor my garage doors.
One ESP-device checks the state of the doors with GPIO, the other sits in my home and drives four LEDs, one each for door-open and one each for door-closed. I may upgrade this to a small OLED screen if I get it working.
When everything is connected it works perfectly using on_press
, on_release
and on_state
. But it’s important that the display never shows an erroneous state, and that if it doesn’t know the current state of the sensor it tries to get it.
I’ve used status_led
and Status Binary Sensor
to hide erroneous states, but I still have the problem that if either the display or the sensor looses connection I can’t get updated information. The display will start blank no matter the state of the sensor, and if the sensor drops out then reconnects all the display LEDs remain lit.
I’m sure I’m missing a grand unified way to do this- when I reset the sensor I even see the reconnection in the display logs, but I can’t trigger off it for some reason. What am I missing?
Sensor code:
esphome:
name: garage-door-sensor
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: True
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "GarageDoorSensorFallbackHotspot"
password: !secret wifi_ap_password
captive_portal:
binary_sensor:
#State of connection for HA frontend and status screen
- platform: status
name: garage_door_sensor_connection
device_class: connectivity
#senses left and right garage doors
- platform: gpio
device_class: garage_door
#publish_initial_state: true
name: "garage door left magnet"
pin:
number: 13
mode: INPUT_PULLUP
- platform: gpio
device_class: garage_door
#publish_initial_state: true
name: "garage door right magnet"
pin:
number: 12
mode: INPUT_PULLUP
Display code:
esphome:
name: garage-door-status-screen
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: True
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "GarageDoorDisplayFallbackHotspot"
password: !secret wifi_ap_password
captive_portal:
script:
#define lights config for open, closed, error
- id: left_door_open
then:
- light.turn_on: Garage_door_left_open
- light.turn_off: Garage_door_left_closed
- id: right_door_open
then:
- light.turn_on: Garage_door_right_open
- light.turn_off: Garage_door_right_closed
- id: left_door_closed
then:
- light.turn_off: Garage_door_left_open
- light.turn_on: Garage_door_left_closed
- id: right_door_closed
then:
- light.turn_off: Garage_door_right_open
- light.turn_on: Garage_door_right_closed
- id: left_door_error
then:
- light.turn_on: Garage_door_left_open
- light.turn_on: Garage_door_left_closed
- id: right_door_error
then:
- light.turn_on: Garage_door_right_open
- light.turn_on: Garage_door_right_closed
#define GPIOS for LEDs with light component. status_led flashes all LEDs if the display is not connected to HA
light:
- platform: status_led
id: Garage_door_right_closed
name: "Garage_door_right_closed"
pin: GPIO2
restore_mode: ALWAYS_OFF
internal: true
- platform: status_led
id: Garage_door_left_closed
name: "Garage_door_left_closed"
pin: GPIO0
restore_mode: ALWAYS_OFF
internal: true
- platform: status_led
id: Garage_door_right_open
name: "Garage_door_right_open"
pin: GPIO4
restore_mode: ALWAYS_OFF
internal: true
- platform: status_led
id: Garage_door_left_open
name: "Garage_door_left_open"
pin: GPIO5
restore_mode: ALWAYS_OFF
internal: true
binary_sensor:
#State of connection for HA frontend
- platform: status
name: garage_door_status_screen_connection
device_class: connectivity
#get status binary sensor for sensor from HA. This shows if the sensor is connected to wifi or not.
- platform: homeassistant
id: "is_the_garage_sensor_connected_to_homeassistant"
entity_id: binary_sensor.garage_door_sensor_connection
on_state:
then:
if:
condition:
binary_sensor.is_off: is_the_garage_sensor_connected_to_homeassistant
then:
- script.execute: left_door_error
- script.execute: right_door_error
#Get garage door status from home assistant
- platform: homeassistant
id: right_garage_door_state
entity_id: binary_sensor.garage_door_right_magnet
on_press:
- script.execute: right_door_open
on_release:
- script.execute: right_door_closed
- platform: homeassistant
id: left_garage_door_state
entity_id: binary_sensor.garage_door_left_magnet
on_press:
- script.execute: left_door_open
on_release:
- script.execute: left_door_closed
logs from display when sensor looses power then regains it:
INFO Successfully connected to garage-door-status-screen.local
[13:53:40][W][homeassistant.binary_sensor:017]: Can't convert 'unavailable' to binary state!
[13:53:40][W][homeassistant.binary_sensor:017]: Can't convert 'unavailable' to binary state!
[13:53:40][D][homeassistant.binary_sensor:026]: 'binary_sensor.garage_door_sensor_connection': Got state OFF
[13:53:40][D][binary_sensor:036]: 'is_the_garage_sensor_connected_to_homeassistant': Sending state OFF
[13:53:40][D][light:035]: 'Garage_door_left_open' Setting:
[13:53:40][D][light:046]: State: ON
[13:53:40][D][light:035]: 'Garage_door_left_closed' Setting:
[13:53:40][D][light:046]: State: ON
[13:53:40][D][light:035]: 'Garage_door_right_open' Setting:
[13:53:40][D][light:046]: State: ON
[13:53:40][D][light:035]: 'Garage_door_right_closed' Setting:
[13:53:40][D][light:046]: State: ON
[13:53:40][D][status_led:051]: 'Garage_door_right_closed': Setting state ON
[13:53:40][D][status_led:051]: 'Garage_door_left_closed': Setting state ON
[13:53:40][D][status_led:051]: 'Garage_door_right_open': Setting state ON
[13:53:40][D][status_led:051]: 'Garage_door_left_open': Setting state ON
[13:53:43][D][homeassistant.binary_sensor:026]: 'binary_sensor.garage_door_sensor_connection': Got state ON
[13:53:43][D][binary_sensor:036]: 'is_the_garage_sensor_connected_to_homeassistant': Sending state ON
[13:53:43][D][homeassistant.binary_sensor:026]: 'binary_sensor.garage_door_left_magnet': Got state ON
[13:53:43][D][homeassistant.binary_sensor:026]: 'binary_sensor.garage_door_right_magnet': Got state OFF