Switch position doing really weird random things in ESPHome

I have connected up a micro switch to 3.3v and pin D1 on a nodemcu, I want to detect when a door is locked or unlocked but I am getting incredibly weird behavior, the switch is not faulty I have swapped it out with another micro switch, have tried a reed switch and even a traditional switch and all do exactly the same.

When the switch is toggled it should be either on or off depending on its position but it goes into a weird non stop loop and constantly switches between on and off (see image below and look at the timestamps), state changes are in the region of 15-20 per second.

If I test with a multimeter when its doing this the switch and board are either in the on or off position and is not actually cycling the on / off position as the software is showing.

I’m stumped, any ideas why it would be doing this?

binary_sensor: 

  - platform: gpio
    pin: D1
    name: Garage
    device_class: door

This is caused by not pulling up or down the pin either with your own resistor circuit or using the builtin resistors. I’d recommend using

mode: INPUT_PULLUP

and connecting one wire to ground and one wire to your pin.

https://learn.sparkfun.com/tutorials/pull-up-resistors/all

Thanks for the feedback, I’ve tried mode: INPUT_PULLUP previously as I read about it online but it gives me an error in ESPHome "[mode] is an invalid option for [binary_sensor.gpio]. Please check the indentation.

Can I try just with the resistor without having that in there or would it not work?

Here is snippet of working binary_sensor with pullup

binary_sensor:
  #Left button
  - platform: gpio
    id: btn_l
    name: "button_left"
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: false
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms
    internal: true
    discovery: false

Thanks Gents, that’s done the trick.

Changed config like so

binary_sensor:
  - platform: gpio
    name: "Garage"
    pin:
      number: D1
      mode: INPUT_PULLUP
#      inverted: false
    device_class: door

& soldered a 10K resistor between 3.3V and D1 then put the switch between ground and D1

This works too. When you use INPUT_PULLUP, it connects an internal resistor, so you don’t technically need the external one. Alternatively, you can use your external resistor and you could use the default with just INPUT too. But I don’t think there is anything wrong with how you have it setup. Just wanted to clarify.

I stand corrected but I think I did try that without the resistor and it didn’t work but I may be wrong, I’ll desolder it tomorrow and test to confirm, too lazy to do it how.

Thanks for the advise, learnt some new stuff today.

No need to desolder unless you are personally curious IMO. If your wires are long and have interference, the internal pull-up resistors may not be sufficient. I think they are something like 50k ohm, so yours provides stronger pull-up which can alleviate minor noise issues as well.

Yeah I’m just curios, besides our country is currently in lockdown so I have tons of time to try and kill.

For those interested I desoldered the external resistor and the switch is still working as needed.