Page 1 of 1
Two way switch for light
Posted: 18 Jan 2020, 20:34
by Modestas
Hi,
In the past I made light control project with wemos d1 mini (ESP8266), relay and act. But after two years something happening with wemos board ant I need to change it. So I decided use ESP EASY instead Arduino IDE for new wemos board.
In the proper operating system I can control light with physical switch on the wall and with Home assistant that means that I can turn On and off light how I want.
For the beginner it's not so easy make Rules configuration on ESP Easy, so I need help for it. I want to make this if else logic ( it is taken from the system that preceded ).
Code: Select all
if((GPIO12 == 1 && GPIO14 == 0)||(GPIO12 == 0 && GPIO14 == 1)){
if ( (msgString == "1")) {
digitalWrite(GPIO12,LOW);
}
if ( msgString == "0") {
digitalWrite(GPIO12,HIGH);
}
}
else{
if ( (msgString == "1")) {
digitalWrite(GPIO12,HIGH);
}
if ( msgString == "0") {
digitalWrite(GPIO12,LOW);
}
}
}
msgString is massages from Home assistant MQTT server ( ON =1, Off=0). Please help me how to make rules on ESP Easy with same logic like was on Arduino IDE.
Re: Two way switch for light
Posted: 18 Jan 2020, 22:18
by grovkillen
In you case you could simplify like this perhaps:
Code: Select all
On System#Boot Do
Monitor,GPIO,12
Monitor,GPIO,14
EndOn
On GPIO#12 Do
Let,12,%eventvalue%
Let,1,[INT#12]*[INT#14]
EndOn
On GPIO#14 Do
Let,14,%eventvalue%
Let,1,[INT#12]*[INT#14]
Endon
On msg Do
Let,2,%eventvalue%
Event,ToggleSwitch
EndOn
On ToggleSwitch Do
If [INT#1]=0 and [INT#2]=0
GPIO,12,1
Else
GPIO,12,0
EndIf
EndOn
From HA you send the http request to:
http://ipadress/cmd?event,msg=1
Or
http://ipadress/cmd?event,msg=0
Re: Two way switch for light
Posted: 19 Jan 2020, 21:17
by Modestas
Thank you for your help. I have tried this code, but unfortunately works wrong ( maybe because I used gpio 14 with Inversed Logic) . For example when system is starting:
- relay state is 0 wall switch is 0 and Feedback is 0
- then I send msg 1 than ralay = 1 , wall switch = 0 and Feedback = 1, Home assistant get signal from Feedback and I can see that light is turn ON
- My girlfriend furn off light with Wall switch, so relay = 1, wall switch =1, Feedback = 0
- further i see in HA that light is turn off ( feedback =0) and I decide turn on light from HA and send msg = 1, and nothing is happen, because relay still on state 1
Actually I don't familiar with rules and I don't understand it, I just copy and paste, maybe you can make a comment little bit maybe I need just change something
Re: Two way switch for light
Posted: 22 Jan 2020, 19:17
by Modestas
It was wrong structure of the request . It should looks like this :
http://ipadress/control?cmd=event,msg=1
http://ipadress/control?cmd=event,msg=0
I don't know, maybe you not understand my if/else function and how it should works, but your advice was completely wrong
grovkillen wrote: ↑18 Jan 2020, 22:18
On System#Boot Do
Monitor,GPIO,12
Monitor,GPIO,14
EndOn
On GPIO#12 Do
Let,12,%eventvalue%
Let,1,[INT#12]*[INT#14]
EndOn
On GPIO#14 Do
Let,14,%eventvalue%
Let,1,[INT#12]*[INT#14]
Endon
On msg Do
Let,2,%eventvalue%
Event,ToggleSwitch
EndOn
On ToggleSwitch Do
If [INT#1]=0 and [INT#2]=0
GPIO,12,1
Else
GPIO,12,0
EndIf
EndOn
I found solution by myself approximately in two day
Code: Select all
On System#Boot Do
Monitor,GPIO,12
Monitor,GPIO,14
EndOn
On GPIO#12 Do
Let,1,%eventvalue%
EndOn
On ESP_Easy_205#State Do
Let,3,%eventvalue%
If [INT#3]=1
Event,TurnOn
Else
Event,TurnOff
EndIf
EndOn
On TurnOn Do
If [INT#1] != [Virtuve_Light_State#State]
GPIO,12,0
Else
GPIO,12,1
EndIf
EndOn
On TurnOff Do
If [INT#1] != [Virtuve_Light_State#State]
GPIO,12,1
Else
GPIO,12,0
EndIf
EndOn
Configuration looks like this.
Re: Two way switch for light
Posted: 22 Jan 2020, 19:23
by grovkillen
Yes it was completely wrong but got you doing the digging yourself. Good on you!
And to my defence, I wrote that rule out of memory, on my phone.
Re: Two way switch for light
Posted: 23 Jan 2020, 17:56
by Modestas
Yes you are right
You showed me the road, thank you for that.
Re: Two way switch for light
Posted: 18 May 2020, 17:01
by deon.dup
Hi there
I found your post and code very interesting, and want to try it on a Sonoff Mini. I'm however not getting the Rule right, and my first problem starts with the MQTT. Can you perhaps please show me your config on the MQTT Import? I'm using NR, and can send any MQTT Topic, but it needs to be correct for the Rule to work. Also, on your circuit, is your GPIO-14 connected to the physical switch and GPIO-12 to the relay? I have GPIO-4 and GPIO-12, so should be easy to change the rule.