Home Assistant + ESPHome + Latching relays

Hi all!
The problem:
I want to turn on/off lights using multiple switches and latching relays in a central box. I want to connect that in a fail-safe way, so that I can disable home assistant and the house keeps working as normal.

The context:
I am rebuilding my house, and starting to learn about HA because I want to automate as much as possible.
I want to build resilient solutions, so if something goes wrong, or If I have to sell the house and the new owner wants me to remove everything, I can do that easily.

Instead of three-way switches, my electrician always uses latching relays in a central box and push buttons. This is much cleaner when you have more than two switches, and this also gives me the opportunity to change some stuffs.

I have two options in mind. Option 1 is to buy something like Shelly PRO. If one day I don’t like it any more, i ca change Shellys by standard latching relays and everything will work.

Option 2:
Create some ESPHome device that acts as a push button within the standard circuit (draft diagram):

I really like this option because if something breaks or I want to remove all automation, I can simply shut it down without changing anything else.

My problem with this designs are:

  1. How can I give feedback to HA when the light changes state due to a push-button?"
    Since I am new into HA. It is not clear for me if it is possible to update the state of the light so that the UI (and tablet dashboards) are updated without triggering a new light switching (and create an infinite loop)
  2. What is the best way to read the light state in hardware from ESP Home?. I was thinking in either using a tiny power supply to transform 220vAC into 3.3vDC with a Traco/Recon (2.1), or using an SCT clamp (2.2):

(2.1) https://www.distrelec.ch/en/ac-dc-converter-5w-3v-1a-pc-pins-recom-rac3-3sk-277/p/30163087?queryFromSuggest=true

Could you please guide me so I can start searching and learning in the right direction?

Thanks a lot!

You do know these ones?

[6,87€ 46% OFF]16A Mini Smart Wifi DIY Switch Support 2 Way Control Smart Home Automation Module Work with Alexa Google Home Smart Life App Module Board from Electronic Components & Supplies on banggood https://banggood.app.link/X0uzGJ7SXib

or dimmers
[16,06€ 24% OFF]MoesHouse Mini DIY Tuya ZigBee3.0 Smart Dimmer Switch Module Hub Required Smart Life App Alexa Google Home Voice Control 1/2Way Smart Home from Consumer Electronics on banggood https://banggood.app.link/pu6grqsTXib

I flashed these with esphome and use them with the original switches (using a piece of rubber foam under the switch cover to transform them into a pulse switch :wink:)

Great idea. How do you wire that?
Would you replace the latching relay by those devices?

Thanks!

No relay;
I just placed the module behind the (existing) light bulb and wired it like this:


Now these modules expect a push (pulse) switch, and not a on/off.
So I placed a piece of rubber under one side of the cover, to make it bounce back…
(as new switches turned out to be more expensive as the module…)

1 Like

Sounds like a good idea. My last question, can you connect multiple switches in parallel to the “S”? Thank you!

One of options is to use an optocoupler. You can power primary side without transformer, just like led, powered on 230V: with resistor. You can find schematics on google for that. Some use a resistor of, say, around 100kohm, it should work even without diode. Some trials should be needed to find ideal value. Then you connect output of optocoupler to input of esp module and you have on/off information.
Oh, maybe you’ll need an electrolytic capacitor on the secondary side to smooth signal, otherwise it can happen that you’d have 50 signal changes per second (as 50Hz from mains).

1 Like

As long as they are the pulsing type…yes you can use as many parallel switches as you like…

1 Like

Only problem is (i experienced similar situation) if esp module goes bad in a way that it keeps holding contact - in this situation you won’t be able to manage lights until you disconnect broken module. (but, same situation will happen if one of your “customized” switches decides NOT to pop out when you press it :joy:…)
But, that’s already an extreme situation, i guess…a man just can’t make an absolute bulletproof system…

That is true. And that is why I prefer my idea over adding a smart device (like Shelly or the recommended by aceindy). I want to control many relays with one ESP, and the ESP can have physical kill switch to remove all power from it. That way by shutting down one master switch I can turn off my entire automation. That is easier than having to call the electrician to remove something from the main board (I don’t want to mess with that board) on a Saturday night because my lights are playing disco dancing :stuck_out_tongue:
Regarding HA. Do you know if I can update the light state without triggering anything? So that the state gets updated when I use the standard circuit?

If you mean HA, states are read after restart, so they will be same as before restart and no devices will be changed or triggered.
But, if you mean restarting ESPHome modules thing is different: you must program all output’s states at boot what will be (ON or OFF), or program that states are restored to the same state as before reboot/power loss (restore from flash).

Sorry, I was not clear enough. In my solution, I can push a wall button, that will trigger the latching relay and turn on the light. This information somehow has to go back to HA, to notify to it that the light has been turned on. I am not sure how to do that in HA. How can I have an INPUT EspHome pin updating the state of the light in HA. Thanks!

All states on ESPHome modules are re-read when you restart HA, so i think this is not a problem. When HA is restarted all ESPHome modules are disconnected, and after HA starts again it re-connects to all modules and it reads all their current states. I never had any problems with ESPHome input states and i’m constantly “tinkering” with HA which often requires restart.

Thank you very much for your help!! my question is not about ESP getting restarted, is about the light being turned-on or off from a circuit outside ESPHome. In ESPHome I have have one pin for the light, but how can i configure the Esp_input_read_light_state (from my diagram). How can I “notify” ESPHome or HA that the light have been turned on/off with a manual switch

Just fyi, the modules i mentioned also work stand alone (without HA), that was also one of reasons i used them, just in case, but so far i never needed it, HA is always working :stuck_out_tongue:

And their state is read when HA starts up…

One of options is to use an extra optocoupler as i mentioned above and connect it parralel with light bulb to ESP input pin - that way HA will always know state of bulb. In any case you must connect something from lightbulb to ESP input and then ESP will notify HA, that’s pretty much only way to really know the state of the bulb.

Yes! I really your idea. But how can i configure that in HA? any hints on what I should look at? I am very begginer with ESPHome and HA, and I can’t figure out how to change the sate of the bulb based on a GPIO, without making it trigger another action

You configure one pulsed output in ESPHome. With that output you switch your latching relay on and off. Then you configure one binary sensor as input - that input you use for on/off detection = connect optocoupler to it… Beware, only 3.3V at input! If you use opto you can only make a shor circuit between GPIO and GND, since input is already configured as internal pullup resistor, so normal state is 3.3V.

In HA both entities will appear, one as switch and another as binary input sensor.

Here is one example: i have one output (named “radio_rele”) with that i turn on and off an old radio receiver This output is activated for 500ms. Then i have one input (named “radio_stanje”) that one i use to monitor state of radio (it’s connected to radio’s power supply).

Then there’s also a third entity as an option (“radio_tipka”) that’s an extra pushbutton on ESP module - with that i can also turn on and off my radio.

When using this setup i can also turn on radio with original button and ESP will still be able to see this via input GPIO “radio_stanje”

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: Radio tipka
    on_press:
      - switch.turn_on: radio_rele  

  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: false
    name: Radio stanje

switch:
  - platform: gpio
    name: $imenaprave rele
    pin: GPIO12
    id: radio_rele
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: radio_rele

1 Like

Thanks a lot! your answer pointed me in the right direction. I had to use a “Template” switch in order I can take actions into the relay, but report the state from the sensing GPIO.

This is the code I use:

# ####################################################################
# Latching relay Light Switch with manual push buttons
# ####################################################################
#  
# The circuit has a manual part, that combines multiple 
# toggle switches (_-_) with a latching relay.
# ESPHome acts as another toggle switch using a standard relay. 
# It also sense the output of the latching relay to notify HA if the
# light is on or off, since it could have been changed manually
#
#  Diagram:
#
#         _________ _-_ ________
#         |________ _-_ ________|
#         |________ _-_ ________|
# [AC] ___|                     |__[Latching Relay]___[Light]
#         |______ [Relay] ______|                   |
#                   |                               |
#                   |        _______________________|
#             ______|________|______
#             |   GPIO22    GPIO23  |
#             |                     |
#             |       [ESP32]       |
#             |_____________________|
#
# ####################################################################
# To make this work, ESPHome uses a Template Switch, that can receive 
# commands and respond with a calculated state:
#
#              [BinarySensor] (GPIO23)
#               |
#               | (Read state)
#               |
#               V
#
# [HA] <-----> [Template Switch]
#
#               |
#               | (Issue actions)
#               |
#               V 
#
#              [Switch] (GPIO22)
#
# ####################################################################

esphome:
# usual stuff in here

# This sensor checks if the light is really on or off by checking the voltage
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO23
      mode: INPUT_PULLUP
      inverted: True
    name: demo_led_light_2_sensor_name
    id: demo_led_light_2_sensor_id

switch:
  # This switch is a template (virtual). 
  # It allows HA to send turn_on_action and turn_off_action, but the state is
  # read directly from the binary_sensor
  - platform: template
    name: demo_led_light_2_template_name
    id: demo_led_light_2_template_id
    icon: mdi:ceiling-light
    lambda: |-
      if (id(demo_led_light_2_sensor_id).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - if:
          condition:
            binary_sensor.is_off: demo_led_light_2_sensor_id
          then:
            - switch.turn_on: demo_led_light_2_relay_id
    turn_off_action:
      - if:
          condition:
            binary_sensor.is_on: demo_led_light_2_sensor_id
          then:
            - switch.turn_on: demo_led_light_2_relay_id

  # This controls the relay. It turns it on for 2 seconds and then it turns it back off
  - platform: gpio
    id: demo_led_light_2_relay_id  
    name: demo_led_light_2_relay_name
    pin: GPIO22
    on_turn_on:
      - delay: 2000ms
      - switch.turn_off: demo_led_light_2_relay_id

Now I will try to prototype with the real hardware, look at the optocoupler for the sensor, and try to desing so I can control many circuits with the same ESP32

Thanks a lot! I have learned so much from you about ESPHome and HA from your answers!

1 Like

This is quite an old topic, but I stumbled on some information that has some relevance for this and can be helpful for others.

These are also applications with latching relays. They make use of a latching relay with two switched loads. One every impulse both loads are switched or unswitched. The relay switches the light (or other device) and the other output can switch a circuit to inform the status of the latching relay to GPIOs.

Do make sure that there is a mixture of high and low voltage, so maybe incorporate a optocoupler in the design.

Image from that post:

Image from that post:
afbeelding

What hardware you used? Latching relay or the optocoupler?