Latching relay and light component unable to get it work


Hello everyone,

I’ve been trying for three days to get a latching relay system working, controlled by an ESP-based relay with feedback from the lamp’s status. I’m using one of the latching relay’s poles connected as a dry contact to a GPIO configured with a pull-up to get the lamp’s state.

Everything works perfectly. However, in Home Assistant, I see the lamp state on one side (via a binary_sensor) and the switch on the other (via a switch).

What I’d like is to have everything on a single line, like with any other light: the toggle button and the lamp state combined. I tried using the light component, but it doesn’t work because the output parameter requires an output component, not a switch component.

The issue with the output component is that it can’t be activated for just 500ms (to trigger the latching relay) and then deactivated. I tried to manipulate the states to force this behavior, but it didn’t work.

Do you know how I can create a light component that, when I press the toggle button, activates the relay for 500ms and that’s it?

Right now with my current configuration the status is in sync with the lamp when I turn it on on the UI. then when I toggle it to off the status goes to off but the lamp on the wall stay on. When I retoggle it to on the lamp on the wall goes off status (on UI) briefly goes to on then goes off.

my current configuration (part of it) :

light:
  - platform: binary
    output: Relay
    id: Light
    name: "Light"
    on_turn_off: 
      - logger.log: "turn off starts"
      - output.turn_on:
          id: Relay
      - delay: 500ms
      - output.turn_off:
          id: Relay
      - logger.log: "turn off ends"
    on_turn_on:
      - logger.log: "turn on starts"
      - output.turn_on:
          id: Relay
      - delay: 500ms
      - output.turn_off:
          id: Relay
      - logger.log: "turn on ends"

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO18
      mode: INPUT_PULLUP
      inverted: True
    name: "Light Status"
    device_class: light
    trigger_on_initial_state : true
    on_press:
      - logger.log: "Light On - Force the status of light to be in sync"
      - if:
          condition:
            light.is_on: Light
          then:
            - logger.log: "Light status set to off"
            - light.turn_on: Light
    on_release:
      - logger.log: "Light Off - Force the status of light to be in sync"
      - if:
          condition:
            light.is_on: Light
          then:
            - logger.log: "Light status set to off"
            - light.turn_off: Light

# Individual outputs
output:
  - platform: gpio
    id: Relay
    pin:
      xl9535: xl9535_hub
      number: 10
      mode:
        output: true
      inverted: false 

May be it’s not at all the way how to make this happens.

Any help would be greatly appreciated. Thanks!

I’m not sure if I understood correctly, but if you are trying to make a light component that just toggles the relay, you need dummy output for the light.

light:
  - platform: binary
    output: dummy_output
    id: Light
    name: "Light"
...

then add new output, for example gpio output on some un-used pin.

output:
  - platform: gpio
    id: dummy_output
    pin: # whatever un-used pin here

They have a light connected to a latching relay. So the relay only needs a pulse to turn on. They are using a spare contact to feed back the state.

Yep. So the light output can’t be the same relay output that is toggled on the turn_on/off automation.

So without binary sensor…

Thanks all for your very helpfull comments. With Karosm option it’s working fine :

light:
  - platform: binary
    output: DummyRelay
    id: Light
    name: "Light"
    on_turn_off: 
      - logger.log: "turn off starts"
      - switch.turn_on:
          id: Relay_Bureau_Chambre_Marc
      - delay: 500ms
      - switch.turn_off:
          id: Relay_Bureau_Chambre_Marc
      - logger.log: "turn off ends"
    on_turn_on:
      - logger.log: "turn on starts"
      - switch.turn_on:
          id: Relay_Bureau_Chambre_Marc
      - delay: 500ms
      - switch.turn_off:
          id: Relay_Bureau_Chambre_Marc
      - logger.log: "turn on ends"

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO18
      mode: INPUT_PULLUP
      inverted: True
    name: "Light Status"
    device_class: light
    trigger_on_initial_state : true
    on_press:
      - logger.log: "Light On - Force the status of light to be in sync"
      - light.turn_on: Light
    on_release:
      - logger.log: "Light Off - Force the status of light to be in sync"
      - light.turn_off: Light

output:
  - platform: gpio
    id: DummyRelay
    pin:
      xl9535: xl9535_hub
      number: 2
      mode:
        output: true
      inverted: false

switch:
  - platform: gpio
    name: "Relay Bureau Chambre Marc"
    id: Relay_Bureau_Chambre_Marc
    icon: "mdi:lightbulb"
    pin:
      xl9535: xl9535_hub
      number: 10
      mode:
        output: true
      inverted: false

Like this it behaves properly. The only remaining issue is I need to sacrifice a Arbitrary Relay on my relay board for each latching relay. Knowing I have 8 latching relay pending this setup. I think a can use the same relay for all of them in the end.

Still, Once againt thanks a lot for your precious help.

Do you mean for dummy output? You can use just some spare gpio pin of your Esp.
Or use template output:

output:
  - platform: template
    id: DummyRelay
    type: binary

Perfect ! it did the trick.

Thanks a lot !

You’re welcome