Trying to make a garage door opener with zigbee door sensors

Hello,

This is my first post on this forum and 1 of my first projects I try to integrate in Home Assistant. I am making a garage door opener with an ESP32, 5V relay and 2 Tuya zigbee door sensors (open and closed state) which are connected with zigbee2mqtt. I tried to write the code using github copilot and some basic programming from myself, but i keep hitting the same wall.

My code:

esphome:
name: garage-door
friendly_name: garage-door

esp32:
board: esp32dev
framework:
type: arduino

# Enable logging

logger:

# Enable Home Assistant API

api:
encryption:
key: "xxx”

ota:

* platform: esphome
password: “xxx”

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

# Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Garage-Door Fallback Hotspot”
password: "xxx”

mqtt:
broker: “192.168.x.x”
username: “mqtt-user”
password: "xxx”

captive_portal:

# Switch object to control the relay

switch:

* platform: gpio
name: “Garage Door Relay Switch”
id: garage_door_relay_switch
pin: GPIO14
on_turn_on:
  * delay: 500ms
  * switch.turn_off: garage_door_relay_switch

# Sensors for Zigbee2MQTT

sensor:

* platform: mqtt_subscribe
name: “Garage Door Closed Sensor”
id: mqtt_closed_sensor
topic: “zigbee2mqtt/Deursensor garage 1”
on_value:
then:
- lambda: |-
if (x.contains(“contact”) && x[“contact”] == false) {
id(close_sensor).publish_state(true);
} else {
id(close_sensor).publish_state(false);
}
* platform: mqtt_subscribe
name: “Garage Door Open Sensor”
id: mqtt_open_sensor
topic: “zigbee2mqtt/Deursensor garage 2”
on_value:
then:
- lambda: |-
if (x.contains(“contact”) && x[“contact”] == false) {
id(open_sensor).publish_state(true);
} else {
id(open_sensor).publish_state(false);
}
* platform: wifi_signal
name: “WiFi Signal”
update_interval: 60s

binary_sensor:

* platform: template
name: “Garage Door Closed Sensor”
id: close_sensor
lambda: |-
if (id(mqtt_closed_sensor).state) {
return true;
} else {
return false;
}
on_press:
then:
- cover.template.publish:
id: garage_door
state: CLOSED
current_operation: IDLE
- binary_sensor.template.publish:
id: is_jammed
state: OFF
- binary_sensor.template.publish:
id: is_open
state: OFF
on_release:
then:
- cover.template.publish:
id: garage_door
current_operation: OPENING
- binary_sensor.template.publish:
id: is_open
state: ON
- delay: 25s
- if:
condition:
binary_sensor.is_off: close_sensor
then:
- binary_sensor.template.publish:
id: is_jammed
state: ON
* platform: template
name: “Garage Door Open Sensor”
id: open_sensor
lambda: |-
if (id(mqtt_open_sensor).state) {
return true;
} else {
return false;
}
on_press:
then:
- cover.template.publish:
id: garage_door
state: OPEN
current_operation: IDLE
- binary_sensor.template.publish:
id: is_jammed
state: OFF
on_release:
then:
- cover.template.publish:
id: garage_door
current_operation: CLOSING
- delay: 25s
- if:
condition:
binary_sensor.is_off: close_sensor
then:
- binary_sensor.template.publish:
id: is_jammed
state: ON
* platform: template
name: “Garage Door Is Jammed”
id: is_jammed
device_class: problem
* platform: template
name: “Garage Door Is Open”
id: is_open
device_class: garage_door

cover:

* platform: template
name: “Garage Door”
id: garage_door
device_class: garage
open_action:
then:
- switch.turn_on: garage_door_relay_switch
close_action:
then:
- switch.turn_on: garage_door_relay_switch
stop_action:
then:
- switch.turn_on: garage_door_relay_switch
has_position: true
assumed_state: true
lambda: |-
if (id(close_sensor).state) {
return COVER_CLOSED;
} else if (id(open_sensor).state) {
return COVER_OPEN;
} else {
return COVER_OPENING;
}

The error i get:
Compiling .pioenvs/garage-door/libbc6/Update/HttpsOTAUpdate.cpp.o
/config/esphome/garage-door.yaml: In lambda function:
/config/esphome/garage-door.yaml:183:16: error: ‘COVER_OPENING’ was not declared in this scope
return COVER_OPENING;
^~~~~~~~~~~~~
/config/esphome/garage-door.yaml: In lambda function:
/config/esphome/garage-door.yaml:57:13: error: request for member ‘contains’ in ‘x’, which is of non-class type ‘float’
if (x.contains(“contact”) && x[“contact”] == false) {
^~~~~~~~
/config/esphome/garage-door.yaml:57:47: error: invalid types ‘float[const char [8]]’ for array subscript
if (x.contains(“contact”) && x[“contact”] == false) {
^
/config/esphome/garage-door.yaml: In lambda function:
/config/esphome/garage-door.yaml:70:13: error: request for member ‘contains’ in ‘x’, which is of non-class type ‘float’
if (x.contains(“contact”) && x[“contact”] == false) {
^~~~~~~~
/config/esphome/garage-door.yaml:70:47: error: invalid types ‘float[const char [8]]’ for array subscript
if (x.contains(“contact”) && x[“contact”] == false) {
^
Compiling .pioenvs/garage-door/libbc6/Update/Updater.cpp.o
Compiling .pioenvs/garage-door/lib69a/ESPAsyncWebServer-esphome/AsyncEventSource.cpp.o
Archiving .pioenvs/garage-door/lib01c/libFS.a
Compiling .pioenvs/garage-door/lib69a/ESPAsyncWebServer-esphome/AsyncWebSocket.cpp.o
*** [.pioenvs/garage-door/src/main.cpp.o] Error 1
========================== [FAILED] Took 7.81 seconds ==========================

What am I doing wrong here?

This is the problem with finding random code online and choosing to copy it and find out the hard way.

My suggestion to you is do a full start over. Go to the documentation and start over. Dont try taking the fast and easy route by copying code because, you end up in this position a lot, you learn absolutely nothing and dont know your head from your @ss when things break and you need to fix things because its someone else’s code that you dont understand.

The documentation is broken up into individual sections and what you need to do is read the documentation for each one that applies to you.

Id first start with your Switch and verify that is working and you can toggle it On/Off. Second, do your 2 sensors and once you have those 2 working, you can use those entities to configure your Cover.

Just an FYI but, its extremely simple to just run a pair of wires along the outside of the door track and hardwire reed sensors straight from your esp board thats also controlling your relay. Its a lot simpler, it makes your whole “smart garage door” a single system on 1 esp board using the same wireless protocol and has 0 maintenance because theres no batteries to now have to endlessly change out or worse, they die on you and cause who knows what kind of whacky issues.

You can do it the way your trying to but, i personally try steering away from doing things like that because it just adds additional reliability issues and unnecessarily overcomplicates a very simple project

Heres a 10 pack for 15$ for example.