Relay on Pi control


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?

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
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
where is the ip address of the Pi zero and 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
    command_on: ssh [email protected] -C python
    command_off: ssh pi(at) python

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) python /home/pi/
No dice.
Any suggestions?