Momentary switch on raspberry

Hi,
I´m working on a garden light system. I have connected several relays and momentary switch (buttons) via gpio. If i push a button the relay goes on as long as I push the button.

I want that the relay stays on as long as I push the button again. Any idea?

Here is the code I have right now:

# Relays
switch:
  platform: rpi_gpio
  assumed_state: false
  ports:
    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
  
# Taster
binary_sensor:
  - platform: rpi_gpio
    ports:
      4: button tree

automation:
  - trigger:
      platform: state
      entity_id: binary_sensor.button_tree
    action:
      service: switch.toggle
      entity_id: switch.relay_1_kirschbaum

Like I mentioned in the other thread over here: Switch relays with raspi and push button

It should already work. If not could you tell us what the state of “binary_sensor.button_tree” is when the button is not pressed and what happens when you press it to it’s state?

~Cheers

Hello again,
if I the button is not pressed the relay is off. If I press the button the relay is on.

Hope this helps…

thanks

This is the button mounted on a breadboard.

Yeah but I need to see the states in your HomeAssistant or somebody else has to chime in who has expierence with Raspi GPIO

~Cheers

What do you mean with states? I´m german and I don´t understand in this case the word states.

=== Using german to better communicate here ===
Der Status den die entität hat in home assistant auf der web gui sichtbar…wenn du den knopf drückst / gedrückt hälst / los lässt.

~Cheers

OK, now I know what you mean. the state of the button is on when i push the button it goes off, but the automation works good because the relay goes on when i push the button.

automation:
  - trigger:
      platform: state
      entity_id: binary_sensor.button_tree
      to: 'on'

try this. I assume that both - pressing and releasing the button - triggers the automation rule, so you need to focus on one state.

Oder auf Deutsch: Die Regel wird bei jeder Zustandsänderung angesprochen, wenn Du den Taster drückst und wenn Du ihn wieder loslässt. Durch 2x toggle wird dann wieder derAusgangszustand hergestellt. Mit der Änderung wird nur das Drücken des Tasters ausgewertet und das Lösen ignoriert. Sollte Deinen Wünschen entsprechen.

Perfect that´s it.

Thanks!!

Oder auf deutsch, so funktioniert es. Vielen Dank und schönes Wochenende.

Gruß

Ingo

Hallo nochmal,
ist es möglich, dass wenn ich keinen Taster drücke, das Relay zum Beispiel
einen Stunde nach Sonnenuntergang an geht und um 23 Uhr wieder aus?

Vorab herzlichen Dank

Sure,

take this as a template:

trigger: 
  platform: sun
  event: sunset
  offset: '01:00:00'
action:
  service: switch.turn_on
  entity_id: switch.relay_1_kirschbaum

and

trigger: 
  platform: time
  after: '23:00:00'
action:
  service: switch.turn_off
  entity_id: switch.relay_1_kirschbaum

Regards

m0wlheld

1 Like

Hello,
I´m testing the “sunset” automation at the moment but I think it will work.

By the way, how can I change the name of the automation? At the moment there are called
automation 0 automation 1 and so on.

regards

Ingo

Put

alias: 'Friendly Name'

before the trigger. The rule will then display as “Friendly Name” in the UI and have an entity id of automation.friendly_name

Hello,
I have the alias in my config but it doesn´t work.
Here is my code:

automation:
alias: 'Friendly Name'
  - trigger:
      platform: state
      entity_id: binary_sensor.button_tree
      to: 'on'
    action:
      service: switch.toggle
      entity_id: switch.relay_1_kirschbaum
  - trigger:
      platform: state
      entity_id: binary_sensor.button_pump
      to: 'on'
    action:
      service: switch.toggle
      entity_id: switch.relay_2_Licht_Brunnen
  - trigger:
      platform: state
      entity_id: binary_sensor.button_light_pump
      to: 'on'
    action:
      service: switch.toggle
      entity_id: switch.relay_3_Brunnen
  - trigger:
      platform: state
      entity_id: binary_sensor.button_wall
      to: 'on'
    action:
      service: switch.toggle
      entity_id: switch.relay_4_Licht_Mauer
  - trigger: 
      platform: sun
      event: sunset
      offset: '01:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.relay_1_kirschbaum
  - trigger: 
      platform: time
      after: '20:00:00'
    action:
      service: switch.turn_off
      entity_id: switch.relay_1_kirschbaum

One automation is made with an alias, one or multiple triggers (all trigger trigger THIS automation), a condition (optional) and one or more actions (all get executed). The code you have provided does not match this. What do you expect to happen here? Either you want multiple automations or you are all over the place with your sorting of stuff…

~Cheers

I want to rename the automations. Is it possible?

That is what the “friendly name” is for… or “alias”

~Cheers

Hi, I don´t know where and how I have to insert the code for friendly_name.

Need help…

regards

Ingo

automation:
  alias: 'Friendly Name'

As @m0wlheld told you…

~Cheers