Gpio trigger input to output

Hi All

I’m new to HA, however I’ve got it up and running and triggering relays on my Pi. What I can’t work out how to do is, have a momentary input NO going NC on a gpio input, to toggle the state of a output pin, and subsequently update the dashboard with the status change of the switch/output.

If somebody could attach some sample code that would be a great help, I have been looking for hours for a suitable answer but can’t find anything.

Also on the pi, if I run gpio readall I get unknown command (I used the all in one installer of HA). Do I need to install something additional, wiring pi or other?..

So, basically a pushbutton connected to gpio, that switches relays on/off?

This config worked for me. Button press will toggle all relays on/off in this case.

switch.yaml

- platform: rpi_gpio
  ports:
    4: relay1
    7: relay2
    8: relay3
    9: relay4
  invert_logic: true

configuration.yaml

binary_sensor:
    platform: rpi_gpio
    ports:
      27: pibutton
    value_high: "On"
    value_low: "Off"
    invert_logic: true

automation.yaml

- alias: 'Pi Button Test'
  trigger:
    platform: state
    entity_id: binary_sensor.pibutton
    state: 'on'
  action:
    service: switch.toggle
    entity_id:
      - switch.relay1
      - switch.relay2
      - switch.relay3
      - switch.relay4

As for gpio readall, you’ll probably have to install wiring pi, but I have no idea if that’ll work alongside hass or cause problems.

1 Like

Thanks I will look into it. Yes just a push button as such.

Am I right in thinking the reference switch.toggle is related to the content of switch.yaml? Is this how things are referenced? As I say I’m very new to this, next step is temp sensors, which will probably be another minefield​:see_no_evil::see_no_evil:.

Thanks for your time

switch.toggle is the service linked to the action. You also have a switch.turn_on and switch.turn_off.

So you could have one pushbutton to only switch things on, and another to only switch things off, or toggle as per the code example.

The entity_id: in this case would be the reference.

switch.name_of_the_thing
sensor.name_of_the_thing

Usually everything lives in configuration.yaml, but you can split it up into separate files.
You’ll just have to tell hass via configuration.yaml to include them and where they are.

automation: !include automation.yaml
sensor: !include sensor.yaml
switch: !include switch.yaml

Check out the example configurations as well. Often helps me figure out how things work.

Superb info. Thanks again

Still struggling.

I had a single pushbutton configured and tested to trigger a pi GPIO output. I have now done what i think is correct in the configuration file (i havent got around to splitting automation file as yet). However when pushing the buttons they may work once but then not again ie, they will switch the ligh on int he living room but when pressed again it wont go off. Could you somebody check the following code please? :blush:

switch:
platform: rpi_gpio
ports:
17: Nursery
18: LivingRoom
27: OutsideLight
22: Heating
23: Pin16
24: Bathroom
25: Under Stairs
5: Pin 29
invert_logic: true

binary_sensor:
platform: rpi_gpio
ports:
16: OutLight
26: LRoom
20: Nursery
21: Bathroom
value_high: “On”
value_low: “Off”
invert_logic: true

automation:

  • alias: ‘OutLight’
    trigger:
    platform: state
    entity_id: binary_sensor.OutLight
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.OutsideLight
  • alias: ‘LRoom’
    trigger:
    platform: state
    entity_id: binary_sensor.LRoom
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.LivingRoom
  • alias: ‘Nursery’
    trigger:
    platform: state
    entity_id: binary_sensor.Nursery
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.Nursery
  • alias: ‘Bathroom’
    trigger:
    platform: state
    entity_id: binary_sensor.Bathroom
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.Bathroom

nudge :slight_smile: sorry not sure how to indent the code, but at this point as it works randomly not sure if its necceserry

Cheers

hi folks

me again, anybody help me on this please?

Not sure if it is related to cable distances/interference or if its the code itself.

I have normally open going closed momentary switches (push buttons) that are connected to the codes respective GPIOs. I have also tried with external pull up resistors 4.7k and 10k and still seeing the same results. When monitoring the sensor status on the HA status page, they do not always change state and when they do change state it doesnt always switch the output.

Hope somebody can assist

I have the same problem. Any suggestions would be appreciated.

1 Like

I used your Config and just reduced the number of Outputs for my needs, and adjusted it till it worked.
Here is my config.

switch:

  • platform: rpi_gpio
    ports:
    6: Relay 1
    13: Relay 2
    19: Relay 3
    26: Relay 4
    invert_logic: true

binary_sensor:

  • platform: rpi_gpio
    ports:
    4: Button 1
    17: Button 2
    22: Button 3
    27: Button 4
    value_high: “On”
    value_low: “Off”
    invert_logic: true

automation:

  • alias: ‘OutLight’
    trigger:
    platform: state
    entity_id: binary_sensor.Button_1
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.Relay_1
  • alias: ‘LRoom’
    trigger:
    platform: state
    entity_id: binary_sensor.Button_2
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.relay_2
  • alias: ‘Nursery’
    trigger:
    platform: state
    entity_id: binary_sensor.Button_3
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.Relay_3
  • alias: ‘Bathroom’
    trigger:
    platform: state
    entity_id: binary_sensor.Button_4
    state: ‘on’
    action:
    service: switch.toggle
    entity_id:
    - switch.Relay_4

This works, good luck
CVA

to check status with gpio readall, you can use this in the shell:

cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb