Switch relays with raspi and push button

Hi, I´m working on gardenlights. I have 4 relays working in HA. Now I want to
turn the relay on with a hardware push button in my garden.
I´ve connected a button with a resistor to 3.3v and to a gpio port.
Now I need the “code” for the configuration.yaml.

Thanks for your help!!!

Need more details. Is HA running on the PI? Do you want to use pull or push method?


Hi, HA is running on the PI. I want to push the button and the relay goes on. If I push the button again the relay goes off. I hope that can help.


So you would need this then: https://home-assistant.io/components/switch.rpi_gpio/


Hi, that´s the code i used to switch the relays over the web interface. But I want
to have some “real” buttons on the wall in the garden wich can turn the relay on an off.

Sorry I linked the wrong one. This is the one you want probably: https://home-assistant.io/components/binary_sensor.rpi_gpio/


Cool, the button works. Now I want to tell the button that if I push the button
once eg. relay1 goes on. If I push the button again the relay1 goes off.
Any idea?


Naive suggestion: Use an automation triggered by your button to do an action to “toggle” the switch controlling the relay.

Have the button press toggle a switch, and automations that run based on what should happen when the switch goes off or when the switch goes on, I think this is what @MojaveTom is getting at.

Thanks for your answers.
Do you have any example code witch I can use? I´m new at HS and have a lot
to learn.
This is what I have right now (there are some german words in it):

# Raspberry Pi Switches To Control GPIO Pins
  platform: rpi_gpio
  assumed_state: false
    13: Relay 1 Kirschbaum
    19: Relay 2 Licht Brunnen
    21: Relay 3 Brunnen
    26: Relay 4 Licht Mauer
#20: Relay 5 Taster
#9: Relay 6
#10: Relay 7
#11: Relay 8
#23: DHT11 Sensor
#18: GPIO 18
#22: GPIO 22
#23: GPIO 23
#24: GPIO 24
#25: GPIO 25
#26: GPIO 26
#27: GPIO 27
  invert_logic: true
# Example configuration.yaml entry
  - platform: rpi_gpio
      4: Licht Kirschbaum

Like many people before I encourage you to use english names in your config and only german stuff for friendly names…far less trouble that way…but anyway!

I do not have a raspi setup with gpio so the entity_ids and states I am using might be wrong (see the devtools for correct ones):

    platform: state
    entity_id: binary_sensor.rpi_gpio_4
    service: switch.toggle
      entity_id: switch.rpi_gpio_13

PS: Devtools are on the left side navigation at the bottom


Hi, I past the code in the config. I get an automation field in the UI but nothing happened.
I´ll try now some other gpio pins.


I´ve changed the entity_id and it works. Now I can push the button and
the relay turns on.

Sorry, for my next question! I want to push the button on time and the relay goes on and stay on. If I push the button again the relay goes off. How can I make this?

Thanks so much for your help!!

If I am not mistake this should already work this way. If not maybe someone else has to chime in because I don’t know enough of the raspi gpio component. But usually switches are toggles and stay in one state.