I have a setup where ESP32 (ESPhome) is connected to a relay which is further connected to a bulb. The same bulb is also connected to a physical two-way switch (parallel connection). Now, when I turn on the bulb using a physical switch, I can toggle bulb state from Home assistant. Similarly, when I turn on the bulb from Home assistant, it can toggle from physical switch.
The problem starts when I use the physical switch. For example, when I turn on bulb using the physical switch, HA does not know the state of bulb and still shows off. Now when I click ON on the switch button on HA, it actually turns off. This is understood since HA does not know the state of bulb.
To overcome this issue, I attached a voltage sensor parallel to the bulb and connected the output to Pin 14 on ESP32. When the bulb is turned on, voltage indicator gives a low output and if the bulb is off, it outputs high which is connected to pin 14.
Being new to home assistant and ESPhome, I am not able to figure out how to read the pin state and change the front end of HA to reflect the same. Can someone please help with the code to control this?
I cannot do this since I need a fail safe way to turn on the bulb even if esp or ha fails. In those cases, the physical switch still works like normal.
If there is any solution to mix both, please suggest.
So you need a manual override.
If the switch is programmed to toggle even without comms then why wouldn’t that work for you ?
All else fails, unplug it
I have attached an image to show what I am trying to achieve. If you see, there is a relay on one side and a switch on the other side. The physical switch should be able to turn on and turn off the bulb as usual. The relay connected to HA should also be able to turn on and turn off the switch through ESPHome.
Now, lets take this issue where relay is OFF and switch is OFF. Now the bulb is OFF and HA says the switch status is OFF. When I turn on the relay, HA says switch status is ON and bulb is also ON. Now if I use the physical switch to turn it off, there is no change in HA switch status and still says ON. When I toggle it to OFF, the bulb turns on since it was already OFF. Hope I made this clear.
To check the status of the bulb, I have connected a voltage sensor which checks if the bulb is ON or OFF. If bulb is ON, sensor output is LOW and if bulb is OFF, sensor output is HIGH.
My only requirement is to combine voltage sensor with switch in ESPHome. If bulb is ON, voltage is low and HA should say “ON” and if the bulb is OFF, voltage is HIGH and HA should say “OFF”.
I have seen this done by someone, but not able to find the link to it after searching a lot. Any help?
Yeah, I got all that without the picture.
What I’m saying is that you remove the manual switch from the mains circuit and connect it into a low voltage loop (3.3v) direct to the esp32 device to have it toggle its output.
This is basic to a sonoff (just solder across the button loop) but any esp32 should have an available pin somewhere to serve this function.
However if you’re doing it the hard way just as a challenge (to yourself I assume) then by all means proceed.
Thank you for the advice. Maybe its the hard way, but I need a solution similar to this. Once everything is fixed, I do not want to end up in a situation where nothing works if one of the component dies (or maybe even if ESP32 dies for various reasons). I understand that I can remove it and rework, but I don’t want to lose the liberty to directly turn on or off the switch even without HA.
Kindly let me know if there is a solution. I am trying to use binary sensor code like below and working on the logic. Just a heads up…
For something like this in HA you would use switch.template. It doesn’t seem to work in esphome config. You need to use a value_template for the icon. Create a sensor for pin 14 and use it’s state.
The description of the code is wrong, ignore it. I assume you’re also using a single relay that is actually toggling from the NC --> NO and vice versa to switch. So switch.turn_on and switch.turn_off maybe a problem, since the on/off position will change depending on the physical switch. switch.toggle is probably need to use but I’m not sure how to implement it.
Tried template switch from ESPHome. This is not working. It turns on the switch status irrespective of physical switch status. Need to check if Template Switch from HA works.
binary_sensor:
- platform: gpio
pin:
number: GPIO14
inverted: True
id: sensor
name: "Sensor"
filters:
- delayed_off: 50ms
internal: True
switch:
- platform: gpio
pin: GPIO27
id: relay
name: "Relay"
internal: True
# present the state of the switch in HA based on the sensor
- platform: template
# https://esphome.io/components/switch/template.html
name: "Relay Template"
id: relay_3_switch
icon: "mdi:lightbulb"
lambda: |-
if (id(sensor).state) {
return true;
} else {
return false;
}
# https://esphome.io/guides/automations.html#if-action
turn_on_action:
- if:
condition:
binary_sensor.is_off: sensor
then:
- switch.toggle: relay
turn_off_action:
- if:
condition:
binary_sensor.is_on: sensor
then:
- switch.toggle: relay
Ok the filter is cause your having problems with the sensor turning on and off? I don’t know enough about this to answer correctly. I think one way to solve this is using a resistor in the pin 14 circuit.
The on/off state needs to be steady otherwise the icon is going to keep switching back and forth. You need to eliminate the fluctuation, I believe you can use also use template sensor but again I’m not sure how to use it.
The circuit is stable. I add a card for voltage indicator status, it turns on and off correctly. If I add a card for switch, it works correctly. I am not sure why it is so difficult to integrate it together.
When I started using ESPHome and Home assistant, I assumed this should never take so much effort. I configured everything else, but this. Anyone has a clean and easy to understand solution, kindly share.