Automation pHAT and Home Assistant

Hello!

I recently purchased my first (and likely not my last!) Pimoroni product: an Automation pHAT. I was tinkering with it and a Pi Zero W with a PiNoir camera to make a garage door controller. It was pretty easy to set up a simple configuration with a reed switch to tell if the door was closed, use the relay to control the garage door opener, and set the camera up with motion-mmal for alerts.

I recently discovered Home Assistant, and started setting that up on a rPi 2 b. Somewhere along the way I got the bright idea to combine the two projects. I was able to get the relay working natively within Home Assistant using the instructions at https://www.home-assistant.io/components/switch.rpi_gpio/. Essentially I just added a section to my configuration file that tells the software to use GPIO 16 as a switch. Doing so added a switch to the Home Assistant GUI, and clicking it toggles the relay. Couldn’t be simpler, really. I can even add it to automations and easily control it from my phone.

I’m hoping to get some help doing something similar using the three input channels on GPIO 26 (input 1), 20 (input 2), and 21 (input 3) on the pHAT. I’m wondering if I can use them as binary sensors using the instructions at https://www.home-assistant.io/components/binary_sensor.rpi_gpio/. I did try just adding the three GPIOs as sensors, but Home Assistant always thinks they are on, no matter what I do to change the physical state of the inputs.

Does anyone have any ideas?

If you run automation pHat “input.py” example does it see then input changes you are doing?

Just to test whether the inputs are working or not? Yes, the inputs work fine using the input.py script.

Have you played with the “pull_mode” and “invert_logic” parameters?

Since the inputs on Automation HAT involve 24v tolerant protection circuit they are, unlike how you might traditionally wire a button, not inverted.

Looks like the binary_sensor is, by default, pulled “UP” and “ACTIVE HIGH”. You probably want it to be pulled down instead.

I hadn’t yet, but did just now. They work! If anyone is wondering in the future, the config that I used is:

    binary_sensor:
      - platform: rpi_gpio
        ports:
          26: pimoroni_1
          20: pimoroni_2
          21: pimoroni_3
        pull_mode: "DOWN"

For the relay it was even simpler:

    switch:
      - platform: rpi_gpio
        ports:
          16: pimoroni_relay

The pimoroni_ portions of the config are just labels, and can be changed to whatever is desired.

The screw terminals make it so easy to wire things up compared to doing everything manually. This is a great little board, at a great price.

Hi,

actually I am trying to get Automation pHat running too, but without full success. The config above incl. pull_mode: “DOWN” works, but only after original Pimoroni’s demo script is started after HA. This looks like the HA’s GPIO configuration still differs. When trying to find the difference, there looks that the original code uses no pull up/down configuration. The same happens also with plain python code (see below) or configuration with raspi-gpio. But what I am missing is - how to configure binary_sensor WITHOUT pull_mode? (as the default is “UP”).

Tested scenarios:

#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print(GPIO.input(26))

→ returns always UP

#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print(GPIO.input(26))

→ returns always DOWN

#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN)
print(GPIO.input(26))

→ returns correctly UP/DOWN as on input wire.

The same (= no pull up/down) configuration seems to be used also in the original Pimoroni’s code (see https://github.com/pimoroni/automation-hat/blob/master/library/automationhat/__init__.py#L181).

Anyone has an idea what might be wrong or how to configure “no pull up/down” from HA’s binary_sensor?
@Ezrem - does it really work for you with this config without any further GPIO (re)config?

Thanks

Instead of running the example code, what happens if you use something like WiringPi’s gpio utility to change the input mode of that pin?

sudo apt install wiringpi
gpio mode 25 in    # 25 is the WiringPi pin number, for BCM 26

May also be worth running gpio readall to see the input states and modes of each pin, to confirm they are what you expect.