Raspi GPIO advice

Hi guys, im attempting to control a door lock using a relay, and the RPI with GPIO.

What is the best method to achieve this? Originally I was going to go down the track of using node-red and creating a flow, which I would then trigger in HA, but upon further investigation I found that it is possible to trigger the GPIO directly from within HA?

Simple Lovelace UI with 2 button, one to lock and one to unlock, GPIO going high or low.

Thanks

You can create a GPIO switch:

Thanks, im struggling with this as the article does actually tell me how to create the button in HA to activate the GPIO, could you please explain that.

Thanks

Put this in your configuration.yaml:

switch:
  - platform: rpi_gpio
    ports:
      11: door lock

(Replace 11 with the number of the GPIO you use.)
You’ll need to restart Home Assistant for the entity to appear.

Then click on Edit dashboard in your UI, add a button card and assign your entity (e.g. switch.door_lock) to it.

Thanks for the reply! Thats helpful. So do I also need additional config in the configuration.yaml?

Thanks

Don’t delete the defaults (other stuff you already have in your configuration.yaml), but other than that, I don’t think you need to add anything else.

Great thanks.

I have 2 questions.

  1. I don’t have a entity. Do I need to create on or just make a new name in the entity second in the button.

  2. How do I tell the GPIO to turn on and off?

Thanks for the help, I’m really struggling with this.

Once you add the platform: rpi_gpio switch to your configuration.yaml and restart Home Assistant, a new switch entity will be created. You can than control the GPIO by turning the switch entity on and off.

Thanks so much for the help! Great learning from that. Its all working now. I do however have another question.

I would like a button to send a “on” only signal or ACTIVE for the relay
and another button to send a “off” only doe INACTIVE for the relay?

Is this going to require scripting to achieve?
Can I use the “:high” and “low” in the button YAML directly?

Thanks heaps

You should just control your switch entity by calling services like switch.turn_on.

I guess you could do something like this: (This is a Lovelace card. Do NOT put this in your configuration.yaml. Add a manual card to your Lovelace UI and paste it there instead.)

type: horizontal-stack
cards:
  - type: button
    tap_action:
      action: call-service
      service: switch.turn_off
      service_data: {}
      target:
        entity_id: switch.door_lock
    entity: switch.door_lock
    icon: mdi:lock-open
  - type: button
    tap_action:
      action: call-service
      service: switch.turn_on
      service_data: {}
      target:
        entity_id: switch.door_lock
    entity: switch.door_lock
    icon: mdi:lock

Thanks for the reply. Where do the reflected services go? Are they put in the configuration? For example how does it know what service: switch.turn_on does? Or is that just understood by default?

Also, I have another strange issue. Im not YAML pro but I do know its very fussy on indentation so this could be something im doing wrong.

I have the following in my configuration.yaml

switch:
  - platform: rpi_gpio
    ports:
      4: door lock

relay2:
  - platform: rpi_gpio
    ports:
      17: relay2

relay3:
  - platform: rpi_gpio
    ports:
      27: relay3

relay4:
  - platform: rpi_gpio
    ports:
      22: relay4

relay5:
  - platform: rpi_gpio
    ports:
      5: relay5

relay6:
  - platform: rpi_gpio
    ports:
      6: relay6

relay7:
  - platform: rpi_gpio
    ports:
      13: relay7

relay8:
  - platform: rpi_gpio
    ports:
      26: relay8

However, I am receiving this error in the notifications.

Invalid config

The following integrations and platforms could not be set up:

  • relay2
  • relay3
  • relay4
  • relay5
  • relay6
  • relay7
  • relay8

Please check your config and logs.

My logs as follows.

Setup failed for relaya: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay2: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay3: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay4: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay5: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay6: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay7: Integration not found.
7:26:03 PM – (ERROR) setup.py

Setup failed for relay8: Integration not found.
7:26:03 PM – (ERROR) setup.py

However, door lock is working perfectly (relay1)

Thanks again for the help!

Remove the lines relay2: , 3 and so on. Leave the other code.
They are all under switch

Ohh gotcha, cheers!

switch.turn_on is a service that can operate on switch entities. In my example, I specified a target with entity_id: switch.door_lock.

Keeps track which switches are in your environment, their state and allows you to control them.

  • Maintains a state per switch and a combined state all_switches.
  • Registers services switch.turn_on, switch.turn_off, and switch.toggle to control switches.

You can play with services in developer tools.

Would this mean,

When I create a button called “lock” and call service "switch.turn_on, it will ignore the request if the given switch is already on,

and visa versa for the switch.turn_off function?

I want “lock” to only switch on the relay
and “unlock” to only switch off the relay.

Thanks

It won’t ignore the request, but it will try to set the gpio pin (which is already logical high) to high. That’s effectively the same as ignoring the request.
Source code here:

Excellent, I got it working.

Thanks for the help! Legend!