Switch state based on input from binary sensor and controlled by relay pulse

Hi all,

I 'm a total newbie to Home Assistant and ESPhome. In my house all the lights are controled by momentary (pulse) switches on a 24V DC circuit, the switches then give pulses to an impulse switch relay. this system works perfectly and i want to be able to continue using the system this way.

Additional to this i want to have the possibility to control all the lights “smart”. For the moment I configured a relay board that i control with an ESP32 to give a 500ms pulse to my impulse switch relay.

The problem with this is that i can’t see the current state of my light in HomeAssistant.
For this i want to use an optocoupler board to “read” the state of the light and feed the info back into the esp32.

In the lovelace dashboard i want to see a switch with the current state of the light. When i change the on-off state in home assistant this should trigger the 500ms pulse command on the relay board.

Currently i can read the state of the light and give the 500ms second pulse but i cannot combine this into a single switch on the lovelace dashboard.

Could somebody here in the community help me out with this problem?
Thanks a lot in advance for everybody reading this post and thinking along.

Hi @Schepers_HA

Tricky… just quickly looking at your wiring diagram… maybe consider trying to move the pulse switch to the ESP32 board and then use the relay to do the switching…you then you could maybe forget about the optocoupler board… in saying that I not really fully sure of your requirements… just brain storming :grinning:

Thanks for the brainstorming but i found a working solution with the code i added below.

captive_portal:

binary_sensor:
  
  - platform: gpio
    pin: 
      number: 17
      inverted: True
    name: "status relay 4"
    id: State_Relay_4
    device_class: light

switch:
   
     
  - platform: gpio
    id: Relay_4
    name: "Relay_4"
    pin: 26
    inverted: True
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: Relay_4
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: template
    name: "Template Switch"
    lambda: |-
      if (id(State_Relay_4).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - switch.turn_on: Relay_4
    turn_off_action:
      - switch.turn_on: Relay_4
2 Likes