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?