Control Physical and Digital Switch with ESPHome

3-way switches cause problems in general. If you look, you’ll find plenty of threads documenting it. Your idea to monitor the line for voltage is great, I hadn’t seen that solution yet.

That’s the strength of HA though. I used to think it was a weakness with all these bits and pieces scattered about. It’s this diverse availability that allows you to cobble together the pieces you need. There’s a trade off flexibility for simplicity.

It’s keeping everything documented that is the sticking point. The question is how do you amass all the 1000’s of independent projects in a cohesive searchable database.

Yes. Voltage monitor was one of the solution I thought should work. After putting it all together and trying the best of my coding techniques, it seems like it goes into a loop of on and off when I press the switch. I understand this might be a different approach, but surprised nobody has documented similar approach.

If my solution works with the help of someone who knows how this works, I will document the entire approach. If not, I may have to give up sadly.

Thanks for your suggestions till now.

Ok so the flux, I’ve had this problem with a physical switch. I was able to over come this problem by adding a 10k resistor between the sensing pin and ground. With out the resistor activating the switch would cause the sensor to go on and off.

I have a similar setup, but the resistor is a pull up resistor (10k) connected between sensor out and 5v. The voltage sensor outputs low on voltage detection. Hence it is pulled up in all other cases.

Just to be clear, the only problem left, is that you can’t get a steady state of the voltage sensor? Could you expand on how. Like the type of sensor, the line in the a/c circuit that you’re monitoring and the readings your getting back on the pin?

No. Its not the problem with the voltage sensor. Below is the code that require changes. Hardware wise, everything is fine. It is only the software that is breaking.

To explain what is happening now, below is a truth table

|   Relay      |   Sensor         |   Relay Status on Click            |   Bulb Status   |
|   0          |       0          |   Turn On                          |   ON            |
|   1          |       1          |   Turn Off                         |   OFF           |
|   0          |       1          |   Turn Off                         |   ON            |
|   1          |       0          |   Turn On                          |   OFF           |
1. Relay is relay_3 status
2. Sensor is the voltage sensor which reads if physical bulb is on or off
3. Relay Status on Click is the action which happens when clicking on Template switch
4. Bulb Status is the actual status of the real bulb. 

So, when physical switch is turned off, everything works as expected. Clicking on Template switch, it turns on the bulb and clicking again, it turns off the bulb. The status of ON and OFF is shown correctly in the Template Switch (relay_template).

However, when the physical switch is turned on, template button works reverse. That is when I click OFF, it turns on the real bulb and when I click ON, it turns off the real bulb.

The only solution I need is how do I invert the status of template switch on HA when physical switch is turned ON (Relay is Off and Sensor On, or Relay is On and Sensor is Off status).

# Example configuration entry
binary_sensor:
  - platform: gpio
    id: relay_3_sense
    filters:
      - invert:
    pin:
      number: 14
      mode: INPUT_PULLUP
    name: "Relay 3 Sense"

# Example configuration entry
switch:
  - platform: gpio
    name: "Relay Three"
    pin: 27
    id: relay_3
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
    
  - platform: template
    name: "Room Light"
    id: relay_template
    icon: mdi:light_bulb
    lambda: |-
      if (!id(relay_3).state && !id(relay_3_sense).state) {
        return false;
      }
      else if (id(relay_3).state && id(relay_3_sense).state) {
        return true;
      }
      else if (!id(relay_3).state && id(relay_3_sense).state) {
        return false;
      }
      else if (id(relay_3).state && !id(relay_3_sense).state) {
        return true;
      }
      else {
        return {};
      }
    turn_on_action:
      - switch.turn_on: relay_3
    turn_off_action:
      - switch.turn_off: relay_3
1 Like

try this. There is no off/on position on the relay. A relay is not configured for a 3 way, you can use it this way. Expected use is only 2 wires either on the NC or the NO position and common, to open or close one circuit. You’re actually switching between two circuits simultaneously, turning one off and the other circuit on.

    turn_on_action:
      - switch.toggle: relay_3
    turn_off_action:
      - switch.toggle: relay_3

Thanks. Toggle works kind of ok now. Just that when I use Alexa, it still does not know the status. However, I guess I will live with it and leave it for more interesting things.

Thanks again for all the assistance provided.

This is the screen till now. Room Light in the image was causing all the issues.

Well lol I had another suggestion afterwards in case this didn’t work, it’s kinda hacky. First you’d need to sense both lines out of the relay to see which is active. Still at that point switching the position of turn_on and turn_off can still be problematic.

You can interlock two relays in esp. Where switching the state of one toggles the other. This way you can have two relays set opposite each other. So regardless you’ll always have a distinct call to turn_on or turn_off.

I’m not even going to attempt this code but the idea is
turn_off commands
if (line a) has voltage turn_off relay 1,
if (line b) has voltage turn_off relay 2,
You wouldn’t need the second relay, nor is it advisable to use another relay, just a spare pin.

turn_on would be a little trickier since you would have to get which voltage sensor was last active. There is a way to get it out of HA’s database. I’ve recently been trying to learn this so I can only point you to what I have been reading.

My approach of the whole project would be a so called multiway switch. Basically, you connect the live wire into a spdt switch and connect both NO and NC to the NO and NC of a second spdt switch, the common is live and goes to your applience. https://en.wikipedia.org/wiki/Multiway_switching has a few pictures which can help you visualise what i mean. The second switch would be in your case a relay.
After the wiring part is complete, you setup a template switch in HA with your voltage sensor you already have as value_template: https://www.home-assistant.io/integrations/switch.template/ . Then you create a light out of the switch with the light switch platform: https://www.home-assistant.io/integrations/light.switch/ .
This way, you get a normal light and you can switch it either from the physical or digital switch and get correct state report, without a huge config mess in HA and esphome. You only need the wiring, the sensor, setup the entities in esphome and template switch and light switch in HA.

praveen_khm - i am on similar solution lookout but but not able to get with codes in this thread…can you share your circuit diagram…and what voltage sensing chip used…

HI,
I am absolutely beginner for this ESP home automation system. Requesting to share your complete circuit diagram, type and name of sensor used.

how they connected to esp pins. that would be great help.