I have a NodeMCU flashed with ESPHome. I have installed a magnetic reed switch on a door. I want to use that switch to detect the state of said door, perhaps taking action or at least reporting when its state changes.
I assume connecting the switch to D4 and 3.3v is the starting point. I can’t find a pre-configured state-switch YAML code to use it. Any ideas?
I can see the entity, but no change when I open or close the switch, either with the magnet or just shorting the wires. Wires are connected to 3.3v and GPIO2 (D4) right next to it. No external resistors. Here’s the YAML:
esphome:
name: nodemcu1
platform: ESP8266
board: nodemcuv2
wifi:
ssid: "xxx"
password: "x"
# Enable logging
logger:
# Example configuration entry
sensor:
- platform: wifi_signal
name: "WiFi Signal Sensor"
update_interval: 60s
# Enable Home Assistant API
api:
password: "xxx"
ota:
password: "xxx"
binary_sensor:
- platform: gpio
name: "Garage Door is Open"
pin:
number: D4
inverted: true
mode: INPUT_PULLUP
OK, some progress. Switch, which has been tested to work, is now connected across D4 and gnd. When the switch closes (magnet is engaged…) the blue LED on the NodeMCU lights up, but no status change is sent. I confirmed this by observing silence in the ESPHome logs. The logs do show NodeMCU1 sending WiFi status messages, but no status on the switch.
Alternatively, you could use input_pulldown_16, which would require no hardware change. You are conveniently using the only pin that supports this. I use input_pullup FWIW.
Here’s the log from ESPHome. It is not reporting the repeated opening/closing cycles I’ve taken the switch through. Every time I toggle the switch, the LED near the USB port toggles with it, so I know the board is seeing the switch toggle (though I can’t say why the LED is toggling, LOL.)
INFO Reading configuration...
INFO Starting log output from nodemcu2.local using esphome API
INFO Connecting to nodemcu2.local:6053 (192.168.2.115)
INFO Successfully connected to nodemcu2.local
[12:10:00][I][app:096]: esphome version 1.13.6 compiled on Aug 2 2019, 15:44:35
[12:10:00][C][wifi:372]: WiFi:
[12:10:00][C][wifi:254]: SSID: [redacted]
[12:10:00][C][wifi:255]: IP Address: 192.168.2.115
[12:10:00][C][wifi:257]: BSSID: [redacted]
[12:10:00][C][wifi:258]: Hostname: 'nodemcu2'
[12:10:00][C][wifi:262]: Signal strength: -63 dB ▂▄▆█
[12:10:00][C][wifi:263]: Channel: 6
[12:10:00][C][wifi:264]: Subnet: 255.255.255.0
[12:10:00][C][wifi:265]: Gateway: 192.168.2.1
[12:10:00][C][wifi:266]: DNS1: 192.168.2.1
[12:10:00][C][wifi:267]: DNS2: 0.0.0.0
[12:10:00][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Garage Door'
[12:10:00][C][gpio.binary_sensor:015]: Device Class: 'garage_door'
[12:10:00][C][gpio.binary_sensor:016]: Pin: GPIO16 (Mode: INPUT_PULLUP)
[12:10:00][C][logger:137]: Logger:
[12:10:00][C][logger:138]: Level: DEBUG
[12:10:00][C][logger:139]: Log Baud Rate: 115200
[12:10:00][C][logger:140]: Hardware UART: UART0
[12:10:00][C][ota:029]: Over-The-Air Updates:
[12:10:00][C][ota:030]: Address: nodemcu2.local:8266
[12:10:00][C][ota:032]: Using Password.
[12:10:00][C][api:103]: API Server:
[12:10:00][C][api:104]: Address: nodemcu2.local:6053