Close

Basics: Input Button or Switch

A project log for Experiences with ESPhome

Setting up some ESPhome-devices

mabe42MaBe42 a day ago0 Comments

Of course, there need also to be inputs. For example a switch or a button. I added a button to toggle the red LED. To interact between different components, the components need to have an id. So, an id needs to be added to the red LED:

light:
  - platform: monochromatic
    name: "red LED"
    id: red_led
    output: gpio_d7
    default_transition_length: 1.5s

As can be seen, the transition length can be fractions of seconds.

To add the button or switch as an input, a binary_sensor needs to be added:

binary_sensor:
  - platform: gpio
    name: "Button"
    pin: 
      number: GPIO14 #D5
      inverted: true
      mode: 
        input: true
        pullup: true 
    filters:
      - delayed_on: 50ms               # debouncing
    on_click:
      max_length: 1000ms            
      then:
        - light.toggle: red_led

The button connects the gpio pin to ground. Therefore, a pull-up resistor to Vcc is needed. This can be done using an internal pull-up. As the gpio goes to 0 when the button is pressed, inverted: true is used which provides a logic 1 when the button is pressed. max_length is used to define what is considered a click.

Now, the red LED can be toggled using the button and in addition its intensity can be changed from HA.

In HA, there is now an additional Sensors field which shows the state of the button:

Discussions