Solved — Working with relay actions and states

Hello world,

I’m trying to figure out how to work with Hassio, a relay board and contactors.
The purpose is to configure a “switch” on GPIO#2, able to activate a relay for 0.5 second that will itself activate the contactor. Then, I’d like to read contactor’s state back on GPIO#12.
My will is to bundle all that in a simple command with state return on dashboard.

Any idea on directions to follow is welcome.

Thx

GPIO switch for the output to the relay, GPIO binary sensor for the feedback input.

Beware, the GPIO binary sensor has a habit of missing the turn off edge of short pulses.

Thank you for your time.

I read this (3 or four times ^^) and managed to have a switch turning GPIO “on/off”.
I also configured the binary_sensor (but this one doesn’t work and stays “off”).

Do you have the correct pin for the GPIO you are using?
Are you supplying the correct voltage (3.3v = on, 0v = off) or are you using a pull up resistor and a switch to ground?

Pins and voltage are OK. I missed the pull up resistor thing. Will elaborate on this and will re-open when sensor is OK.

TU

Hello again,

Thanks for your advices, I managed to get it flawlessly working adding pull-pu and pull-down resistors. Great.
Now back to my first consideration, I’d like to bundle sensor and trigger in a smart switch telling me if the circuit is closed on my dashboard, letting me to open it and reciprocally.

Should I go for a script or a command line switch ?

Why don’t you trust the GPIO output state?

Anyway you could use this:

Thank you for the link, I’ll check it tomorrow.

I do trust it but I also want to be able to keep manual action with a push button to activate my contactors. Getting contactor feedback is the best method I found to use both software and manual actions.

Well, I think there’s something I don’t get again.
My switch looks like this:

# Relay controls
switch:
  - platform: rpi_gpio
    invert_logic: true
    ports:
      2: relay1
  - platform: template
    switches:
      setone:
        value_template: "{{ is_state('sensor.relay1_state', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.relay1
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.relay1

# Relay states
binary_sensor:
  - platform: rpi_gpio
    ports:
      6: relay1_state
    bouncetime: 50
    pull_mode: "DOWN"

The rpi_gpio switch and rpi_gpio sensor are OK.
I thought having the sensor “ON” would declare the switch “ON” but that’s not the way it looks to behave.
Can I achieve this with this template ?

– EDIT –
Actually, the rpi_gpio switch is not so OK. It can turn on but turning it off is somewhat broken:
image

That’s working better using right sensor type:
value_template: "{{ is_state('binary_sensor.relay1_state', 'on') }}"

Good evening

I am trying to use relay modules to control my lamps, plugged directly into my Raspberry GPIO pins, but would like to keep my wrist switches installed, and have both control options, manual and Home Assistant. Today I use a NODEMCU with ESPHOME for this, with an external sources that feeds the 5v of the relay module and NODEMCU, with the external source GND connected to a NODEMCU GND and the pulse switches are connected to a pin and the GND of the NODEMCU to use directly on Raspberry is the connection the same?

My Code:

binary-sensor:

  • platform: gpio
    pin:
    number: GPIO2
    mode: INPUT_PULLUP
    inverted: True
    name: “Luz Cozinha”
    on_press:
    - switch.toggle: relay1

  • platform: status
    name: “Status”

switch:

  • platform: gpio
    name: “Luz Cozinha”
    pin: GPIO16
    id: relay1
    inverted: True