Sync Software switch State with Physical Switch

2 Likes

Okay so I have the ESP with the relay working from OH software switch. But without the ESPEasy rules how do I control the relay using the physical switch. I’m assuming I need OH to listen for ESP_377E2F/SwitchIn/Switch:state and send the ESP_377E2F/gpio/5:command depending on the state. But don’t I then have to use OH rules?
Here is my item in OH

Switch  Lamp2   "Bed Room Lamp"   <poweroutlet_us> {mqtt="<[broker:ESP_377E2F/Relay/state:state:MAP(onoff.map)],>[broker:ESP_377E2F/gpio/5:command:ON:1],>[broker:ESP_377E2F/gpio/5:command:OFF:0]"}

Here is my Devices SwitchIn is sending the MQTT status too


I took your advice and changed my Controller
image

Can you give me some advice on how to use the Physical switch to control the relay?

This is how I control my relay on an ESP8266 with ESPEasy:

Switch Alarm_Relay <switch> {mqtt=">[mosquitto:/esp8266two/gpio/4:command:ON:0],>[mosquitto:/esp8266two/gpio/4:command:OFF:1]"}

Note that I left the default values from the firmware, so I have to use a slash.

And this is a switch config:

Switch Alarm_Switch_WLAN_IN <alarm>  {mqtt="<[mosquitto:/esp8266two/alarm/switch:state:MAP(alarm.map)],<[mosquitto:/esp8266two/alarm/switch:command:ON:1],<[mosquitto:/esp8266two/alarm/switch:command:OFF:0]"}

If I understand correctly you have 2 items in your items file. One is Alarm_Relay and one is Alarm_Switch_WLAN_IN

The first switch Alarm_Relay controls the relay it’s self by turning gpio 4 high or low. The 2nd switch subscribes to
esp8266two/alarm/switch state, what do the commands do after alarm.map? Do you have 2 items in your sitemap?
One for turning the alarm on and off and the other for the alarm state?
Here is my physical setup since you can’t make out the pin numbers I’ll elaborate. The pushbutton reset switch (pulled from an old PC case) is connected to GPIO 13 and ground. the Relay is connected to GPIO 5. I guess my question is how do you tell GPIO 5 to go high or low when GPIO 13 changes it’s state? Is this possible without using any rules in ESPEasy and OH2?
image

I was a bit short on time yesterday, so let me elaborate a bit on the link I provided:

Assuming the solution in that link is a working solution (I did not try anything similar with my ESP yet, I’m just using virtual switches for my relay), this should be a working solution for you:

Switch Lamp2 "Bed Room Lamp" <poweroutlet_us> {mqtt="<[broker:ESP_377E2F/SwitchIn/Switch:command:MAP(onoff.map)],>[broker:ESP_377E2F/gpio/5:command:ON:1],>[broker:ESP_377E2F/gpio/5:command:OFF:0]"}

where onoff.map content is:

1=ON
0=OFF

If that does not work you could use a simple rule in openHAB to synchronize the switch with the relay.

Have fun.

1 Like

Hi @sihui

That does work however, (and maybe it’s just my openhab config as this is my prototype testing setup) it works faster if I use the rules on the ESP Easy. I’m assuming that’s because instead of waiting on the ESP to send a status update for SwitchIn then mosquito sends the commands to turn on the relay. When the rule is set directly on the ESP no message needs to be sent or received when the physical switch is pressed to turn on the relay. The only message Openhab has to process is the state and the speed of that is not as important to me.
Thanks again for all your help and advice on this. You really helped me learn a lot about using the ESP Easy with Openhab2.

Great, will try that too :+1: