Relay on Pi control

Hi,

I have a relay connected to a Raspberry Pi Zero, running Raspbian Lite. The relay is connected in parallel to my garage door button. I can SSH into the Pi Zero and execute a Python script, which will then open/close the garage door.
I’d like to be able to control this via Home Assistant.
What is the best way to achieve this? Should I be looking for a way to execute the Python script from within HA? If so, how would I do that?
Should I be controlling the relay via MQTT instead of a Python script? If so, what is the best way to do that?
Is there a better option?
Thanks.

Either of your ideas would work, but I think using your existing script would be easier.

To use your existing script, you would need to define a command line switch and have the command run do the ssh and the command as one line - ssh [email protected] -C your_command.py
You would also need to make sure you enabled passwordless login from your HA pi to the other pi.

Alterntively, you could modify your Python script to connect to a broker, subscribe to a topic, and wait for messages on that topic before performing the action. Most people use the eclipse paho library and there are examples there on how to do this.

Thank you. I’m not sure how I do either of those things you suggested, but I will have a crack at it when I get home from work.

I’ve set up passwordless login. If I ssh into the Pi running Home Assistant, I can issue the command: ssh [email protected] python relay.py
where 1.2.3.4 is the ip address of the Pi zero and relay.py is the script that activates the relay, and it works like a charm. However, I cannot get it to work from within Home Assistant.
My config file looks like this:

  • platform: command_line
    switches:
    garage_door:
    command_on: ssh [email protected] -C python relay.py
    command_off: ssh pi(at)1.2.2.4 python relay.py

I’ve tried it with and without the -C, with and without quotation marks.
I have also enabled root ssh login and tried command_on: ssh root(at)1.2.2.4 python /home/pi/relay.py
No dice.
Any suggestions?